注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-11-one2one,需要自取,需要配置maven环境以及mysql环境(sql语句在resource下的test.sql中),觉得有用可以点个小星星,小菜鸟在此Thanks~
所谓一对一多表查询,举个例子:我们有很多国家,每一个国家只有一个领导人(假设),我们需要根据id查询国家信息,带上领导人的信息。
创建数据表
设计表的时候,我们需要考虑由于是一对多关系,我们需要在国家表里面使用一个字段对应领导人的信息。
#创建数据库
CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
#创建数据表
CREATE TABLE `test`.`country` ( `cid` INT(10) NOT NULL AUTO_INCREMENT ,`cname` VARCHAR(20) NOT NULL ,`mid` INT(10) NOT NULL,PRIMARY KEY(`cid`)) ENGINE = MyISAM;
CREATE TABLE `test`.`minister` ( `mid` INT(10) NOT NULL AUTO_INCREMENT ,`mname` VARCHAR(20) NOT NULL,PRIMARY KEY(`mid`)) ENGINE = MyISAM;
#初始化数据表
INSERT INTO `country` (`cid`, `cname`, `mid`) VALUES ('1', 'aaa', '1');
INSERT INTO `country` (`cid`, `cname`, `mid`) VALUES ('2', 'bbb', '2');
INSERT INTO `country` (`cid`, `cname`, `mid`) VALUES ('3', 'ccc', '3');
INSERT INTO `minister` (`mid`, `mname`) VALUES ('1', 'sam');
INSERT INTO `minister` (`mid`, `mname`) VALUES ('2', 'jane');
INSERT INTO `minister` (`mid`, `mname`) VALUES ('3', 'jone');
数据表如下:
实体类
country对应的实体类:
public class Country {
private Integer cid;
private String cname;
private Minister minister;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Minister getMinister() {
return minister;
}
public void setMinister(Minister minister) {
this.minister = minister;
}
@Override
public String toString() {
return "Country{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", minister=" + minister +
'}';
}
}
Minister对应的实体类:
public class Minister {
private Integer mid;
private String mname;
@Override
public String toString() {
return "Minister [mid=" + mid + ", mname=" + mname + "]";
}
public Integer getMid() {
return mid;
}
public void setMid(Integer mid) {
this.mid = mid;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
}
MybatisUtils工具类,用于获取单例的sqlSession对象:
public class MyBatisUtils {
static private SqlSessionFactory sqlSessionFactory;
static public SqlSession getSqlSession() {
InputStream is;
try {
is = Resources.getResourceAsStream("mybatis.xml");
if (sqlSessionFactory == null) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
}
return sqlSessionFactory.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
在mybatis.xml里面注册mapper文件:
<mappers>
<mapper resource="mapper/mapper.xml"/>
</mappers>
我们的两个接口(对应mapper文件中的两个sql语句):
public interface ICountryDao {
Country selectCountryById(int cid);
Country selectCountryById2(int cid);
}
mapper文件,有两种写法,一种是嵌套结果,一种是嵌套查询,嵌套结果的就是使用一条sql完成查询,需要在自己定义的resultMap里面使用来组织结果属性,嵌套查询也有一个,但是在最外面的标签并没有将所有需要的东西查询出来,而是在里面指定需要关联查询的sql语句的id,这样的方式就是懒加载:
<?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="dao.ICountryDao">
<!-- resultMap 能解决字段和属性不一样的问题 -->
<!-- 以后用得比较多 ,是因为可以使用延迟加载-->
<!-- 嵌套查询 -->
<select id="selectMinisterByCountry" resultType="Minister">
select mid,mname from minister where mid=#{ooo}
</select>
<resultMap type="Country" id="countryMapper">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
<!-- country中有一个成员变量是ministers,它的泛型是Minister -->
<association property="minister"
select="selectMinisterByCountry"
column="mid">
</association>
</resultMap>
<select id="selectCountryById" resultMap="countryMapper">
select cid,cname,mid
from country
where
cid=#{cid}
</select>
<!-- 嵌套结果-->
<select id="selectCountryById2" resultMap="countryMapper2">
select * from country c,minister m where c.mid = m.mid and c.cid= #{cid}
</select>
<resultMap id="countryMapper2" type="Country">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
<association property="minister" javaType="Minister">
<id property="mid" column="mid"/>
<result property="mname" column="mname"/>
</association>
</resultMap>
</mapper>
单元测试:
public class MyTest {
private ICountryDao dao;
private SqlSession sqlSession;
@Before
public void Before(){
sqlSession=MyBatisUtils.getSqlSession();
dao=sqlSession.getMapper(ICountryDao.class);
}
@Test
public void TestselectCountryById(){
Country country=dao.selectCountryById(1);
System.out.println(country);
}
@Test
public void TestselectCountryById2(){
Country country=dao.selectCountryById2(1);
System.out.println(country);
}
@After
public void after(){
if(sqlSession!=null){
sqlSession.close();
}
}
}
结果:
[service] 2018-07-12 15:23:38,971 - dao.ICountryDao.selectCountryById -537 [main] DEBUG dao.ICountryDao.selectCountryById - ==> Preparing: select cid,cname,mid from country where cid=?
[service] 2018-07-12 15:23:39,004 - dao.ICountryDao.selectCountryById -570 [main] DEBUG dao.ICountryDao.selectCountryById - ==> Parameters: 1(Integer)
[service] 2018-07-12 15:23:39,037 - dao.ICountryDao.selectMinisterByCountry -603 [main] DEBUG dao.ICountryDao.selectMinisterByCountry - ====> Preparing: select mid,mname from minister where mid=?
[service] 2018-07-12 15:23:39,037 - dao.ICountryDao.selectMinisterByCountry -603 [main] DEBUG dao.ICountryDao.selectMinisterByCountry - ====> Parameters: 1(Integer)
[service] 2018-07-12 15:23:39,040 - dao.ICountryDao.selectMinisterByCountry -606 [main] DEBUG dao.ICountryDao.selectMinisterByCountry - <==== Total: 1
[service] 2018-07-12 15:23:39,041 - dao.ICountryDao.selectCountryById -607 [main] DEBUG dao.ICountryDao.selectCountryById - <== Total: 1
Country{cid=1, cname='aaa', minister=Minister [mid=1, mname=sam]}
此文章仅代表自己(本菜鸟)学习积累记录,或者学习笔记,如有侵权,请联系作者删除。人无完人,文章也一样,文笔稚嫩,在下不才,勿喷,如果有错误之处,还望指出,感激不尽~
技术之路不在一时,山高水长,纵使缓慢,驰而不息。
公众号:秦怀杂货店