一、核心配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 最开始加载引用db.properties文件 --> 6 <properties resource="db.properties"/> 7 <!-- 取别名 以下情况别名为类名 --> 8 <typeAliases> 9 <package name="com.myBatis.entity"/> 10 </typeAliases> 11 <!-- 12 development : 开发模式 13 work : 工作模式 14 --> 15 <environments default="development"> 16 <environment id="development"> 17 <transactionManager type="JDBC" /> 18 <!-- 配置数据库连接信息 --> 19 <dataSource type="POOLED"> 20 <property name="driver" value="${driver}" /> 21 <property name="url" value="${url}" /> 22 <property name="username" value="${username}" /> 23 <property name="password" value="${password}" /> 24 </dataSource> 25 </environment> 26 </environments> 27 <!-- 引用映射文件 --> 28 <mappers > 29 <mapper resource="com/myBatis/mapper/classesMapper.xml"/> 30 </mappers> 31 </configuration>
二、实体类
2.1Teacher
1 package com.myBatis.entity; 2 3 public class Teacher { 4 // 定义实体类的属性,与teacher表中的字段对应 5 private int id; // id===>t_id 6 private String name; // name===>t_name 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 @Override 20 public String toString() { 21 return "Teacher [id=" + id + ", name=" + name + "]"; 22 } 23 24 25 }
2.2Classes
1 package com.myBatis.entity; 2 3 public class Classes { 4 private int id; // id===>c_id 5 private String name; // name===>c_name 6 /** 7 * class表中有一个teacher_id字段,所以在Classes类中定义一个teacher属性, 8 * 用于维护teacher和class之间的一对一关系,通过这个teacher属性就可以知道这个班级是由哪个老师负责的 9 */ 10 private Teacher teacher; 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public Teacher getTeacher() { 24 return teacher; 25 } 26 public void setTeacher(Teacher teacher) { 27 this.teacher = teacher; 28 } 29 @Override 30 public String toString() { 31 return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + "]"; 32 } 33 34 }
三、映射文件
teacherMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 5 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名, 6 这样就能够保证namespace的值是唯一的 7 --> 8 <mapper namespace="com.myBatis.mapper.teacherMapper"> 9 <!-- 根据id查询得到一个user对象 --> 10 <select id="getteacher" parameterType="int" resultType="Teacher"> 11 select * from teacher where id=#{id} 12 </select> 13 14 </mapper> 15
classesMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 5 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名, 6 这样就能够保证namespace的值是唯一的 7 --> 8 <mapper namespace="com.myBatis.mapper.classesMapper"> 9 <!-- 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 10 封装联表查询的数据(去除重复的数据) 11 根据id查询得到一个user对象 --> 12 <select id="getclassess" parameterType="int" resultMap="ClassResultMap"> 13 select * from classes c, teacher t where c.t_id=t.t_id and c.c_id=#{id} 14 </select> 15 <!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> 16 <resultMap type="Classes" id="ClassResultMap"> 17 <id property="id" column="c_id"/> 18 <result property="name" column="c_name"/> 19 <association property="teacher" javaType="Teacher"> 20 <id property="id" column="t_id"/> 21 <result property="name" column="t_name"/> 22 </association> 23 </resultMap> 24 25 </mapper> 26
四、测试类
1 package myBatisTest; 2 import org.apache.ibatis.session.SqlSession; 3 import org.junit.Test; 4 5 import com.myBatis.entity.Classes; 6 import com.myBatis.util.myBtaisUtil; 7 8 public class TestUser { 9 /** 10 * 11 //修改 12 @Test 13 public void testUpdateUser() { 14 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 15 //先查找 16 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 17 EUser user = sqlSession.selectOne(select, 7); 18 System.out.println(user); 19 //再修改 20 user.setName("改啦7777"); 21 user.setAge(50); 22 String update = "com.myBatis.mapper.userMapper.xml.updateUser"; 23 int i = sqlSession.update(update, user); 24 System.out.println(i); 25 EUser user1= sqlSession.selectOne(select, 7); 26 System.out.println(user1); 27 } 28 29 30 //添加 31 @Test 32 public void testAddUser() { 33 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 34 String add = "com.myBatis.mapper.userMapper.xml.addUser"; 35 EUser user = new EUser(); 36 user.setName("用户孤傲苍狼"); 37 user.setAge(20); 38 int i = sqlSession.insert(add, user); 39 System.out.println(i); 40 } 41 //删除 42 @Test 43 public void testDeleteUser() { 44 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 45 String delete = "com.myBatis.mapper.userMapper.xml.deleteUser"; 46 int i= sqlSession.delete(delete, 6); 47 System.out.println(i); 48 } 49 */ 50 //查询 51 @Test 52 public void testGetUser() { 53 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 54 String select = "com.myBatis.mapper.classesMapper.getclassess"; 55 Classes classes = sqlSession.selectOne(select, 1); 56 System.out.println(classes); 57 } 58 }
时间: 2024-10-07 22:38:17