您的当前位置:首页正文

springmvc+spring+mybatis整合

来源:个人技术集锦


springmvc+spring+mybatis整合

首先在整合这个框架的时候,想想其一般的步骤是怎样的,先有个步骤之后,不至于在后面的搞混了,这样在整合的时候也比较清晰些。

然后我们就细细的一步一步来整合。 1 创建一个Web项目。

2 导入Mybatis3、Spring4、SpringMVC4、连接数据库(我使用的数据库是mysql)的jar包。 我所用的包:

spring-websocket-4.2.0.RELEASE.jar

1 / 19

3 创建Mybatis3、Spring4、SpringMVC4、连接数据库的配置文件。

4 配置web.xml

1 2 3 xmlns=\"http://java.sun.com/xml/ns/javaee\"

4 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 5 xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\"> 7

8 9

10 contextConfigLocation 11

12 /WEB-INF/classes/applicationContext.xml, 13 14 15 16 17

18 19

20 log4jConfigLocation 21 /WEB-INF/log4j.xml 22 23

24

25 log4jRefreshInterval 26 60000 27

2 / 19

28

29 30

31 webAppRootKey 32 ssm.root 33 34

35 36

37 default 38 /static/* 39 40 41

42 43

44 mvc

45 org.springframework.web.servlet.DispatcherServlet

46 48

49 contextConfigLocation

50 /WEB-INF/classes/mvc-servlet.xml 51 52

53

54 mvc 55 / 56 57

58

59 60

61 encodingFilter

62 org.springframework.web.filter.CharacterEncodingFilter 63

64 encoding 65 UTF-8

3 / 19

66

67 68

69 forceEncoding 70 true 71 72

73

74 encodingFilter 75 /* 76 77

78 79

80 loginFilter

81 com.cy.ssm.filter.LoginFilter 82

83

84 loginFilter 85 /* 86 87 88

89 org.springframework.web.context.ContextLoaderListener 90 91

92

93 org.springframework.web.util.Log4jConfigListener 94 95

96

97 index.jsp 98 99

5 datasource.properties 连接数据库 1 jdbc.driver=com.mysql.jdbc.Driver

2 jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8

4 / 19

3 jdbc.username=root 4 jdbc.password=root

6 mybatis.cfg.xml文件

7 mvc-servlet.xml

1

2 5 xsi:schemaLocation=\"http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd 8 http://www.springframework.org/schema/mvc

9 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd\"> 10

11 12 13 14

15

16 17

18 19 20 21

22 23

26

27 28 29 30 31

8 applicationContext.xml

1

2 5 xsi:schemaLocation=\"http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/aop

8 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 9 http://www.springframework.org/schema/tx

10 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 11 http://www.springframework.org/schema/context

12 http://www.springframework.org/schema/context/spring-context-4.2.xsd\"> 13 14

15

16

17 18 19 20

21 22 23

6 / 19

24

25 26

27 classpath:datasource.properties 28 29 30

31

32 33 34

35 36 37 38 39

40

41 42 43

44 45

46 47 48

49

50

51 52 53

54 55 56 class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\"> 57 58 59

60

61 62

63

7 / 19

65

67

69

71 72 73 74 75 76 77 78

79

80

81

82 83 84 85

9 配置文件都差不多配置好了,接下来就写个测试的。

UserBean.java

8 / 19

1 package com.cy.ssm.beans; 2

3 import java.io.Serializable; 4

5 public class UserBean implements Serializable { 6 7

8 private static final long serialVersionUID = -2682305557890221059L; 9 private Integer id; 10 private String username; 11 private String password; 12 private Double account; 13 public UserBean() { 14 super();

15 // TODO Auto-generated constructor stub 16 }

17 public UserBean(Integer id, String username, String password, Double account) { 18 super(); 19 this.id = id;

20 this.username = username; 21 this.password = password; 22 this.account = account; 23 }

24 public Integer getId() { 25 return id; 26 }

27 public void setId(Integer id) { 28 this.id = id; 29 } 30

31 public String getUsername() { 32 return username; 33 }

34 public void setUsername(String username) { 35 this.username = username; 36 }

37 public String getPassword() { 38 return password; 39 }

9 / 19

40 public void setPassword(String password) { 41 this.password = password; 42 }

43 public Double getAccount() { 44 return account; 45 }

46 public void setAccount(Double account) { 47 this.account = account; 48 }

49 @Override

50 public String toString() {

51 return \"UserBean [account=\" + account + \52 + password + \53 } 54 }

UserMapper.java

1 package com.cy.ssm.mapper; 2

3 import java.util.List; 4 import java.util.Map; 5 6

7 import org.apache.ibatis.annotations.Delete; 8 import org.apache.ibatis.annotations.Insert; 9 import org.apache.ibatis.annotations.Options; 10 import org.apache.ibatis.annotations.Param; 11 import org.apache.ibatis.annotations.Result; 12 import org.apache.ibatis.annotations.ResultMap; 13 import org.apache.ibatis.annotations.Results; 14 import org.apache.ibatis.annotations.Select; 15 import org.apache.ibatis.annotations.Update; 16

17 import com.cy.ssm.beans.UserBean; 18

10 / 19

19

20 public interface UserMapper { 21 22 23 /** 24 * 登录

25 * @param userName 26 * @param password 27 * @return

28 * @throws Exception 29 */

