Mybatis集成Spring的实例代码_动力节点Java 学院整理

网友投稿 287 2023-04-10


Mybatis集成Spring的实例代码_动力节点Java 学院整理

所需要用到的其他工具或技术:

项目管理工具 : Maven

前台WEB展示:jsP

其他框架:Spring, Spring MVC

数据库 : Derby

新建一个Maven的Web项目

Maven Dependencies:

org.springframework

spring-context

4.0.0.RELEASE

org.springframework

spring-webmvc

4.0.0.RELEASE

org.springframework

spring-tx

4.0.0.RELEASE

org.springframework

spring-jdbc

4.0.0.RELEASE

org.aspectj

aspectjrt

1.6.10

org.slf4j

slf4j-api

1.6.6

org.slf4j

jcl-over-slf4j

1.6.6

runtime

org.slf4j

slf4j-log4j12

1.6.6

runtime

javax.inject

javax.inject

1

javax.servlet

servlet-api

2.5

provided

javax.servlet.jsp

jsp-api

2.1

provided

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.2.7

org.mybatis

mybatis-spring

1.2.1

junit

junit

4.9

test

org.apache.derby

derby

10.10.2.0

org.apache.derby

derbyclient

10.10.2.0

SQL建表及数据插入

CREATE TABLE USER_TEST_TB(

ID INT PRIMARY KEY,

USERNAME VARCHAR(20) NOT NULL,

PASSWORD VARCHAR(20) NOT NULL,

NICKNAME VARCHAR(20) NOT NULL

);

INSERT INTO USER_TEST_TB VALUES(1,'1st','111','Jack');

INSERT INTO USER_TEST_TB VALUES(2,'2nd','222','Rose');

INSERT INTO USER_TEST_TB VALUES(3,'3rd','333','Will');

web.xml(scr/main/webapp/WEB-INF下)

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

/WEB-INF/*Context.xml

org.springframework.web.context.ContextLoaderListener

appServlet

org.springframework.web.servlet.DispatcherServlet

1

appServlet

/

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

/WEB-INF/*Context.xml

org.springframework.web.context.ContextLoaderListener

appServlet

org.springframework.web.servlet.DispatcherServlet

1

appServlet

/

appServlet-servlet.xml(Spring的Servlet配置文件scr/main/webapp/WEB-INF下)

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:beans="http://springframework.org/schema/beans"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:beans="http://springframework.org/schema/beans"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

JSP文件

show.jsp(src/main/webapp/WEB-INF/views目录下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

USER_NAME

PASSWORD

NICK_NAME

EDIT

DELETE

${user.username}

${user.password}

${user.nickname}

edit

delete

Add new User

update.jsp(src/main/webapp/WEB-INF/views目录下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

User ID:${user.id}

Username:

Password:

Nickname:

insert.jsp(src/main/webapp/WEB-INF/views目录下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

User Id:

Username:

Password:

Nickname:

applicationContext.xml(Spring的Application配置文件在src/main/resources目录下)

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:lang="http://springframework.org/schema/lang" xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

name="derbySqlSessionFactory">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:lang="http://springframework.org/schema/lang" xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

name="derbySqlSessionFactory">

name="derbySqlSessionFactory">

Java文件

UserController.Java(在src/main/java/com.bjpowernode.practice.controller目录下)

package com.bjpowernode.practice.controller;

import com.bjpowernode.practice.User;

import com.bjpowernode.practice.UserMapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc

@Controller

public class UserController

{

@Autowired

private UserMapper userMapper;

/**

*

* 获得所有的User信息

*

* @param model

* @return

*/

@RequestMapping(value = {"/", ""}, method = RequestMethod.GET)

public String getAllUser(Model model)

{

List users = userMapper.getUsers();

System.out.println("Show all user size:" + users.size());

model.addAttribute("users", users);

return "show";

}

/**

*

* INSERT的GET请求,跳转到Insert的View即insert.jsp

*

* @return

*/

@RequestMapping(value = {"/insert", ""}, method = RequestMethod.GET)

public String insertUser()

{

return "insert";

}

/**

*

* INSERT的POST请求,执行插入操作并返回ShowAll页面

*

* @param user

* @return

*/

@RequestMapping(value = {"/insert", ""}, method = RequestMethod.POST)

public String insertUserPOST(User user)

{

userMapper.insertUser(user);

return "redirect:/";

}

/**

*

* UPDATE的GET请求,跳转到update的View即update.jsp

*

* @param id

* @param model

* @return

*/

@RequestMapping(value = {"/update/{id}", ""}, method = RequestMethod.GET)

public String updateUser(@PathVariable String id, Model model)

{

model.addAttribute("user", userMapper.getUser(Integer.valueOf(id)));

return "update";

}

/**

*

* UPDATE的POST请求,执行更新操作并返回ShowAll页面

*

* @param id

* @param user

* @return

*/

@RequestMapping(value = {"/update/{id}", ""}, method = RequestMethod.POST)

public String updateUserPOST(@PathVariable String id, User user)

{

userMapper.updateUser(user);

return "redirect:/";

}

/**

*

* 通过Id删除USER

*

* @param id

* @return

*/

@RequestMapping(value = {"/delete/{id}", ""}, method = RequestMethod.GET)

public String deleteUser(@PathVariable int id)

{

userMapper.deleteUser(id);

return "redirect:/";

}

}

User.java(在src/main/java/com.bjpowernode.practice)

package com.bjpowernode.practice;

/**

*

* User 对象。

*

* @author Freud Kang

*

*/

public class User

{

private Integer id;

private String username;

private String password;

private String nickname;

public Integer getId()

{

return id;

}

public void setId(Integer id)

{

this.id = id;

}

public String getUsername()

{

return username;

}

public void setUsername(String username)

{

this.username = username;

}

public String getPassword()

{

return password;

}

public void setPassword(String password)

{

this.password = password;

}

public String getNickname()

{

return nickname;

}

public void setNickname(String nickname)

{

this.nickname = nickname;

}

}

UserMapper.java(在src/main/java/com.bjpowernode.practice目录下)

package com.bjpowernode.practice;

import java.util.List;

public interface UserMapper

{

/**

*

* 获得所有User

*

* @return

*/

public List getUsers();

/**

*

* 通过Id获得User

*

* @param id

* @return

*/

public User getUser(int id);

/**

*

* 插入User

*

* @param user

*/

public void insertUser(User user);

/**

*

* 更新User

*

* @param user

*/

public void updateUser(User user);

/**

*

* 通过Id删除User

*

* @param userId

*/

public void deleteUser(int userId);

}

UserMapper.xml(mybatis的mapper配置文件,在src/main/java/com.bjpowernode.practice目录下)

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select *

from USER_TEST_TB

select *

from USER_TEST_TB

where ID=#{id}

insert into

USER_TEST_TB

values(#{id},#{username},#{password},#{nickname})

update USER_TEST_TB set

USERNAME = #{username},

PASSWORD = #{password},

NICKNAME = #{nickname}

where ID = #{id}

delete from USER_TEST_TB where ID=#{id}

总结

以上所述是给大家介绍的Mybatis集成Spring的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java远程共享目录的操作代码
下一篇:java 字符串的拼接的实现实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~