MaBatis之Mapper.xml的配置(多对一关联关系)
程序员文章站
2022-04-15 22:12:16
...
<?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.BookMapper">
<resultMap id="BaseResultMap" type="domain.Book">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="author" jdbcType="VARCHAR" property="author" />
<result column="isbn" jdbcType="VARCHAR" property="isbn" />
<result column="category_id" jdbcType="INTEGER" property="categoryId" />
</resultMap>
<sql id="Base_Column_List">
id, title, author, isbn, category_id
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from book
where id = #{id,jdbcType=INTEGER}
</select>
<resultMap id="bookResultMap" type="domain.Book" extends="BaseResultMap">
<!-- 关联属性多对一一般用的是立即加载(分步查询)
每次查询都要通过两表主键和外键进行匹配
也就说有多少个类别就查多少次,性能差,我希望能只查找一次全部都出来
fetchType是否立即加载eager就是立即加载,lazy就是懒加载
-->
<association property="category" javaType="Category" column="category_id"
select="dao.CategoryMapper.selectByPrimaryKey" fetchType="eager">
</association>
</resultMap>
<select id="selectAll" resultMap="bookResultMap">
select id, title, author, isbn, category_id from book
</select>
<resultMap id="bookResultMap2" type="domain.Book" extends="BaseResultMap">
<!-- 关联属性多对一一般用的是立即加载
这里跟bookResultMap区别:这里直接把category的Bean类属性和数据库的字段直接连接
-->
<association property="category" javaType="Category" column="category_id">
<!-- 这有个坑为了防止左外连接查询的时候多表联查出现mybatis分不清2个表的id所以给其中一个id加上
一个别名-->
<id property="id" column="catId"/>
<result property="name" column="name"/>
</association>
</resultMap>
<select id="selectAll2" resultMap="bookResultMap2">
<!-- 一次性获取关联对象(也就是join fetch)-->
select b.id,
b.title,
b.author,
b.isbn,
b.category_id,
c.id as catId,
c.name
from book b left join category c on b.category_id=c.id
</select>
<!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> <resultMap id="bookResultMap3" type="domain.Book" extends="BaseResultMap"> <association property="category" javaType="Category"> <id property="id" column="c_id"/> <result property="name" column="name"/> </association> </resultMap> <!-- 两表联查 效率没有左外连接快但是要比分步速度快--> <select id="selectAll3" resultMap="bookResultMap3"> select b.id, b.title, b.author, b.isbn, b.category_id, c.id as c_id, c.name from book b,category c where b.category_id = c.id </select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from book
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="domain.Book" useGeneratedKeys="true">
insert into book (title, author, isbn,
category_id)
values (#{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{isbn,jdbcType=VARCHAR},
#{categoryId,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="domain.Book" useGeneratedKeys="true">
insert into book
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="title != null">
title,
</if>
<if test="author != null">
author,
</if>
<if test="isbn != null">
isbn,
</if>
<if test="categoryId != null">
category_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
<if test="author != null">
#{author,jdbcType=VARCHAR},
</if>
<if test="isbn != null">
#{isbn,jdbcType=VARCHAR},
</if>
<if test="categoryId != null">
#{categoryId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="domain.Book">
update book
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
<if test="author != null">
author = #{author,jdbcType=VARCHAR},
</if>
<if test="isbn != null">
isbn = #{isbn,jdbcType=VARCHAR},
</if>
<if test="categoryId != null">
category_id = #{categoryId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="domain.Book">
update book
set title = #{title,jdbcType=VARCHAR},
author = #{author,jdbcType=VARCHAR},
isbn = #{isbn,jdbcType=VARCHAR},
category_id = #{categoryId,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
下一篇: Hibernate分页查询通用方法