概述
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 on
s.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;
/**
* 测试多对一
*/
@Test
public 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);
}
}
/**
* 测试一对多
*/
@Test
public 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依次去查询学生的信息,在延迟加载策略中比较有用
*/
@Test
public 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;
}
}
目录结构:
最后
以上就是友好茉莉为你收集整理的Mybatis多对一、多对一关联查询的全部内容,希望文章能够帮你解决Mybatis多对一、多对一关联查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复