mybatis 一对一级联 一对多

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 一对一级联 一对多相关的知识,希望对你有一定的参考价值。

一:一对一   用association

一   建表

   course 课程与score 课程成绩表是一对一关系

 course课程表

技术分享

 

score课程成绩表

技术分享

连接数据库dbconf.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--<mapper resource="StfMapper.xml"/>-->
<!--<mapper resource="KcMapper.xml"/>-->
<mapper resource="StudMapper.xml"/>
<mapper resource="ScoreMapper.xml"/>
<mapper resource="CourseMapper.xml"/>
</mappers>
</configuration>
写一个db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myba
jdbc.username=root
jdbc.password=×××

 

 

 

 二 使用mybatis插件自动生成相对应表的配置文件

   CourseMapper.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="com.ygy.mapper.CourseMapper" >
<resultMap id="course" type="com.ygy.model.Course" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Oct 27 12:25:54 CST 2017.
-->
<id column="coid" property="coid" jdbcType="INTEGER" />
<result column="coname" property="coname" jdbcType="VARCHAR" />
</resultMap>
<select id="selectcou" resultMap="course" parameterType="int">
SELECT myba.course.* from myba.course WHERE coid=#{coid}
</select>
</mapper>

CourseMapper接口
*CourseMapper.xml是对接口的实现 所以 select 的id与接口中方法名相同
package com.ygy.mapper;

import com.ygy.model.Course;
import com.ygy.model.CourseExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface CourseMapper {
public Course selectcou(int id);
}

ScoreMapper.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="com.ygy.mapper.ScoreMapper" >
<resultMap id="score" type="com.ygy.model.Score" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Oct 27 12:25:54 CST 2017.
-->
<id column="scid" property="scid" jdbcType="INTEGER" />
<result column="scsid" property="scsid" jdbcType="INTEGER" />
<result column="sckcid" property="sckcid" jdbcType="INTEGER" />
<result column="mark" property="mark" jdbcType="VARCHAR" />
<association property="course" column="sckcid" select="com.ygy.mapper.CourseMapper.selectcou"/>
</resultMap>
<select id="selectscore" parameterType="int" resultMap="score">
select myba.score.* from myba.score WHERE myba.score.scsid=#{id}
</select>
</mapper>
ScoreMapper接口
package com.ygy.mapper;

import com.ygy.model.Score;
import com.ygy.model.ScoreExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface ScoreMapper {
public Score selectscore(int id);
}
在score的model中加上
private Course course;
//get set方法

三 测试
@Test
public void main(){
SqlSessionFactory sqlSessionFactory;
InputStream inputStream;

try {
inputStream= Resources.getResourceAsStream("dbconf.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
sqlSession=sqlSessionFactory.openSession();
//SCORC和course一对一
ScoreMapper scoreMapper=sqlSession.getMapper(ScoreMapper.class);
Score score=scoreMapper.selectscore(1);
System.out.println(score.getCourse().getConame());

} catch (IOException e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
二:一对多   collection
加入学生表
stud
技术分享

插件生成对应的接口什么的

<?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="com.ygy.mapper.StudMapper" >
<resultMap id="stud" type="com.ygy.model.Stud" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 26 15:09:44 CST 2017.
-->
<id column="sid" property="sid" jdbcType="INTEGER" />
<result column="sname" property="sname" jdbcType="VARCHAR" />
<!--<association property="kc" column="sid" select="com.ygy.mapper.KcMapper.select"/>-->
<collection property="scoreList" column="sid" select="com.ygy.mapper.ScoreMapper.selectscore"/>
<!--collection 将column的数据传到select对应的接口 property映射到列结果的字段或属性-->
</resultMap>
<select id="selectsud" parameterType="int" resultMap="stud">
SELECT myba.stud.* FROM myba.stud WHERE myba.stud.sid=#{id}
</select>
</mapper>

接口
package com.ygy.mapper;

import com.ygy.model.Stud;
import com.ygy.model.StudExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface StudMapper {
public Stud selectsud(int id);
}

测试
@Test
public void main(){
SqlSessionFactory sqlSessionFactory;
InputStream inputStream;

try {
inputStream= Resources.getResourceAsStream("dbconf.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
sqlSession=sqlSessionFactory.openSession();
     StudMapper studMapper=sqlSession.getMapper(StudMapper.class);
     Stud stud=studMapper.selectsud(1);
    System.out.println(stud.getScoreList().get(1).getMark());
     } catch (IOException e) {
     e.printStackTrace();
     }finally {
     sqlSession.close();
     }
结果:
技术分享

 

 





 
 

 

 

 

 










































































































































































以上是关于mybatis 一对一级联 一对多的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis 使用接口增删改查和两表一对一级联查询

mybatis之级联关系(一对一对多)

Mybatis 级联查询 (一对多 )

java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???

Mybatis一对一,一对多,多对多代码

mybatis 一对多和多对一关联查询