MyBatis-3
多表模型
一对一
标签:配置字段和对象属性的映射关系标签。
id属性:唯一标识
type属性:实体对象类型
:配置主键映射关系标签。
:配置非主键映射关系标签。
column属性:表中字段名称
property属性:实体对象变量名称
:配置被包含对象的映射关系标签。
property属性:被包含对象的变量名
JavaType属性:被包含对象的数据类型
一对多
标签:配置字段和对象属性的映射关系标签。
id属性:唯一标识
type属性:实体对象类型
:配置主键映射关系标签。
:配置非主键映射关系标签。
column属性:表中字段名称
property属性:实体对象变量名称
:配置被包含集合对象的映射关系标签。
property属性:被包含集合对象的变量名
ofType属性:集合中保存的对象数据类型
ResultMap
作用:ResultMap他就是一个方法的返回值的对应映射,等价于ResultType
ResultMap相当于一个通用返回值类型,具体返回值类型通过type属性指定
多对多
标签:配置字段和对象属性的映射关系标签。
id属性:唯一标识
type属性:实体对象类型
:配置主键映射关系标签。
:配置非主键映射关系标签。
column属性:表中字段名称
property属性:实体对象变量名称
:配置被包含集合对象的映射关系标签。
property属性:被包含集合对象的变量名
ofType属性:集合中保存的对象数据类型
通过注解实现mybatis功能
工程搭建步骤
- 新建一个模块
- 新建libs目录导包
- mybatis的核心配置文件
- 创建实体类型
- 写接口
- 直接利用SQLSession获取接口实现对象
注解开发
对比配置文件方式的不同
-
SQL编写的位置
-
核心配置文件如何加载Mapper的
-
使用MApper配置文件加载的方式
<mappers> <mapper resource="com/itheima/one_to_one/OneToOneMapper.xml"/> <mapper resource="com/itheima/one_to_many/OneToManyMapper.xml"/> <mapper resource="com/itheima/many_to_many/ManyToManyMapper.xml"/> </mappers>
-
加载接口的方式
<mappers> <package name="com.itheima.mapper"/> </mappers>
-
注解开发注意事项
- @Results的大括号是写在括号内的
- @Result中的属性是不区分先后顺序的
- @One中的select中指定的方法如果有参数那么参数的来源是查询的结果的字段
- Result里面可以使用的column字段是查询结果集中的字段
//查询全部身份证(一对一)
@Select("SELECT * FROM card")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "number",property = "number"),
@Result(
javaType = Person.class, // 被包含对象的实际数据类型
property = "p", // 被包含对象的变量名
column = "pid", // 根据查询出的card表中的pid字段来查询person表
/*
one=@One 一对一固定写法
select属性:指定调用哪个接口中的哪个方法
*/
one = @One(select ="com.itheima.one_to_one.PersonMapper.selectById")
)
})
public abstract List<Card> selectAll();
public interface PersonMapper {
//根据id查询
@Select("SELECT * FROM person WHERE id=#{id}")
public abstract Person selectById(Integer id);
}
//---------------------------------------------------------------------------------
//查询全部(一对多)
@Select("SELECT * FROM classes")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(
property = "students", // 被包含对象的变量名
javaType = List.class, // 被包含对象的实际数据类型
column = "id", // 根据查询出的classes表的id字段来查询student表
/*
many、@Many 一对多查询的固定写法
select属性:指定调用哪个接口中的哪个查询方法
*/
many = @Many(select = "com.itheima.one_to_many.StudentMapper.selectByCid")
)
})
public abstract List<Classes> selectAll();
public interface StudentMapper {
//根据cid查询student表
@Select("SELECT * FROM student WHERE cid=#{cid}")
public abstract List<Student> selectByCid(Integer cid);
}
//------------------------------------------------------------------------------
//查询全部(多对多)
@Select("SELECT DISTINCT s.id,s.name,s.age FROM student s,stu_cr sc WHERE sc.sid=s.id"//SELECT * FROM student where id in (select sid from stu_cr))
@Results({
@Result(column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(column = "age",property = "age"),
@Result(
property = "courses", // 被包含对象的变量名
javaType = List.class, // 被包含对象的实际数据类型
column = "id", // 根据查询出student表的id来作为关联条件,去查询中间表和课程表
/*
many、@Many 一对多查询的固定写法
select属性:指定调用哪个接口中的哪个查询方法
*/
many = @Many(select = "com.itheima.many_to_many.CourseMapper.selectBySid")
)
})
public abstract List<Student> selectAll();
public interface CourseMapper {
//根据学生id查询所选课程
@Select("SELECT c.id,c.name FROM stu_cr sc,course c WHERE sc.cid=c.id AND sc.sid=#{id}")
public abstract List<Course> selectBySid(Integer id);
}