30 @Select(\"select * from t_user where username=#{un} and password=#{pw}\") 31 @Results({ 32

33 @Result(id=true,property=\"id\class), 34 @Result(property=\"username\class), 35 @Result(property=\"password\class), 36 @Result(property=\"account\class) 37 })

38 public UserBean login(@Param(\"un\")String username,@Param(\"pw\")String password); 39 /**

40 * 新增用戶 41 * @param user 42 * @return

43 * @throws Exception 44 */

45 @Insert(\"insert into t_user value (null,user.username,user.password,user.account)\") 46 @Options(useGeneratedKeys=true,keyProperty=\"user.id\")

47 public int insertUser(@Param(\"user\")UserBean user) throws Exception; 48 49 50 /**

51 * 修改用戶 52 * @param user 53 * @param id 54 * @return

55 * @throws Exception 56 */

57 @Update(\" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}\")

58 public int updateUser (@Param(\"u\")UserBean user,@Param(\"id\")int id) throws Exception;

11 / 19

59 60 /**

61 * 刪除用戶 62 * @param id 63 * @return

64 * @throws Exception 65 */

66 @Delete(\" delete from t_user where id=#{id} \") 67 public int deleteUser(int id) throws Exception; 68 69 70 /**

71 * 根据id查询用户信息 72 * @param id 73 * @return

74 * @throws Exception 75 */ 76

77 @Select(\" select * from t_user where id=#{id}\") 78 @Results({ 79

80 @Result(id=true,property=\"id\class), 81 @Result(property=\"username\class), 82 @Result(property=\"password\class), 83 @Result(property=\"account\class) 84 })

85 public UserBean selectUserById(int id) throws Exception; 86 /**

87 * 查询所有的用户信息 88 * @return

89 * @throws Exception 90 */ 91

92 @Select(\" select * from t_user\") 93 @ResultMap(\"userMap\")

94 public List selectAllUser() throws Exception; 95 96 97 /**

98 * 批量增加 99 * @param user

12 / 19

100 * @return

101 * @throws Exception 102 */

103 public int batchInsertUser(@Param(\"users\")List user) throws Exception; 104 105 /**

106 * 批量删除 107 * @param list 108 * @return

109 * @throws Exception 110 */

111 public int batchDeleteUser(@Param(\"list\")List list) throws Exception; 112 113 114 /**

115 * 分页查询数据 116 * @param parma 117 * @return

118 * @throws Exception 119 */

120 public List pagerUser(Map parmas) throws Exception; 121 122 /** 123 *

124 * 分页统计数据 125 * @param parma 126 * @return

127 * @throws Exception 128 */

129 public int countUser(Map parmas) throws Exception; 130 131 132 133 134 }

UserMapper.xml

13 / 19

1

2

3 4

5

6

7 8 9 10 11

12 13

14 15 insert into t_user values

16 19 20 21

22 23 delete from t_user where id in (

24 27 ) 28 29

30 31

32 33

34

37 44

45 51 52

ILoginService.java

1 package com.cy.ssm.service; 2 3

4 import com.cy.ssm.beans.UserBean; 5

6 public interface ILoginService { 7

8 public UserBean Login(String username,String password); 9 10 11 }

LoginServiceImpl.java

15 / 19

1 package com.cy.ssm.service.impl; 2

3 import javax.annotation.Resource; 4

5 import org.springframework.stereotype.Service; 6

7 import com.cy.ssm.mapper.UserMapper; 8 import com.cy.ssm.beans.UserBean; 9 import com.cy.ssm.service.ILoginService; 10 @Service

11 public class LoginServiceImpl implements ILoginService{ 12

13 @Resource

14 private UserMapper um; 15 16

17 @Override

18 public UserBean Login(String username, String password) { 19 return um.login(username, password); 20 } 21 22 }

LoginController .java

1 package com.cy.ssm.controller; 2 3

4 import javax.annotation.Resource;

5 import javax.servlet.http.HttpServletRequest; 6

7 import org.apache.log4j.Logger;

8 import org.springframework.stereotype.Controller;

9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.servlet.ModelAndView; 11

16 / 19

12 import com.cy.ssm.beans.UserBean; 13 import com.cy.ssm.service.ILoginService; 14 15 16

17 @Controller

18 public class LoginController {

19 private Logger log = Logger.getLogger(this.getClass()); 20

21 @Resource

22 private ILoginService loginServiceImpl; 23

24 @RequestMapping(\"/login\")

25 public ModelAndView login(HttpServletRequest req,UserBean user){ 26 log.info(user); 27

28 ModelAndView mv = new ModelAndView();

29 UserBean u=loginServiceImpl.Login(user.getUsername(), user.getPassword()); 30

31 if(u != null){ 32

33 req.getSession().setAttribute(\"user\34 mv.addObject(\"password\35 System.out.println(u.getPassword()); 36 }

37 mv.setViewName(\"index\"); 38 return mv; 39 } 40 41 42 43 }

jsp页面; login.jsp 1

2

login\" method=\"post\"> 3

17 / 19

4 5 6

index.jsp ${password } 测试:

点击提交

整体大概就这样了!

18 / 19

本文作者:hellokitty

19 / 19

因篇幅问题不能全部显示,请点此查看更多更全内容