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 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 12 /WEB-INF/classes/applicationContext.xml, 13 14 18 19 20 24 25 2 / 19 28 29 30 31 35 36 37 42 43 44 45 46 48 49 50 53 54 58 59 60 61 62 64 3 / 19 66 67 68 69 73 74 78 79 80 81 83 84 89 92 93 96 97
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 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 15 16 18 22 23 26 27
8 applicationContext.xml
1
2 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 21 22 6 / 19 24 25 27 31 32 35 40 41 44 46 49 50 51 54 55 60 61 63 7 / 19 65 67 69 71 79 80 81 82
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 98 * 批量增加 99 * @param user 12 / 19 100 * @return 101 * @throws Exception 102 */ 103 public int batchInsertUser(@Param(\"users\")List 106 * 批量删除 107 * @param list 108 * @return 109 * @throws Exception 110 */ 111 public int batchDeleteUser(@Param(\"list\")List 115 * 分页查询数据 116 * @param parma 117 * @return 118 * @throws Exception 119 */ 120 public List 124 * 分页统计数据 125 * @param parma 126 * @return 127 * @throws Exception 128 */ 129 public int countUser(Map UserMapper.xml 13 / 19 1 2 3 5 6 7 12 13 14 16 22 24 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 index.jsp 点击提交 整体大概就这样了! 18 / 19 本文作者:hellokitty 19 / 19 因篇幅问题不能全部显示,请点此查看更多更全内容