文章目录
1.需求分析 2.应用实例(xml配置) 1.数据表设计 2.entity设计(不要使用toString会栈溢出)
3.编写Mapper 1.PetMapper.java 2.UserMapper.java
4.编写Mapper.xml 1.UserMapper.xml 2.PetMapper.xml
5.测试UserMapperTest.java 6.resultMap复用实现PetMapper.java的getPetById方法 1.修改PetMapper.xml添加实现类 2.测试
3.应用实例(注解方式) 1.UserMapperAnnotation.java 2.PetMapperAnnotation.java
4.课后练习(一对多) 1.数据表设计 2.entity设计(不要使用toString会栈溢出)
3.编写Mapper 1.DeptMapper.java 2.EmpMapper.java
4.编写Mapper.xml 1.DeptMapper.xml 2.EmpMapper.xml
5.测试DeptMapperTest.java
5.MyBatis映射关系总结 1.一对一 1.表设计 2.entity设计 3.Mapper设计
2.一对多(多对一) 1.表设计 2.entity设计 3.Mapper设计 4.最简化的理解
1.需求分析
2.应用实例(xml配置)
1.数据表设计
CREATE TABLE mybatis_user(
id INT PRIMARY KEY auto_increment ,
name VARCHAR ( 32 ) NOT NULL DEFAULT ''
) ;
CREATE TABLE mybatis_pet(
id INT PRIMARY KEY auto_increment ,
nickname VARCHAR ( 32 ) NOT NULL DEFAULT '' ,
user_id INT
) ;
INSERT INTO mybatis_user VALUES ( NULL , '宋江' ) , ( NULL , '张飞' ) ;
INSERT INTO mybatis_pet VALUES ( NULL , '黑背' , 1 ) , ( NULL , '小哈' , 1 ) ;
INSERT INTO mybatis_pet VALUES ( NULL , '波斯猫' , 2 ) , ( NULL , '贵妃猫' , 2 ) ;
SELECT * FROM mybatis_user;
SELECT * FROM mybatis_pet;
2.entity设计(不要使用toString会栈溢出)
1.Pet.java
package com. sun. entity ;
public class Pet {
private Integer id;
private String nickname;
private User user;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getNickname ( ) {
return nickname;
}
public void setNickname ( String nickname) {
this . nickname = nickname;
}
public User getUser ( ) {
return user;
}
public void setUser ( User user) {
this . user = user;
}
}
2.User.java
package com. sun. entity ;
import java. util. List ;
public class User {
private Integer id;
private String name;
private List < Pet > pets;
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 List < Pet > getPets ( ) {
return pets;
}
public void setPets ( List < Pet > pets) {
this . pets = pets;
}
}
3.编写Mapper
1.PetMapper.java
package com. sun. mapper ;
import com. sun. entity. Pet ;
import java. util. List ;
public interface PetMapper {
public List < Pet > getPetByUserId ( Integer userId) ;
public Pet getPetById ( Integer id) ;
}
2.UserMapper.java
package com. sun. mapper ;
import com. sun. entity. User ;
public interface UserMapper {
public User getUserById ( Integer id) ;
}
4.编写Mapper.xml
1.UserMapper.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.UserMapper" >
< resultMap id = " getUserByIdresultMap" type = " User" >
< id property = " id" column = " id" />
< result property = " name" column = " name" />
< collection property = " pets" column = " id" select = " com.sun.mapper.PetMapper.getPetByUserId" ofType = " Pet" />
</ resultMap>
< select id = " getUserById" parameterType = " Integer" resultMap = " getUserByIdresultMap" >
SELECT * FROM mybatis_user WHERE id = #{id}
</ select>
</ mapper>
2.PetMapper.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.PetMapper" >
< resultMap id = " getPetByUserId_resultMap" type = " Pet" >
< id property = " id" column = " id" />
< result property = " nickname" column = " nickname" />
< association property = " user" column = " user_id" select = " com.sun.mapper.UserMapper.getUserById" />
</ resultMap>
< select id = " getPetByUserId" parameterType = " Integer" resultMap = " getPetByUserId_resultMap" >
SELECT * FROM mybatis_pet WHERE user_id = #{userId}
</ select>
</ mapper>
5.测试UserMapperTest.java
import com. sun. entity. Pet ;
import com. sun. entity. User ;
import com. sun. mapper. PetMapper ;
import com. sun. mapper. UserMapper ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
import java. util. List ;
public class UserMapperTest {
private SqlSession sqlSession;
private UserMapper userMapper;
@Before
public void init ( ) {
sqlSession = MyBatisUtils . getSqlSession ( ) ;
userMapper = sqlSession. getMapper ( UserMapper . class ) ;
System . out. println ( userMapper. getClass ( ) ) ;
}
@Test
public void getUserById ( ) {
User user = userMapper. getUserById ( 1 ) ;
System . out. println ( user. getName ( ) + "-" + user. getId ( ) ) ;
List < Pet > pets = user. getPets ( ) ;
for ( Pet pet : pets) {
System . out. println ( pet. getId ( ) + "-" + pet. getNickname ( ) + "-" + pet. getUser ( ) ) ;
}
if ( sqlSession != null ) {
sqlSession. close ( ) ;
}
}
}
6.resultMap复用实现PetMapper.java的getPetById方法
1.修改PetMapper.xml添加实现类
< select id = " getPetById" parameterType = " Integer" resultMap = " getPetByUserId_resultMap" >
SELECT * FROM mybatis_pet WHERE id = #{id}
</ select>
2.测试
@Test
public void getPetById ( ) {
Pet petById = petMapper. getPetById ( 1 ) ;
System . out. println ( petById. getId ( ) + " " + petById. getNickname ( ) + " " + petById. getUser ( ) ) ;
}
3.应用实例(注解方式)
1.UserMapperAnnotation.java
package com. sun. mapper ;
import com. sun. entity. User ;
import org. apache. ibatis. annotations. Many ;
import org. apache. ibatis. annotations. Result ;
import org. apache. ibatis. annotations. Results ;
import org. apache. ibatis. annotations. Select ;
public interface UserMapperAnnotation {
@Results ( {
@Result ( id = true , property = "id" , column = "id" ) ,
@Result ( property = "name" , column = "name" ) ,
@Result ( property = "pets" , column = "id" , many = @Many ( select = "com.sun.mapper.PetMapperAnnotation.getPetByUserId" ) )
} )
@Select ( "SELECT * FROM mybatis_user WHERE id = #{id}" )
public User getUserById ( Integer id) ;
}
2.PetMapperAnnotation.java
package com. sun. mapper ;
import com. sun. entity. Pet ;
import org. apache. ibatis. annotations. * ;
import java. util. List ;
public interface PetMapperAnnotation {
@Results ( id = "getPetByUserIdResultMap" , value = {
@Result ( id = true , property = "id" , column = "id" ) ,
@Result ( property = "nickname" , column = "nickname" ) ,
@Result ( property = "user" , column = "user_id" , one = @One ( select = "com.sun.mapper.UserMapperAnnotation.getUserById" ) )
} )
@Select ( "SELECT * FROM mybatis_pet WHERE user_id = #{userId}" )
public List < Pet > getPetByUserId ( Integer userId) ;
@ResultMap ( "getPetByUserIdResultMap" )
@Select ( "SELECT * FROM mybatis_pet WHERE id = #{id}" )
public Pet getPetById ( Integer id) ;
}
4.课后练习(一对多)
1.数据表设计
CREATE TABLE dept(
id INT PRIMARY KEY auto_increment ,
name VARCHAR ( 32 ) NOT NULL DEFAULT ''
)
CREATE TABLE emp(
id INT PRIMARY KEY auto_increment ,
name VARCHAR ( 32 ) NOT NULL DEFAULT '' ,
dept_no INT NOT NULL DEFAULT 0
)
INSERT INTO dept VALUES ( NULL , '财务部' ) ;
INSERT INTO dept VALUES ( NULL , '外交部' ) ;
INSERT into emp VALUES ( NUll , '李白1' , 1 ) ;
INSERT into emp VALUES ( NUll , '孙悟空1' , 2 ) ;
INSERT into emp VALUES ( NUll , '李白2' , 1 ) ;
INSERT into emp VALUES ( NUll , '孙悟空2' , 2 ) ;
2.entity设计(不要使用toString会栈溢出)
1.Dept.java
package com. sun. entity ;
import java. util. List ;
public class Dept {
private Integer id;
private String name;
private List < Emp > emps;
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 List < Emp > getEmps ( ) {
return emps;
}
public void setEmps ( List < Emp > emps) {
this . emps = emps;
}
}
2.Emp.java
package com. sun. entity ;
public class Emp {
private Integer id;
private String name;
private Dept dept;
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 Dept getDept ( ) {
return dept;
}
public void setDept ( Dept dept) {
this . dept = dept;
}
}
3.编写Mapper
1.DeptMapper.java
package com. sun. mapper ;
import com. sun. entity. Dept ;
public interface DeptMapper {
public Dept findById ( Integer id) ;
}
2.EmpMapper.java
package com. sun. mapper ;
import com. sun. entity. Emp ;
import java. util. List ;
public interface EmpMapper {
public List < Emp > findByDeptId ( Integer dept_id) ;
}
4.编写Mapper.xml
1.DeptMapper.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.DeptMapper" >
< resultMap id = " findByIdMap" type = " Dept" >
< id property = " id" column = " id" />
< result property = " name" column = " name" />
< collection property = " emps" column = " id" ofType = " Emp" select = " com.sun.mapper.EmpMapper.findByDeptId" />
</ resultMap>
< select id = " findById" parameterType = " Integer" resultMap = " findByIdMap" >
SELECT * FROM dept WHERE id = #{id}
</ select>
</ mapper>
2.EmpMapper.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.EmpMapper" >
< resultMap id = " findByDeptIdMap" type = " Emp" >
< id property = " id" column = " id" />
< result property = " name" column = " name" />
< association property = " dept" column = " dept_no" select = " com.sun.mapper.DeptMapper.findById" />
</ resultMap>
< select id = " findByDeptId" parameterType = " Integer" resultMap = " findByDeptIdMap" >
SELECT * FROM emp WHERE dept_no = #{dept_id}
</ select>
</ mapper>
5.测试DeptMapperTest.java
import com. sun. entity. Dept ;
import com. sun. mapper. DeptMapper ;
import com. sun. mapper. HusbandMapper ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
public class DeptMapperTest {
private SqlSession sqlSession;
private DeptMapper deptMapper;
@Before
public void init ( ) {
sqlSession = MyBatisUtils . getSqlSession ( ) ;
deptMapper = sqlSession. getMapper ( DeptMapper . class ) ;
System . out. println ( deptMapper. getClass ( ) ) ;
}
@Test
public void findById ( ) {
Dept byId = deptMapper. findById ( 1 ) ;
System . out. println ( byId. getId ( ) + " " + byId. getName ( ) ) ;
}
}
5.MyBatis映射关系总结
1.一对一
1.表设计
有外键的是从表,被指向的是主表 设置一个有外键的从表指向一个主表(一般指向主键)
2.entity设计
在表中有外键的一方将外键的字段部分替换为指向的主表的entity对象
3.Mapper设计
查询有外键的一方的基本信息和级联的另一方的信息 注意:如果想要双向映射,不需要再加一个外键,只需要在另一个entity实体类中加上一个entity对象即可
2.一对多(多对一)
1.表设计
2.entity设计
3.Mapper设计
4.最简化的理解
确定一方与多方关联的列名,简述为关联列名 一方查询基本信息,使用关联列名级联查询多方信息 多方查询基本信息,使用关联列名查询一方信息