标签:ibatis efault word nfa character val parameter cto com
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8 db.username=root db.password=123456
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!-- 加载java的配置文件 --> 8 <properties resource="config/db.properties"/> 9 10 <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 --> 11 <environments default="development"> 12 <environment id="development"> 13 <!-- 配置JDBC事务控制,由mybatis进行管理 --> 14 <transactionManager type="JDBC"></transactionManager> 15 <!-- 配置数据源,采用mybatis连接池 --> 16 <dataSource type="POOLED"> 17 <property name="driver" value="${db.driver}" /> 18 <property name="url" value="${db.url}" /> 19 <property name="username" value="${db.username}" /> 20 <property name="password" value="${db.password}" /> 21 </dataSource> 22 </environment> 23 </environments> 24 25 <!-- 加载映射文件 --> 26 <mappers> 27 <mapper resource="com/xiaostudy/oneTOmany/mapper.xml" /> 28 </mappers> 29 30 </configuration>
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.xiaostudy.oneTOmany.Mapper"> 6 7 <!-- ========================================================================================== --> 8 <!-- 一对多之resultMap1 --> 9 <select id="findOfTeacher" resultMap="OfTeacher"> 10 select * from t_student s, t_teacher t where s.sid=t.tid and t.tid=#{id} 11 </select> 12 <resultMap type="com.xiaostudy.oneTOmany.Teacher" id="OfTeacher"> 13 <id column="tid" property="id"/> 14 <result column="tname" property="name"/> 15 <collection property="student" ofType="com.xiaostudy.oneTOmany.Student" ><!-- javaType="HashSet" --> 16 <id column="sid" property="sid"></id> 17 <result column="sname" property="sname"/> 18 </collection> 19 </resultMap> 20 <!-- ========================================================================================== --> 21 22 <!-- ========================================================================================== --> 23 <!-- 一对多之resultMap2 --> 24 <select id="findOfTeachers" resultMap="OfTeachers"> 25 select * from t_student2 s, t_teacher t where s.sid=t.tid and t.tid=#{id} 26 </select> 27 <resultMap type="com.xiaostudy.oneTOmany.Teacher" id="OfTeachers"> 28 <id column="tid" property="id"/> 29 <result column="tname" property="name"/> 30 <collection property="student" ofType="com.xiaostudy.oneTOmany.Student" select="getStudents" column="sid"/> 31 </resultMap> 32 33 <select id="getStudents" parameterType="int" resultType="com.xiaostudy.oneTOmany.Student"> 34 select * from t_student2 s where s.sid = #{id} 35 </select> 36 <!-- ========================================================================================== --> 37 38 <!-- ========================================================================================== --> 39 <!-- 一对多之resultMap3 --> 40 <select id="findOfTeachers2" resultMap="OfTeachers2"> 41 select * from t_student2 s, t_teacher t where s.sid=t.tid and t.tid=#{tid} 42 </select> 43 <resultMap type="com.xiaostudy.oneTOmany.Teacher" id="OfTeachers2"> 44 <id column="tid" property="id"/> 45 <result column="tname" property="name"/> 46 <collection property="student" ofType="com.xiaostudy.oneTOmany.Student" select="getStudents2" column="id"/> 47 </resultMap> 48 49 <select id="getStudents2" resultType="com.xiaostudy.oneTOmany.Student"> 50 select * from t_student2 s where s.sid = #{sid} 51 </select> 52 <!-- ========================================================================================== --> 53 </mapper>
1 package com.xiaostudy.oneTOmany; 2 3 public class Student { 4 private int sid; 5 private String sname; 6 private Teacher teacher; 7 8 public int getSid() { 9 return sid; 10 } 11 12 public void setSid(int sid) { 13 this.sid = sid; 14 } 15 16 public String getSname() { 17 return sname; 18 } 19 20 public void setSname(String sname) { 21 this.sname = sname; 22 } 23 24 public Teacher getTeacher() { 25 return teacher; 26 } 27 28 public void setTeacher(Teacher teacher) { 29 this.teacher = teacher; 30 } 31 32 @Override 33 public String toString() { 34 return "Student [sid=" + sid + ", sname=" + sname + ", teacher=" + teacher + "]"; 35 } 36 37 }
1 package com.xiaostudy.oneTOmany; 2 3 import java.util.Set; 4 5 public class Teacher { 6 private int id; 7 private String name; 8 private Set<Student> student; 9 10 public Set<Student> getStudent() { 11 return student; 12 } 13 14 public void setStudent(Set<Student> student) { 15 this.student = student; 16 } 17 18 public int getId() { 19 return id; 20 } 21 22 public void setId(int id) { 23 this.id = id; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 @Override 35 public String toString() { 36 return "Teacher [id=" + id + ", name=" + name + ", student=" + student + "]"; 37 } 38 39 }
1 package com.xiaostudy.oneTOmany; 2 3 import java.util.List; 4 5 public interface Mapper { 6 7 // 一对多之resultMap1 8 public List<Teacher> findOfTeacher(int id); 9 10 // 一对多之resultMap2 11 public List<Teacher> findOfTeachers(int id); 12 13 // 一对多之resultMap3 14 public Teacher findOfTeachers2(int id); 15 16 }
1 package com.xiaostudy.oneTOmany; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession; 10 import org.apache.ibatis.session.SqlSessionFactory; 11 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 12 import com.xiaostudy.oneTOmany.Teacher; 13 14 /** 15 * @desc 测试类 16 * @author xiaostudy 17 * 18 */ 19 public class MybatisTest { 20 21 public static void main(String[] args) throws IOException { 22 String resource = "config/SqlMapConfig.xml"; 23 InputStream inputStream = Resources.getResourceAsStream(resource); 24 25 // 创建SqlSessionFactory 26 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 27 28 // 创建SqlSession 29 SqlSession sqlSession = sqlSessionFactory.openSession(); 30 31 // 获取一个代理dao实现 32 Mapper mapper = sqlSession.getMapper(Mapper.class); 33 34 //一对多之resultMap1 35 // List<Teacher> list = mapper.findOfTeacher(3); 36 37 //一对多之resultMap2 38 List<Teacher> list = mapper.findOfTeachers(2); 39 /*Teacher teacher = list.get(0); 40 System.out.println(teacher.getId()); 41 System.out.println(teacher.getName()); 42 Set<Student> student = teacher.getStudent(); 43 for(Student s : student) { 44 System.out.println(s); 45 }*/ 46 //一对多之resultMap3 47 // Teacher t = mapper.findOfTeachers2(2); 48 49 for(Teacher t : list) { 50 System.out.println(t); 51 } 52 53 // System.out.println(list.get(0)); 54 // System.out.println(t); 55 // System.out.println(list); 56 57 sqlSession.close(); 58 59 } 60 61 }
标签:ibatis efault word nfa character val parameter cto com
原文地址:https://www.cnblogs.com/xiaostudy/p/9588985.html