Mybatis多对一、多对一关联查询

来源:互联网 发布:仟家信黄金分析软件 编辑:程序博客网 时间:2024/05/16 17:05

1,新建 实体class,与student,同时创建数据库


student:

CREATE TABLE`t_student` (

  `id` int(5) NOT NULL AUTO_INCREMENT,

  `sname` varchar(10) DEFAULT NULL,

  `cid` int(5) NOT NULL DEFAULT '0',

  PRIMARY KEY (`id`,`cid`)

)

clazz:

CREATE TABLE `t_classes`(

  `id` int(5) NOT NULL AUTO_INCREMENT,

  `cname` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`id`)

)


2.实体类对象:






ClazzMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="clazz"><select id="getAllClazzs" resultMap="cMap">select c.id cid,c.cname,s.id,s.sname from t_classes c left join t_student s ons.cid=c.id</select><resultMap type="cn.gzsxt.pojo.Clazz" id="cMap"><id property="id" column="cid" /><result property="cname" column="cname" /><collection property="students" javaType="java.util.List"ofType="cn.gzsxt.pojo.Student"><id property="id" column="id" /><result property="sname" column="sname" /></collection></resultMap></mapper>

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="student"> <select id="getALlStudents" resultMap="sMap">select s.id,s.sname,c.id cid,c.cname from t_student s,t_classes c where s.cid=c.id</select><resultMap type="cn.gzsxt.pojo.Student" id="sMap"><id property="id" column="id"/><result property="sname" column="sname"/><association property="clazz" javaType="cn.gzsxt.pojo.Clazz"><id property="id" column="cid"/><result property="cname" column="cname"/></association></resultMap><select id="getStudentByClazzId" resultType="cn.gzsxt.pojo.Student">select * from t_student where cid=#{cid}</select></mapper>

Text:

package cn.gzsxt.text;import java.io.IOException;import java.io.InputStream;import java.util.List;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import cn.gzsxt.pojo.Clazz;import cn.gzsxt.pojo.Student;public class TextConnection {private SqlSession sqlSession;/** * 测试多对一 */@Testpublic void textMany2One(){try {sqlSession = getSqlSession();List<Student> sList = sqlSession.selectList("getALlStudents");for (Student student : sList) {System.out.println(student);}} catch (Exception e) {e.printStackTrace();}finally{closeSession(sqlSession);}}/** * 测试一对多 */@Testpublic void textOnt2many(){try {sqlSession = getSqlSession();List<Clazz> cList = sqlSession.selectList("getAllClazzs");for (Clazz clazz : cList) {System.out.println(clazz);}} catch (Exception e) {e.printStackTrace();}finally{closeSession(sqlSession);}}/** * 测试一对多中n+1的问题 * 区别在于 首次查出所有的班级信息,然后根据班级的id依次去查询学生的信息,在延迟加载策略中比较有用 */@Testpublic void textOne2many2(){try {sqlSession = getSqlSession();List<Clazz> cList = sqlSession.selectList("clazz1.getAllClazzs1");for (Clazz clazz : cList) {System.out.println(clazz);}} catch (Exception e) {e.printStackTrace();}finally{closeSession(sqlSession);}}/** * 关闭session *  * @param session */private void closeSession(SqlSession sqlSession) {if (null != sqlSession) {sqlSession.close();}}/** * 获取SqlSession *  * @return * @throws IOException */private SqlSession getSqlSession() throws IOException {SqlSessionFactory sessionFactory;SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();// 2,读取配置文件InputStream inputStream = Resources.getResourceAsStream("mybatis.cfg.xml");sessionFactory = builder.build(inputStream);// 3,根据会话工厂获取会话SqlSession session = sessionFactory.openSession();return session;}}

目录结构:



原创粉丝点击