文章目录
1.基本介绍
2.配置xml方式(多表联查) 1.数据库表设计 2.新建子模块
3.MyBatisUtils.java和jdbc.properties和mybatis-config.xml与原来的一致 4.IdenCard.java 5.Person.java 6.IdenCardMapper.java 7.IdenCardMapper.xml(名字需要与前面的接口名相同!!!) 8.IdenCardMapperTest.java测试 9.PersonMapper.java 10.PersonMapper.xml(一对一关系体现) 11.测试PersonMapperTest.java 12.查询优化 13.对一对一关系的理解 1.对IdenCard和Person属性的分析
2.查询出来的结果
3.配置xml方式(多表联查分解为单表操作) 1.PersonMapper.java添加方法 2.PersonMapper.xml添加查询 3.测试
4.注解方式 1.基本介绍 2.代码实现 1.IdenCardMapperAnnotation.java 2.PersonMapperAnnotation.java 3.PersonMapperAnnotationTest.java测试
5.注意事项和细节 1.表是否设置外键,对MyBatis级联映射没有影响 2.关于ResultMap
6.课后练习 1.IdenCard.java添加属性Person用来关联person表 2.IdenCardMapper.java添加方法 3.IdenCardMapper.xml添加实现类 4.PersonMapper.java根据card_id查询 5.PersonMapper.xml添加实现类 6.测试IdenCardMapperTest.java
7.一对一映射总结(idencard和person) 1.设计表 idencard表字段 person表字段 分析映射关系(person -> idencard)
2.设计bean(person -> idencard)
3.设计查询 根据person的id查询时级联查询idencard,返回一个Person对象
4.映射(idencard -> person) 1.设计bean 2.设计查询 根据idencard的id查询时级联查询person,返回一个Idencard对象
1.基本介绍
1.基本说明
2.映射方式
2.配置xml方式(多表联查)
1.数据库表设计
CREATE TABLE idencard(
id INT PRIMARY KEY auto_increment ,
card_sn VARCHAR ( 32 ) NOT NULL DEFAULT ''
) ;
INSERT INTO idencard VALUES ( 1 , '111111000' ) ;
CREATE TABLE person(
id INT PRIMARY KEY auto_increment ,
name VARCHAR ( 32 ) NOT NULL ,
card_id INT ,
FOREIGN KEY ( card_id) REFERENCES idencard( id)
) ;
INSERT INTO person VALUES ( 1 , '牛魔王' , 1 ) ;
2.新建子模块
1.创建子模块
2.创建基本结构
3.MyBatisUtils.java和jdbc.properties和mybatis-config.xml与原来的一致
4.IdenCard.java
package com. sun. entity ;
public class IdenCard {
private Integer id;
private String card_sn;
public IdenCard ( ) {
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getCard_sn ( ) {
return card_sn;
}
public void setCard_sn ( String card_sn) {
this . card_sn = card_sn;
}
@Override
public String toString ( ) {
return "IdenCard{" +
"id=" + id +
", card_sn='" + card_sn + '\'' +
'}' ;
}
}
5.Person.java
package com. sun. entity ;
public class Person {
private Integer id;
private String name;
private IdenCard card;
public Person ( ) {
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public IdenCard getCard ( ) {
return card;
}
public void setCard ( IdenCard card) {
this . card = card;
}
@Override
public String toString ( ) {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", card=" + card +
'}' ;
}
}
6.IdenCardMapper.java
package com. sun. mapper ;
import com. sun. entity. IdenCard ;
public interface IdenCardMapper {
public IdenCard getIdenCardById ( Integer id) ;
}
7.IdenCardMapper.xml(名字需要与前面的接口名相同!!!)
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.sun.mapper.IdenCardMapper" >
< select id = " getIdenCardById" resultType = " IdenCard" parameterType = " Integer" >
SELECT *
FROM idencard
WHERE id = #{id}
</ select>
</ mapper>
8.IdenCardMapperTest.java测试
import com. sun. entity. IdenCard ;
import com. sun. mapper. IdenCardMapper ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
public class IdenCardMapperTest {
private SqlSession sqlSession;
private IdenCardMapper idenCardMapper;
@Before
public void init ( ) {
sqlSession = MyBatisUtils . getSqlSession ( ) ;
idenCardMapper = sqlSession. getMapper ( IdenCardMapper . class ) ;
System . out. println ( idenCardMapper. getClass ( ) ) ;
}
@Test
public void getIdenCardById ( ) {
IdenCard idenCardById = idenCardMapper. getIdenCardById ( 1 ) ;
System . out. println ( idenCardById) ;
if ( sqlSession != null ) {
sqlSession. close ( ) ;
}
}
}
9.PersonMapper.java
package com. sun. mapper ;
import com. sun. entity. Person ;
public interface PersonMapper {
public Person getPersonById ( Integer id) ;
}
10.PersonMapper.xml(一对一关系体现)
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.sun.mapper.PersonMapper" >
< resultMap id = " getPersonByIdResult" type = " Person" >
< result property = " id" column = " id" />
< result property = " name" column = " name" />
< association property = " card" javaType = " IdenCard" >
< result property = " id" column = " id" />
< result property = " card_sn" column = " card_sn" />
</ association>
</ resultMap>
< select id = " getPersonById" parameterType = " Integer" resultMap = " getPersonByIdResult" >
SELECT *
FROM person,
idencard
WHERE person.card_id = idencard.id
AND person.id = #{id}
</ select>
</ mapper>
11.测试PersonMapperTest.java
import com. sun. entity. Person ;
import com. sun. mapper. IdenCardMapper ;
import com. sun. mapper. PersonMapper ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
public class PersonMapperTest {
private SqlSession sqlSession;
private PersonMapper personMapper;
@Before
public void init ( ) {
sqlSession = MyBatisUtils . getSqlSession ( ) ;
personMapper = sqlSession. getMapper ( PersonMapper . class ) ;
System . out. println ( personMapper. getClass ( ) ) ;
}
@Test
public void getPersonById ( ) {
Person personById = personMapper. getPersonById ( 1 ) ;
System . out. println ( personById) ;
}
}
12.查询优化
13.对一对一关系的理解
1.对IdenCard和Person属性的分析
1.IdenCard
id属性对应表的id字段 card_sn属性对应表的card_sn字段
2.Person
id属性对应表的id字段 name属性对应表的name字段 **card属性:**原本的person表应该是一个card_id字段,但是这个card_id字段是可以映射到idencard表的一条记录即在java中的IdenCard对象,所以使用这个对象来替代了原来的card_id字段
2.查询出来的结果
可以看出这个上面的结果只有四个字段,也就是删除了person表中原有的card_id字段!
3.配置xml方式(多表联查分解为单表操作)
1.PersonMapper.java添加方法
public Person getPersonById2 ( Integer id) ;
2.PersonMapper.xml添加查询
< resultMap id = " getPersonByIdResult2" type = " Person" >
< id property = " id" column = " id" />
< result property = " name" column = " name" />
< association property = " card" column = " card_id" select = " com.sun.mapper.IdenCardMapper.getIdenCardById" />
</ resultMap>
< select id = " getPersonById2" parameterType = " Integer" resultMap = " getPersonByIdResult2" >
select * from person where id = #{id}
</ select>
3.测试
4.注解方式
1.基本介绍
2.代码实现
1.IdenCardMapperAnnotation.java
package com. sun. mapper ;
import com. sun. entity. IdenCard ;
import org. apache. ibatis. annotations. Select ;
public interface IdenCardMapperAnnotation {
@Select ( "select * from `idencard` where `id` = #{id}" )
public IdenCard getIdenCardById ( Integer id) ;
}
2.PersonMapperAnnotation.java
package com. sun. mapper ;
import com. sun. entity. Person ;
import org. apache. ibatis. annotations. One ;
import org. apache. ibatis. annotations. Result ;
import org. apache. ibatis. annotations. Results ;
import org. apache. ibatis. annotations. Select ;
public interface PersonMapperAnnotation {
@Results ( {
@Result ( id = true , property = "id" , column = "id" ) ,
@Result ( property = "name" , column = "name" ) ,
@Result ( property = "card" , column = "card_id" , one = @One ( select = "com.sun.mapper.IdenCardMapperAnnotation.getIdenCardById" ) )
}
)
@Select ( "select * from `person` where id = #{id}" )
public Person getPersonById ( Integer id) ;
}
3.PersonMapperAnnotationTest.java测试
import com. sun. entity. Person ;
import com. sun. mapper. PersonMapper ;
import com. sun. mapper. PersonMapperAnnotation ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
public class PersonMapperAnnotationTest {
private SqlSession sqlSession;
private PersonMapperAnnotation personMapperAnnotation;
@Before
public void init ( ) {
sqlSession = MyBatisUtils . getSqlSession ( ) ;
personMapperAnnotation = sqlSession. getMapper ( PersonMapperAnnotation . class ) ;
System . out. println ( personMapperAnnotation. getClass ( ) ) ;
}
@Test
public void PersonMapperAnnotation ( ) {
Person personById = personMapperAnnotation. getPersonById ( 1 ) ;
System . out. println ( personById) ;
if ( sqlSession != null ) {
sqlSession. close ( ) ;
}
}
}
5.注意事项和细节
1.表是否设置外键,对MyBatis级联映射没有影响
2.关于ResultMap
MyBatis默认机制是调用查询到的列名的setter方法给返回的类型对象的属性赋值,但是如果列名与属性名不对应就只能使用ResultMap来进行映射 ResultMap中的column属性实际上指的是查询到的结果的列名,如果有别名则这个列就应该对应这个别名 如果有select属性,则指的是将这个column的值传给select属性对应的方法并得到返回值,然后调用property属性的setter方法为其赋值
6.课后练习
1.IdenCard.java添加属性Person用来关联person表
由于person表的card_id对应于idencard表的id,所以数据表不需要变化
package com. sun. entity ;
public class IdenCard {
private Integer id;
private String card_sn;
private Person person;
public Person getPerson ( ) {
return person;
}
public void setPerson ( Person person) {
this . person = person;
}
public IdenCard ( ) {
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getCard_sn ( ) {
return card_sn;
}
public void setCard_sn ( String card_sn) {
this . card_sn = card_sn;
}
@Override
public String toString ( ) {
return "IdenCard{" +
"id=" + id +
", card_sn='" + card_sn + '\'' +
", person=" + person +
'}' ;
}
}
2.IdenCardMapper.java添加方法
public IdenCard getIdenCardById2 ( Integer id) ;
3.IdenCardMapper.xml添加实现类
首先根据id查询idencard表的id和card_sn 将查询到的结果填充到IdenCard的id和card_sn属性中 再使用id调用person表的方法,当作person表的card_id来查到person表的数据并填充到IdenCard的Person属性中
< resultMap id = " getIdenCardById2ResultMap" type = " IdenCard" >
< id property = " id" column = " id" />
< result property = " card_sn" column = " card_sn" />
< association property = " person" column = " id" select = " com.sun.mapper.PersonMapper.findByCard_Id" />
</ resultMap>
< select id = " getIdenCardById2" resultMap = " getIdenCardById2ResultMap" parameterType = " Integer" >
SELECT *
FROM idencard
WHERE id = #{id}
</ select>
4.PersonMapper.java根据card_id查询
public Person findByCard_Id ( Integer card_id) ;
5.PersonMapper.xml添加实现类
< select id = " findByCard_Id" parameterType = " Integer" resultType = " Person" >
SELECT * FROM person WHERE card_id = #{card_id}
</ select>
6.测试IdenCardMapperTest.java
import com. sun. entity. IdenCard ;
import com. sun. mapper. IdenCardMapper ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
public class IdenCardMapperTest {
private SqlSession sqlSession;
private IdenCardMapper idenCardMapper;
@Before
public void init ( ) {
sqlSession = MyBatisUtils . getSqlSession ( ) ;
idenCardMapper = sqlSession. getMapper ( IdenCardMapper . class ) ;
System . out. println ( idenCardMapper. getClass ( ) ) ;
}
@Test
public void getIdenCardById2 ( ) {
IdenCard idenCardById2 = idenCardMapper. getIdenCardById2 ( 1 ) ;
System . out. println ( idenCardById2) ;
if ( sqlSession != null ) {
sqlSession. close ( ) ;
}
}
}
7.一对一映射总结(idencard和person)
1.设计表
idencard表字段
person表字段
分析映射关系(person -> idencard)
一个person对应一个idencard 表person添加字段card_id来映射idencard表的id
2.设计bean(person -> idencard)
Idencard
Person
id name Idencard(映射idencard表)
3.设计查询
根据person的id查询时级联查询idencard,返回一个Person对象
首先根据person的id查询到
id(填充到Person) name(填充到Person) card_id 再根据person的card_id查询idencard表
id(填充到Idencard对象) card_sn(填充到Idencard对象) 将Idencard对象封装到Person对象的属性
4.映射(idencard -> person)
1.设计bean
由于idencard的id对应person的card_id 所以表不需要变化 idencard 的bean添加字段Person映射表person
2.设计查询
根据idencard的id查询时级联查询person,返回一个Idencard对象
首先根据idencard的id查询
id(填充到Idencard) card_sn(填充到Idencard) 然后根据查询到的id,查询person表的card_id字段
id(填充到Person对象) name(填充到Person对象) card_id 将Person对象封装到Idencard对象的属性