文章目录
mapper通用方法
方法名 | 描述 |
---|---|
selectAll() | 返回所有数据 |
selectByExample() | 根据example进行查询 |
select(T) | T中的非空字段构成查询条件 |
selectByExampleAndRowBounds() | |
selectByRowBounds() | |
selectOne() | |
selectCount() | |
selectCountByExample() | |
update方法 | |
updateByExample() | |
updateByExampleSelective() | |
insertSelective | 只更新设置的字段 |
insert | 更新所有字段 |
delete | |
deleteByExample | |
deleteByPrimaryKey |
参数
parameterType
多参数使用索引
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
<select id="getXXXBeanList" resultType="XXBean">不需要写parameterType参数
select t.* from tableName where id = #{0} and name = #{1}
</select>
多参数使用注解
由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{id} and name = #{code}
</select>
多参数基于map
其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个。
public List<XXXBean> getXXXBeanList(HashMap map);
<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
select 字段... from XXX where id=#{xxId} code = #{xxCode}
</select>
返回值
Mybatis 中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部resultMap的引用,但是resultMap和resultType不能同时存在。
在Mybatis进行查询映射时,其实查询出的每一个属性都是放在一个对应的Map里面的,其中key是属性名,values则是其对应的值。
- 当提供的返回值类型属性是resultType时,Mybatis会将Map里面的key的value取出给resultType所指定对应的属性,其实Mybatis的每一个查询映射的返回值类型都是resultMap,只是当提供的返回值类型属性resultType的时候,Mybatis对其自动的给把对应的值献给resultType所指定对象的属性。
- 当提供的返回值类型是resultMap时,因为Map不能很好表示,就需要自己再进行一步把它转换为对应的对象,这常常在复杂的查询中很有作用如:assoiation、collection。
使用resultType类型
<!-- resultType对应JavaBean类型 -->
<select id="selectAll" resultType="cn.softjx.modle.User">
select t_id id,t_username username,t_password password from t_user;
</select>
可以看到在select语句中都进行了字段名的替换,因为在javabean中属性的命名与在数据库中真实的字段名是不一样的,所以需要在sql中进行手动的代换,这是字段比较少的情况如果数据多就会使sql语句变得复杂,不便于维护修改。
resultMap
<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
</resultMap>
<select id="selectAll2" resultMap="UserMap" >
select t_id,t_username,t_password from t_user;
</select>
使用resultMap在进行sql语句查询时不用在进行手动的替换,交给Mybatis自动映射完成,得到的结果是一致的。
column 字段对应的应当是数据库查询结果字段,而不是数据库中的字段
property 与Javabean中属性值一致
查询标签中的resultMap要与表映射的resultMap标签中的id一致。
一对多的设置
在项目中,某些实体类之间肯定有关键关系,比如一对一,一对多等。在hibernate 中用one to one和one to many,而mybatis 中就用association和collection。
association: 一对一关联(has one)
collection:一对多关联(has many)
注意,只有在做select查询时才会用到这两个标签,都有三种用法,且用法类似。
https://www.jianshu.com/p/018c0f083501
https://blog.csdn.net/liaoxiaohua1981/article/details/6862466
返回字符串list
List<String> listByDepartment(@Param("departmentId") int departmentId);
<select id="listByDepartment" resultType="java.lang.String">
select name from business_type where level = 2 and department_id = #{departmentId}
</select>
Example
import tk.mybatis.mapper.entity.Example;
方法名 | 描述 |
---|---|
selectProperties | 设置选择那些列 |
setOrderByClause(“字段名 ASC”); |
Criteria
Criteria criteria = new Example().createCriteria();
动态sql
if
简单的条件判断,利用 if 语句可以实现简单的条件选择、判断拼接和否定忽略。
<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
<association property="school" javaType="cn.softjx.modle.School">
<result column="s_name" property="name" />
</association>
</resultMap>
<!-- 自定义条件查询 -->
<select id="getStudent" resultMap="UserMap">
select *from t_user where 1=1
<if test="id!=null"><!-- if标签内的字段名应与javabean内的字段名一致 -->
and t_id=#{id}
</if>
<if test="username!=null and username!=''">
and t_username=#{username}
</if>
<if test="password!=null and password!=''">
and t_password=#{password}
</if>
</select>
使用 if 标签进行了动态SQL的拼接,需要注意的是 if 标签内的条件判断语句字段应该与javabean中定义的字段名一致,而不是数据库中真实的字段名。
数组判空
参数为数组object[]。在MyBatis判断空时,先判断是否为null,不为null则判断数组长度object.length是否大于0即可。
<if test="object!=null and object.length>0">
<yourSql>
</if>
参数为集合List。在MyBatis判断空时,先判断是否为null,不为null则判断集合长度object.size()是否大于0即可。
<if test="object!=null and object.size()>0">
<yourSql>
</if>
where
where标签语句的作用主要是简化SQL语句中 where 中的条件判断,where 元素的作用是在会写入 where 元素的地方输出一个 where,另外一个好处是不需要考虑 where 元素里面的条件输出是什么样子的,mybatis 会自动的帮我们处理,如果所有的条件都不满足那么 mybatis 就会查询出所有的记录,如果输出后是 and 开头的,mybatis 会把第一个 and 忽略,如果是 or 开头也会将其忽略。此外,在 where 元素中不需要考虑空格的问题,mybatis都会帮你自动的补充完善。
<select id="getStudent" resultMap="UserMap">
select *from t_user
<where>
<if test="id!=null"><!-- if标签内的字段名应与javabean内的字段名一致 -->
and t_id=#{id}
</if>
<if test="username!=null and username!=''">
and t_username=#{username}
</if>
<if test="password!=null and password!=''">
and t_password=#{password}
</if>
</where>
</select>
choose
choose 标签的作用相当于java中的 switch 语句,通常都是与 when 和 otherwise 搭配使用,when 表示按照顺序来判断是否满足条件。当 when 有条件满足的时候就会跳出 choose,即所有的 when 和 otherwise 条件中只会有一个输出,当所有条件都不满足时,输出 otherwise 中的内容。
<!-- choose自定义条件查询 -->
<select id="getStudent1" resultMap="UserMap">
select *from t_user where 1=1
<choose>
<when test="id!=null"><!-- if标签内的字段名应与javabean内的字段名一致 -->
and t_id=#{id}
</when>
<when test="username!=null and username!=''">
and t_username=#{username}
</when>
<otherwise>
and t_id=2
</otherwise>
</choose>
</select>
在 choose 标签中,条件会按 when 的顺序进行判断,当有一个值为真时,就会跳出 choose 返回内容。
set功能
使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
<update id="updataUser">
update t_user
<set>
<if test="username!=null and username!=''">
t_username=#{username}
</if>
<if test="password!=null and password!=''">
,t_password=#{password}
</if>
<if test="sid!=null">
,t_sid=#{sid}
</if>
</set>
where t_id=#{id}
</update>
在set标签中逗号是不能省略的,mybatis不会自动的把逗号补上造成SQL语句出错。
trim
trim标签时刻在自己包含的内容前加上某些前缀,也可以在其后加上后缀,与其对应的属性是 prefix 和 suffix ;可以把包含的内容的首部某些内容覆盖即忽略,也可以把尾部某些内容覆盖,对应的属性是 prefixOverrides 和 suffixOverrides。
<select id="getStudent3" resultMap="UserMap">
select *from t_user
<trim prefix="where" prefixOverrides='and | or' >
<if test="id!=null">
and t_id=#{id}
</if>
<if test="username!=null and username!=''">
and t_username=#{username}
</if>
<if test="password!=null and password!=''">
and t_password=#{password}
</if>
</trim>
</select>
foreach
foreach 的主要用在构建 in 条件中,它可以在SQL语句中迭代一个集合
<select id="getStudent2" resultMap="UserMap">
select * from t_user where t_id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
sql , include 功能
sql 抽取经常将要查询的列名,或插入用的列名抽取出来方便引用。
include 来引用已抽取的sql
<sql id="Userfield">
(t_username,t_password,t_sid)
</sql>
<insert id="addUser">
insert into t_user
<include refid="Userfield"></include>
values
<foreach collection="list" item="user" separator=",">
(#{user.username},#{user.password},#{user.sid})
</foreach>
</insert>
bind
<select id="getStudent3" resultMap="UserMap">
<!-- 绑定 uname 变量 值为 '%'+username+'%' -->
<bind name="uname" value="'%'+username+'%'"/>
select *from t_user where 1=1
<if test="id!=null">
and t_id=#{id}
</if>
<if test="username!=null and username!=''">
<!-- 将 bind 绑定的变量传入 -->
and t_username like #{uname}
</if>
<if test="password!=null and password!=''">
and t_password=#{password}
</if>
</select>
缓存机制
多表查询
https://www.jianshu.com/p/adf5ddc7247a
多对一
一对多
其他
like
传入参数中直接加入%%
param.setUsername("%CD%");
param.setPassword("%11%");
<select id="selectPersons" resultType="person" parameterType="person">
select id,sex,age,username,password from person where true
<if test="username!=null"> AND username LIKE #{username}</if>
<if test="password!=null">AND password LIKE #{password}</if>
</select>
bind标签
select id,sex,age,username,password from person where username LIKE #{pattern} ### CONCAT like concat('%',#{param},'%') ## pagehelper ### 引入maven依赖<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
使用
使用pageInfo或page接受参数
public PageInfo<Doc> selectDocByPage1(int currentPage, int pageSize) {
PageHelper.startPage(currentPage, pageSize);
List<Doc> docs = docMapper.selectByPageAndSelections();
PageInfo<Doc> pageInfo = new PageInfo<>(docs);
return pageInfo;
}
xml转义
在xml的sql语句中,不能直接用大于号、小于号要用转义字符。
转义字符 | 字符 | 描述 |
---|---|---|
< | < | 小于号 |
CDATA
在使用mybatis中还遇到<![CDATA[]]>的用法,在该符号内的语句,将不会被当成字符串来处理,而是直接当成sql语句,比如要执行一个存储过程。
<![CDATA[ when min(starttime)<='12:00' and max(endtime)<='12:00' ]]>#和$的区别
- #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的值是id,则解析成的sql为order by “id”.
- $将传入的数据直接显示生成在sql中。如:order by u s e r i d user_id userid,如果传入的值是111,那么解析成sql时的值为order by user_id, 如果传入的值是id,则解析成的sql为order by id.
- #方式能够很大程度防止sql注入。
4.$方式无法防止Sql注入。
5.KaTeX parse error: Expected 'EOF', got '#' at position 32: …传入表名. 6.一般能用#̲的就别用.
使用传入参数的属性
run_id like CONCAT('%',#{q.keyword},'%')
判断集合大小
<if test="q.usernames!=null and q.usernames.size>0">
</if>