【MyBatis】-----初识【MyBatis】
一、核心配置文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 <environments default="development"> 5 <environment id="development"> 6 <transactionManager type="JDBC" /> 7 <!-- 配置数据库连接信息 --> 8 <dataSource type="POOLED"> 9 <property name="driver" value="com.mysql.jdbc.Driver" /> 10 <property name="url" value="jdbc:mysql:///mybatis" /> 11 <property name="username" value="root" /> 12 <property name="password" value="root" /> 13 </dataSource> 14 </environment> 15 </environments> 16 <!-- 引用映射文件 --> 17 <mappers > 18 <mapper resource="com/myBatis/mapper/userMapper.xml"/> 19 </mappers> 20 </configuration>
二、实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.myBatis.entity; 2 3 public class EUser { 4 private Integer id; 5 private String name; 6 private Integer age; 7 public int getId() { 8 return id; 9 } 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public Integer getAge() { 17 return age; 18 } 19 public void setAge(Integer age) { 20 this.age = age; 21 } 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 @Override 26 public String toString() { 27 return "EUser [id=" + id + ", name=" + name + ", age=" + age + "]"; 28 } 29 30 31 32 }
三、实体类的映射文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 <!-- 4 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 5 --> 6 <mapper namespace="com.myBatis.mapper.userMapper.xml"> 7 8 <!-- 根据id查询得到一个user对象 --> 9 <select id="getUser" parameterType="int" resultType="com.myBatis.entity.EUser"> 10 select * from users where id=#{id} 11 </select> 12 <!-- 根据id查询删除一个user对象 --> 13 <delete id="deleteUser" parameterType="com.myBatis.entity.EUser"> 14 delete from users where id=#{id} 15 </delete> 16 <!-- 创建用户(Create) --> 17 <insert id="addUser" parameterType="com.myBatis.entity.EUser"> 18 insert into users values(null,#{name},#{age}) 19 </insert> 20 <!-- 修改用户(Update) --> 21 <update id="updateUser" parameterType="com.myBatis.entity.EUser"> 22 update users set name=#{name},age=#{age} where id=#{id} 23 </update> 24 25 </mapper>
四、测试方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package myBatisTest; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.Test; 11 12 import com.myBatis.entity.EUser; 13 14 15 public class TestUser { 16 17 18 19 //修改 20 @Test 21 public void testUpdateUser() { 22 try { 23 Reader reader= Resources.getResourceAsReader("SqlMapConfig.xml"); 24 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 25 SqlSession sqlSession= sqlSessionFactory.openSession(true); 26 //先查找 27 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 28 EUser user = sqlSession.selectOne(select, 5); 29 System.out.println(user); 30 //再修改 31 user.setName("改啦2122"); 32 user.setAge(50); 33 String update = "com.myBatis.mapper.userMapper.xml.updateUser"; 34 int i = sqlSession.update(update, user); 35 System.out.println(i); 36 EUser user1= sqlSession.selectOne(select, 5); 37 System.out.println(user1); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 43 44 //添加 45 @Test 46 public void testAddUser() { 47 try { 48 Reader reader= Resources.getResourceAsReader("SqlMapConfig.xml"); 49 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 50 SqlSession sqlSession= sqlSessionFactory.openSession(true); 51 String add = "com.myBatis.mapper.userMapper.xml.addUser"; 52 EUser user = new EUser(); 53 user.setName("用户孤傲苍狼"); 54 user.setAge(20); 55 int i = sqlSession.insert(add, user); 56 System.out.println(i); 57 } catch (IOException e) { 58 e.printStackTrace(); 59 } 60 } 61 62 63 //删除 64 @Test 65 public void testDeleteUser() { 66 try { 67 Reader reader= Resources.getResourceAsReader("SqlMapConfig.xml"); 68 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 69 /** 70 * openSession(true) 手动提交 71 * 数据库中的autocommit针对的是数据库自己以及命令窗口,必须在程序手动提交 72 */ 73 SqlSession sqlSession= sqlSessionFactory.openSession(true); 74 String delete = "com.myBatis.mapper.userMapper.xml.deleteUser"; 75 int i= sqlSession.delete(delete, 4); 76 System.out.println(i); 77 } catch (IOException e) { 78 e.printStackTrace(); 79 } 80 } 81 82 83 //查询 84 @Test 85 public void testGetUser() { 86 87 try { 88 Reader reader= Resources.getResourceAsReader("SqlMapConfig.xml"); 89 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 90 SqlSession sqlSession= sqlSessionFactory.openSession(true); 91 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 92 EUser user = sqlSession.selectOne(select, 1); 93 System.out.println(user); 94 } catch (IOException e) { 95 e.printStackTrace(); 96 } 97 } 98 }
注:
类的映射文件中的<mapper namespace="com.myBatis.mapper.userMapper.xml"> 映射文件后缀名可不写
测试类中的 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 映射文件后缀名可不写
请留下你的足迹...