底层封装jdbc 优化开发
上手
准备数据库表 实体类 创建boot 项目 idea中连接对应的表 导入依赖 mysql mybatis junit
实体类与mysql表中一致 编写spring对应的mybatis配置类
测试
{
编写持久层 简单语句用注解 @Select 查询所有语句
boot自带的测试类测试 junit @Test
这里的实体类要么get set 要么lombok @Data 不然查不出数据只能查地址
}
crud
{
#{} 控制台显示? 安全
预编译sql语句的性能更高 > 缓存,下次执行时检查是否有类似的sql语句大大的提高性能
普通sql select * from emp where id =1;
select * from emp where id =1;
select * from emp where id =1;执行三次
预编译sql语句 select * from where id = #{id} 预编译一次
sql注入 ' or ' 1 ' = ' 1 1=1永远成功通过预编译sql防止sql注入
${} 拼接sql语句 不是预编译sql
预编译的sql语句性能更高 有缓存;更安全(防止sql注入)
新增
//会自动将生成的主键值,赋值给emp对象的id属性 @Options(useGeneratedKeys = true,keyProperty = "id")
@insert("insert into (数据库字段)values(#{实体类字段}.....)")
实体类 insert(实体类 起名)
更新
@update(update 表明 set 数据库字段 = #{实体类字段}... where 数据库字段= #{实体类字段} )
查询
@Select("select 数据库字段 from 表名 where 数据库字段 =#{实体类字段}")
要使用驼峰命名前提是 实体类的属性 与 数据库表中的字段名严格遵守驼峰民命
{
mybatis.configuration.map-underscore-to-camel-case=true
模糊查询 {"where name like '%${name}%' "}
使用MySQL提供的字符串拼接函数:concat('%' , '关键字' , '%')防止sql注入
{
"where name like concat('%',#{name},'%') "
}
在springBoot的1.x版本/单独使用mybatis(使用@Param注解来指定SQL语句中的参数名)
public List<User> All(@Param("name") String name)
springboot2.x不需要
public List<User> All(String name);
}
Mybatis的核心配置文件.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="接口名路径">
</mapper>
}
语句
{
<!--查询操作-->
<select id="接口方法" resultType="接口实体类路径">
select * from emp
where name like concat('%',#{name},'%')
and gender = #{gender}
and entrydate between #{begin} and #{end}
order by update_time desc
</select>
MybatisX是一款基于IDEA的快速开发Mybatis的插件,为效率而生。 插件 高效开发
}
mybatis动态sql
<if>判断是否成立
<select id="接口方法" resultType="接口实体类">
select * from emp
where
<if test="name != null">
name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
order by update_time desc
</select>
</if>
上述代码 如果第一个参数为null 则存在bug 解决办法
只会在子元素有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR
<where></where> 替换where
更新
<?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="接口lu">
<!--更新操作-->
<update id="接口方法">
update emp
set
<if test="username != null">
username=#{username},
</if>
<if test="name != null">
name=#{name},
</if>
<if test="gender != null">
gender=#{gender},
</if>
<if test="image != null">
image=#{image},
</if>
<if test="job != null">
job=#{job},
</if>
<if test="entrydate != null">
entrydate=#{entrydate},
</if>
<if test="deptId != null">
dept_id=#{deptId},
</if>
<if test="updateTime != null">
update_time=#{updateTime}
</if>
where id=#{id}
</update>
</mapper>
有bug 使用set标签,代替update语句中的set关键字 <set></set>
遍历
<foreach collection="集合名称" item="集合遍历出来的元素/项" separator="每一次遍历使用的分隔符"
open="遍历开始前拼接的片段" close="遍历结束后拼接的片段">
</foreach>
<?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="接口路径">
<!--删除操作-->
<delete id="接口方法">
delete from emp where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
</mapper>
sql片段抽取代码快
<sql id="xxx">
select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp
</sql>
<select id="list" resultType="接口实体类">
<include refid="xxx"/>
<where>
<if test="name != null">
name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
}