标签:dom select inter jdb 查询 映射 报名 catch com
userMapper.xml映射文件如下:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!--namespace一般命名成报名+映射名--> 4 <mapper namespace="me.gacl.mapping.userMapper"> 5 <!--根据id查询--> 6 <select id="getUser" parameterType="int" resultType="me.gacl.domain.User"> 7 select * from users where id=#{id} 8 </select> 9 <!--插入一条记录(Create)--> 10 <insert id="addUser" parameterType="me.gacl.domain.User"> 11 insert into users(name ,age) values(#{name},#{age}) 12 </insert> 13 <!--删除一条记录(Drop)--> 14 <delete id="deleteUser" parameterType="int"> 15 delete from users where id=#{id} 16 </delete> 17 <!--更新(Update)--> 18 <update id="updateUser" parameterType="me.gacl.domain.User"> 19 update users set NAME=#{name},age=#{age} where id=#{id} 20 </update> 21 <!--查询全部记录(Retrieve)--> 22 <select id="getAllUser" resultType="me.gacl.domain.User"> 23 select * from users 24 </select> 25 </mapper>
测试类代码如下
1 package me.gacl.domain; 2 3 import me.gacl.Util.MyBatisUtil; 4 import org.apache.ibatis.session.SqlSession; 5 import java.util.List; 6 /** 7 * Created by hu on 2017/8/26. 8 */ 9 public class TestCRUDByXMLMapper { 10 public void testAdd(){ 11 //参数为true,表示事务自动提交,参数为false则需要手动提交 12 SqlSession sqlSession = MyBatisUtil.getSqlSession(true); 13 //SqlSession sqlSession = MyBatisUtil.getSqlSession(false); 14 //要插入的user对象 15 User user = new User(); 16 user.setName("芝麻糊"); 17 user.setAge(17); 18 //statement是映射sql的标识字符串,me.gacl.mapping.userMapper是userMapper.xml中mapper标签的namespace属性, 19 // addUser是insert标签的id,通过insert标签的id值可以找到要执行的sql语句 20 String statement = "me.gacl.mapping.userMapper.addUser"; 21 //执行插入操作 22 int resultAdd = sqlSession.insert(statement,user); 23 //手动提交 24 //sqlSession.commit(); 25 //使用sqlSession执行完后需要关闭sqlSession 26 sqlSession.close(); 27 System.out.println(resultAdd); 28 } 29 public void testUpdate(){ 30 SqlSession sqlSession = MyBatisUtil.getSqlSession(true); 31 //要更新的user对象 32 User user = new User(); 33 user.setName("胡图图"); 34 user.setAge(18); 35 user.setId(3); 36 //statement是映射sql的标识字符串,me.gacl.mapping.userMapper是userMapper.xml中mapper标签的namespace属性, 37 //updateUser是update标签的id,通过update标签的id值可以找到要执行的sql语句 38 String statement = "me.gacl.mapping.userMapper.updateUser"; 39 //执行更新操作 40 int resultAdd = sqlSession.update(statement,user); 41 //使用sqlSession执行完后需要关闭sqlSession 42 sqlSession.close(); 43 System.out.println(resultAdd); 44 } 45 public void testDelete(){ 46 SqlSession sqlSession = MyBatisUtil.getSqlSession(true); 47 //statement是映射sql的标识字符串,me.gacl.mapping.userMapper是userMapper.xml中mapper标签的namespace属性, 48 // deleteUser是delete标签的id,通过delete标签的id值可以找到要执行的sql语句 49 String statement = "me.gacl.mapping.userMapper.deleteUser"; 50 //执行删除操作 51 int resultAdd = sqlSession.delete(statement,2); 52 //使用sqlSession执行完后需要关闭sqlSession 53 sqlSession.close(); 54 System.out.println(resultAdd); 55 } 56 public void testGetAll(){ 57 SqlSession sqlSession = MyBatisUtil.getSqlSession(true); 58 //statement是映射sql的标识字符串,me.gacl.mapping.userMapper是userMapper.xml中mapper标签的namespace属性, 59 // getAllUser是select标签的id,通select标签的id值可以找到要执行的sql语句 60 String statement = "me.gacl.mapping.userMapper.getAllUser"; 61 //执行查询全部操作 62 List <User> list = sqlSession.selectList(statement); 63 //使用sqlSession执行完后需要关闭sqlSession 64 sqlSession.close(); 65 System.out.println(list); 66 } 67 68 public static void main(String[] args) { 69 TestCRUDByXMLMapper testCRUDByXMLMapper = new TestCRUDByXMLMapper(); 70 testCRUDByXMLMapper.testAdd(); 71 // testCRUDByXMLMapper.testUpdate(); 72 // testCRUDByXMLMapper.testDelete(); 73 // testCRUDByXMLMapper.testGetAll(); 74 } 75 }
在addTest插入数据库过程中出现中文乱码情况,将mybatis-config.xml中的链接URL加上【?characterEncoding=UTF-8】,则中文正常显示
执行了三次插入操作,如下图有两条乱码记录,最后一条是改了之后的
执行更新操作,结果如下图
执行删除操作,可以看到id=2的记录不见了
执行查询全部操作
1.定义sql映射的接口UserMapperI
1 package me.gacl.map; 2 3 import me.gacl.domain.User; 4 import org.apache.ibatis.annotations.Delete; 5 import org.apache.ibatis.annotations.Insert; 6 import org.apache.ibatis.annotations.Select; 7 import org.apache.ibatis.annotations.Update; 8 9 import java.util.List; 10 11 /** 12 * 定义sql映射的接口,使用注解指明方法执行的SQL. 13 */ 14 public interface UserMapperI { 15 //使用@Insert注解指明add方法要执行的SQL 16 @Insert("insert into users(name,age) values(#{name},#{age})") 17 public int add(User user); 18 //使用@Update注解指明update方法要执行的SQL 19 @Update("update users set(name=#{name},age=#{age})") 20 public int update(User user); 21 //使用@Delete注解指明delete方法要执行的SQL 22 @Delete("delete from users where id=#{id}") 23 public int delete(int id); 24 //使用@Select注解指明getAll方法要执行的SQL 25 @Select("select * from users") 26 public List<User> getAll(); 27 }
需要说明的是,我们不需要针对UserMapperI接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。
2.然后在mybatis-config.xml中注册这个映射接口
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 <!--配置数据库环境,可以有多个environment,default值是其中一个environment的id值--> 5 <environments default="developement"> 6 <environment id="developement"> 7 <!--配置事务管理器,JDBC和MANAGED两种,JDBC可以进行回滚和提交--> 8 <transactionManager type="JDBC"></transactionManager> 9 <!--配置数据源,type=[UNPOOLED | POOLED| JNDI], POOLED表示实现简单的数据池连接,连接可以复用--> 10 <dataSource type="POOLED"> 11 <property name="driver" value="com.mysql.jdbc.Driver"></property> 12 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8"></property> 13 <property name="username" value="root"></property> 14 <property name="password" value="123456"></property> 15 </dataSource> 16 </environment> 17 </environments> 18 <!--resource值是映射文件所在的路径--> 19 <!--class值是映射接口类所在的路径--> 20 <mappers> 21 <mapper resource="mapping/userMapper.xml"></mapper> 22 <mapper class="me.gacl.map.UserMapperI"></mapper> 23 </mappers> 24 </configuration>
3.编写测试类,代码如下:
package me.gacl.domain; import me.gacl.Util.MyBatisUtil; import me.gacl.map.UserMapperI; import org.apache.ibatis.session.SqlSession; import java.util.List; /** * Created by huyanxia on 2017/8/26. */ public class TestCRUDByAnnotation { public void testAdd(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = sqlSession.getMapper(UserMapperI.class); //要插入的user对象 User user = new User(); user.setName("我是注解"); user.setAge(12); //执行插入操作 int resultAdd = userMapperI.add(user); //手动提交 //sqlSession.commit(); //使用sqlSession执行完后需要关闭sqlSession sqlSession.close(); System.out.println(resultAdd); } public void testUpdate(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = sqlSession.getMapper(UserMapperI.class); //要更新的user对象 User user = new User(); user.setName("劳斯莱斯了"); user.setAge(2); user.setId(5); //执行更新操作 int resultAdd = userMapperI.update(user); //使用sqlSession执行完后需要关闭sqlSession sqlSession.close(); System.out.println(resultAdd); } public void testDelete(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = sqlSession.getMapper(UserMapperI.class); //执行删除操作 int resultAdd = userMapperI.delete(4); //使用sqlSession执行完后需要关闭sqlSession sqlSession.close(); System.out.println(resultAdd); } public void testGetAll(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = sqlSession.getMapper(UserMapperI.class); //执行查询全部操作 List<User> list = userMapperI.getAll(); //使用sqlSession执行完后需要关闭sqlSession sqlSession.close(); System.out.println(list); } public static void main(String[] args) { TestCRUDByAnnotation testCRUDByAnnotation = new TestCRUDByAnnotation(); testCRUDByAnnotation.testAdd(); //testCRUDByAnnotation.testUpdate(); //testCRUDByAnnotation.testDelete(); //testCRUDByAnnotation.testGetAll(); } }
执行结果如下:
插入操作
更新操作
删除操作
查询全部
4.其中用到的MybatisUtil类是自己编写的,用来获取SqlSession
1 package me.gacl.Util; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 8 import java.io.IOException; 9 import java.io.Reader; 10 11 /** 12 * 获取SqlSession 13 */ 14 public class MyBatisUtil { 15 public static SqlSession getSqlSession(boolean autoCommit){ 16 String resource = "mybatis-config.xml"; 17 try{ 18 Reader reader = Resources.getResourceAsReader(resource); 19 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 20 SqlSession sqlSession = sqlSessionFactory.openSession(autoCommit); 21 return sqlSession; 22 }catch (IOException e){ 23 e.printStackTrace(); 24 } 25 return null; 26 } 27 }
标签:dom select inter jdb 查询 映射 报名 catch com
原文地址:http://www.cnblogs.com/zhima-hu/p/7440340.html