博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis3+Spring4+SpringMVC4 整合
阅读量:6884 次
发布时间:2019-06-27

本文共 13089 字,大约阅读时间需要 43 分钟。

 

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

然后我们就细细的一步一步来整合。

1  创建一个Web项目。

    

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

 我所用的包:

  

 spring-websocket-4.2.0.RELEASE.jar

 

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

    

 

 

4  配置web.xml 

 

1 
2
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
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
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.Driver2 jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-83 jdbc.username=root4 jdbc.password=root

 

6 mybatis.cfg.xml文件

 

7  mvc-servlet.xml

1 
2
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
13 14 15
16
17
18
19
20 21
22
23 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
57
58
59 60
61
62
63
65
67
69
71
72
73
74
75
76
77
78 79
80
81
82
83
84 85

 

 

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

 

 UserBean.java

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 stub16     }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     }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     @Override50     public String toString() {51         return "UserBean [account=" + account + ", id=" + id + ", password="52                 + password + ", username=" + username + "]";53     }54 }
View Code

 

 

 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  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",column="id",javaType=Integer.class), 34         @Result(property="username",column="username",javaType=String.class), 35         @Result(property="password",column="password",javaType=String.class), 36         @Result(property="account",column="account",javaType=Double.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; 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",column="id",javaType=Integer.class), 81         @Result(property="username",column="username",javaType=String.class), 82         @Result(property="password",column="password",javaType=String.class), 83         @Result(property="account",column="account",javaType=Double.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 user100 * @return101 * @throws Exception102 */103 public int batchInsertUser(@Param("users")List
user) throws Exception;104 105 /**106 * 批量删除107 * @param list108 * @return109 * @throws Exception110 */111 public int batchDeleteUser(@Param("list")List
list) throws Exception;112 113 114 /**115 * 分页查询数据116 * @param parma117 * @return118 * @throws Exception119 */120 public List
pagerUser(Map
parmas) throws Exception;121 122 /**123 * 124 * 分页统计数据125 * @param parma126 * @return127 * @throws Exception128 */129 public int countUser(Map
parmas) throws Exception;130 131 132 133 134 }
View Code

 

 

UserMapper.xml

1 
2 3
4
5
6
7
8
9
10
11 12
13 14
15 insert into t_user values 16
17 (null,#{users.username},#{users.password},#{users.account})18
19
20 21 22
23 delete from t_user where id in (24
25 #{id}26
27 )28
29 30
31
32
33 34
37
44 45
51 52
View Code

 

 

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 }
View Code

 

 

LoginServiceImpl.java

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 @Service11 public class LoginServiceImpl implements ILoginService{12     13     @Resource14     private UserMapper um;15 16 17     @Override18     public UserBean Login(String username, String password) {19         return um.login(username, password);20     }21 22 }
View Code

 

 

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 12 import com.cy.ssm.beans.UserBean;13 import com.cy.ssm.service.ILoginService;14 15 16 17 @Controller18 public class LoginController {19     private Logger log = Logger.getLogger(this.getClass());20     21     @Resource22     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", u);34             mv.addObject("password", u.getPassword());35             System.out.println(u.getPassword());36         }37         mv.setViewName("index");38         return mv;39     }40     41     42     43 }

 

 jsp页面;

login.jsp

1  2     
3
4
5
6

 

 index.jsp

<body>

   ${password }
</body>

 

测试:

点击提交

整体大概就这样了!

 

我把整个文件上传到:

继续我的作业了!

 

转载地址:http://zunbl.baihongyu.com/

你可能感兴趣的文章
利用H5的css3制作动画
查看>>
Android View 事件分发源码分析
查看>>
vue 2.0 - props
查看>>
RustCon Asia 实录 | Rust 在国内某视频网站的应用
查看>>
Vue遇上Analytics
查看>>
mysql
查看>>
修改max_allowed_packet(允许执行的sql最大长度)
查看>>
node js 处理时间分析
查看>>
判断数据库、表和字段是否存在
查看>>
新手安装postgreSQL后无法连接服务器
查看>>
递归和动态规划
查看>>
java实现简单的控制台管理系统
查看>>
建造模式
查看>>
深入理解 intent (1)
查看>>
将导航栏始终固定在窗口顶部:
查看>>
手机免流量,还会是天方夜谭吗?
查看>>
find命令
查看>>
Java 多线程(四)——线程同步(synchronized、ReentrantLock)
查看>>
遇到Could not load file or assembly ... or one of its dependencies怎么办
查看>>
TCP 上传文件
查看>>