Mybatis最入门---基本增改删查(CRUD)

来源:互联网 发布:淘宝物流重量什么意思 编辑:程序博客网 时间:2024/05/11 01:57

使用前面的文章解决了Mybatis的配置问题后,本文我们正式来介绍使用Mybatis来开发,从最简单的增改删查开始,后面我们会逐步介绍Mybatis的更多用法,敬请期待!

惯例,先来看看我们的准备工作有:

a.操作系统 :win7 x64

b.基本软件:MySQL,Mybatis,SQLyog

-------------------------------------------------------------------------------------------------------------------------------------

1.创建Mybatis02工程,工程结构图如下:


2.pom文件内容如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.java.mybatis</groupId>  
  6.     <artifactId>mybatis03</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>mybatis01</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>4.12</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.mybatis</groupId>  
  26.             <artifactId>mybatis</artifactId>  
  27.             <version>3.3.1</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>mysql</groupId>  
  31.             <artifactId>mysql-connector-java</artifactId>  
  32.             <version>5.1.26</version>  
  33.         </dependency>  
  34.         <dependency>  
  35.             <groupId>log4j</groupId>  
  36.             <artifactId>log4j</artifactId>  
  37.             <version>1.2.17</version>  
  38.         </dependency>  
  39.     </dependencies>  
  40. </project>  

-------------------------------------------------------------------------------------------------------------------------------------

【鉴于篇幅的关系,下文中确实的其他文件请参照前文HelloWorld工程】

一.查询工程

1.修改UserMapper.xml文件,具体内容如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.csdn.ingo.dao.UserDao">  
  6.     <select id="findUserById" parameterType="String" resultType="User">  
  7.         select * from sysuser where id=#{id}  
  8.     </select>  
  9. </mapper>   
2.修改UserDao文件,具体内容如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.csdn.ingo.dao;  
  2.   
  3. import com.csdn.ingo.entity.User;  
  4.   
  5. /** 
  6. *@author 作者 E-mail:ingo 
  7. *@version 创建时间:2016年4月17日下午6:26:40 
  8. *类说明 
  9. */  
  10. public interface UserDao {  
  11.     User findUserById(String id);  
  12. }  

3.创建单元测试类UserTest.java,具体内容如下
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.csdn.ingo.main;  
  2. import static org.junit.Assert.*;  
  3. import org.apache.ibatis.session.SqlSession;  
  4. import org.apache.log4j.Logger;  
  5. import org.junit.After;  
  6. import org.junit.Before;  
  7. import org.junit.Test;  
  8.   
  9. import com.csdn.ingo.dao.UserDao;  
  10. import com.csdn.ingo.entity.User;  
  11. import com.csdn.ingo.util.SqlSessionFactoryUtil;  
  12.   
  13. /** 
  14. *@author 作者 E-mail:ingo 
  15. *@version 创建时间:2016年4月18日下午5:14:16 
  16. *类说明 
  17. */  
  18. public class UserTest {  
  19.     private static Logger log = Logger.getLogger(UserTest.class);  
  20.       
  21.     private SqlSession sqlSession = null;  
  22.       
  23.     private UserDao userDao = null;  
  24.     @Before  
  25.     public void setUp() throws Exception {  
  26.         log.info("方法执行前调用");  
  27.         sqlSession = SqlSessionFactoryUtil.openSession();  
  28.     }  
  29.   
  30.     @After  
  31.     public void tearDown() throws Exception {  
  32.         log.info("方法执行后调用");  
  33.         sqlSession.close();  
  34.     }  
  35.   
  36.     @Test  
  37.     public void testSelet() {  
  38.         UserDao userDao = sqlSession.getMapper(UserDao.class);  
  39.         String id = "admin";  
  40.         User curUser = userDao.findUserById(id);  
  41.         if(curUser!=null){  
  42.             log.info("成功找到用户:"+curUser.getId());  
  43.         }  
  44.     }  
  45. }  
4.测试方法:在testUpdate()方法上,右键--->run as --->junit test即可。

如果发生错误,请仔细对照上文。

--------------------------------------------------------------------------------------------------------------------------------------------------------

二。修改功能

1.在UserMapper.xml文件中增加update语句,具体内容如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <update id="updateUser" parameterType="User">  
  2.     update sysuser set password=#{password} where id=#{id}  
  3. </update>  
2.在UserDao中增加update方法,具体内容:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface UserDao {  
  2.     User findUserById(String id);  
  3.     int updateUser(User newUser);  
  4. }  
3.创建修改单元测试用例,具体内容如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2. public void testUpdate() {  
  3.     UserDao userDao = sqlSession.getMapper(UserDao.class);  
  4.     User curUser =  new User("admin","changed");  
  5.     int re = userDao.updateUser(curUser);  
  6.     sqlSession.commit();  
  7.     if(re==1){  
  8.         log.info("UpdateUser:"+curUser.getId());  
  9.     }  
  10. }  

4.测试方法:在testUpdate()方法上,右键--->run as --->junit test即可。

如果发生错误,请仔细对照上文。

--------------------------------------------------------------------------------------------------------------------------------------------------------

三。插入功能

1.在UserMapper.xml文件中增加insert语句,具体内容如下:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <insert id="insertUser" parameterType="User" >  
  2.     insert into sysuser (Id,password) values(#{id},#{password})  
  3. </insert>  
2.在UserDao中增加insert方法,具体内容:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface UserDao {  
  2.     User findUserById(String id);  
  3.   
  4.     int insertUser(User newUser);  
  5.       
  6.     int updateUser(User newUser);  
  7. }  
3.创建插入单元测试用例,具体内容如下:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2. public void testInsert() {  
  3.     UserDao userDao = sqlSession.getMapper(UserDao.class);  
  4.     User newUser =  new User("customer222","customer222");  
  5.     int re = userDao.insertUser(newUser);  
  6.     sqlSession.commit();  
  7.     if(re==1){  
  8.         log.info("insertUser:"+newUser.getId());  
  9.     }  
  10. }  

4.测试方法:在testUpdate()方法上,右键--->run as --->junit test即可。

如果发生错误,请仔细对照上文。

--------------------------------------------------------------------------------------------------------------------------------------------------------

四。删除功能

1.在UserMapper.xml文件中增加delete语句,具体内容如下:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <delete id="deleteUser" parameterType="String">  
  2.     delete from sysuser where id=#{id}  
  3. </delete>  
2.在UserDao中增加delete方法,具体内容:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface UserDao {  
  2.     User findUserById(String id);  
  3.   
  4.     int insertUser(User newUser);  
  5.       
  6.     int updateUser(User newUser);  
  7.       
  8.     int deleteUser(String id);  
  9. }  
3.创建修改单元测试用例,具体内容如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2. public void testDelete() {  
  3.     UserDao userDao = sqlSession.getMapper(UserDao.class);  
  4.     String id = "customer";  
  5.     int re = userDao.deleteUser(id);  
  6.     sqlSession.commit();  
  7.     if(re==1){  
  8.         log.info("deleteUser:"+id);  
  9.     }  
  10. }  

4.测试方法:在testUpdate()方法上,右键--->run as --->junit test即可。

如果发生错误,请仔细对照上文。

-------------------------------------------------------------------------------------------------------------------------------------

至此,Mybatis最入门---基本增改删查(CRUD)结束
0 0
原创粉丝点击