mybatis常用配置与简单的使用
resultMap 用于在数据库表字段和java实体类字段建立转换关系
<!-- resultMap 数据结果集映射map,主要作用就是将数据库中的字段跟需要映射的model的属性一一对应,
这样即使model中的属性跟数据库中的字段不一样,也能映射成功
id 这个resultMap的唯一表示,
type 需要映射的model信息-->
<resultMap id="customerMap" type="com.soft.mybatis.model.Customer">
<!-- id 属性专门用来映射主键信息,其他信息用result节点
column 数据库字段
property model属性 -->
<id column="id" property="id"/>
<!-- result 用来映射非主键信息, column property 作用跟id标签的一样-->
<result column="c_name" property="name"/>
<result column="c_sex" property="sex"/>
<result column="c_age" property="age"/>
<result column="c_ceroNo" property="ceroNo"/>
<result column="c_ceroType" property="ceroType"/>
</resultMap>
在标签中是需要增加 resultType 类型,存放查询结果。
mybatisの动态SQL
if 根据条件包含 where 子句的一部分
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
choose when otherwise 不想应用到所有的条件语句,而只想从中择其一项
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
注意:当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
mybatis的配置文件
configuration 配置 主要包含以下信息:
properties 属性
settings 设置
typeAliases 类型别名
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
environments 环境
environment 环境变量
transactionManager 事务管理器
dataSource 数据源
databaseIdProvider 数据库厂商标识
mappers 映射器
项目中使用到的主要是以下列出配置,mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存。默认值true -->
<setting name="cacheEnabled" value="false"/>
<!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。默认false-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--允许 JDBC 支持自动生成主键,需要驱动兼容-->
<setting name="useGeneratedKeys" value="true"/>
<!--允许在嵌套语句中使用分页-->
<setting name="safeRowBoundsEnabled" value="false"/>
<!--配置默认的执行器。SIMPLE普通的执行器;REUSE 执行器会重用预处理语句(prepared statements); BATCH 执行器将重用语句并执行批量更新-->
<setting name="defaultExecutorType" value="REUSE"/>
<!-- 设置超时时间,它决定驱动等待数据库响应的秒数。 -->
<setting name="defaultStatementTimeout" value="600"/>
</settings>
<!--类型别名,存在的意义仅在于用来减少类完全限定名的冗余-->
<typeAliases>
<typeAlias type="com.sgcc.ebm.app.model.Agency" alias="agency"/>
</typeAliases>
<!--用类型处理器将获取的值以合适的方式转换成 Java 类型-->
<typeHandlers>
</typeHandlers>
<!--告诉 MyBatis 到哪里去找映射文件-->
<mappers>
<mapper resource="mapper/agency.xml"/>
</mappers>
</configuration>
mybatis 批量插入数据
- 查询后插入SQL拼接法
好处:新增的时候主键自增,自动生成主键
<insert id="saveInfo" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
insert into v_test
(
`test_name`,
`test_num`,
`test_date`,
)
<foreach collection="list" item="item" index="index" separator="union">
select
#{item.testName},
#{item.testNum},
now()
from dual
</foreach>
</insert>
- 直接数据拼接法
缺点:需要手动的生成ID
<insert id="saveBatch" parameterType="java.util.List">
<selectKey resultType ="java.lang.Integer" keyProperty= "id" order= "AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into v_test
(
`test_id`,
`test_name`,
`test_num`,
`test_date`,
)
values
<foreach collection ="list" item="rec" index= "index" separator =",">
(
#{rec.testId},
#{item.testName},
#{item.testNum},
now()
)
</foreach >
</insert>
- 新增的有的时候可以用到 和 insert into类似的replace into语句