MyBatis
Mybaits 是一个优秀的 持久层 框架, 用于简化 JDBC 开发
简单的说 : Mabatis 是更简单完成程序和数据库交互的框架, 也就是更简单的操作和读取数据库的工具
#{ } & ${ }
#{ } : 预编译 SQL
${ } : 即时 SQL
SQL 的执行流程
- 解析语法, 校验 SQL 有没有问题
- SQL 优化编译, 制定执行计划
- 执行SQL
- 性能区别 : 对于预编译 SQL, 在编译一次过后, 会将编译后的 SQL 语句缓存起来, 后面再次执行这条语句时, 不会重新编译 (只是替换参数), 省去了解析优化等过程, 因此可以提高性能
- SQL 注入 : 通过操作输入的数据来修改实现定义好的 SQL 语句, 以达到执行代码, 对服务器进行攻击. ${ } 存在 SQL 注入问题, #{ } 可以解决 SQL 注入问题.
${ } 的使用场景
- 排序 :
select * from user order by id ${ };
- 模糊匹配 :
like '%${key}%'
– 可以使用内置函数concat( )
替代- 分库分表传 表名
数据库连接池 – 池化技术
数据库连接池负责分配, 管理, 和释放数据库连接. 它允许应用程序重复使用一个现有的数据库连接, 而不是每次使用都要重新简历, 使用完毕就释放销毁.
作用 :
- 减少了网络开销
- 资源重用
- 提升了系统的性能
MyBatis 默认使用的数据库连接池是 Hikari .
数据库表和类对象的参数匹配
- SQL 语句中使用 as 起别名
- 结果映射
<resultMap id="userinfoMap" type="com.zrj.mybatisreview.model.UserInfo">
<id column="id" property="id"></id>
<result column="delete_flag" property="deleteFlag"></result>
<result column="create_tiem" property="createTime"></result>
<result column="update_time" property="updateTime"></result>
</resultMap>
<select id="queryAllUser" resultMap="userinfoMap">
select * from userinfo
</select>
- 配置文件中开启 驼峰命名
驼峰命名规则 : 表中字段: abc_xyz => 类对象属性 : abcXyz
mybatis:
configuration:
map-underscore-to-camel-case: true #配置驼峰⾃动转换
使用注释, 操作数据库
// 使用该注释获取数据库表中的主键, 并将其传输到 绑定类对象中的 keyProperty 属性中
@Options(useGeneratedKeys = true, keyProperty = "id")
@Select("Insert into user values(null, #{username}, #{password})")
public Integer insertOne(@Param("username") String username,@Param("password") String password);
@Delete("delete from user where id = #{id}")
public Integer delOne(int id);
@Select("select * from user order by id desc")
public List<User> selectAll();
@Update("update user set username = #{username} where id = #{id}")
public Integer updateOne(@Param("id") int id, @Param("username") String username);
使用 xml 文件, 操作数据库
<resultMap id="userinfoMap" type="com.zrj.mybatisreview.model.UserInfo">
<id column="id" property="id"></id>
<result column="delete_flag" property="deleteFlag"></result>
<result column="create_tiem" property="createTime"></result>
<result column="update_time" property="updateTime"></result>
</resultMap>
<select id="queryAllUser" resultMap="userinfoMap">
select * from userinfo
</select>
<insert id="insertUserInfo" useGeneratedKeys="true" keyProperty="userinfo.id">
insert into userinfo(username, password, age, gender, phone)
values(#{userinfo.username}, #{userinfo.password},
#{userinfo.age}, #{userinfo.gender},
#{userinfo.phone})
</insert>
<update id="updateOne">
update userinfo set username = #{username} where id = #{id}
</update>
<delete id="delOne">
delete from user where id = #{id}
</delete>
Mybatis 中动态标签
< if > 标签
<select id="one">
select
(username, password,
<if test="age != 0">
age,
</if>
gender, phone)
from user where id = #{id}
</select>
< trim > 标签
理论上其他标签都可以使用 trim 标签实现
<insert id="three" useGeneratedKeys="true" keyProperty="userinfo.id">
insert into userinfo
<trim prefix="(" suffix=")" suffixOverrides="," prefixOverrides=",">
<if test="username != null">
username,
</if>
<if test="password != null">
`password`,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides="," prefixOverrides=",">
<if test="username != null">
#{username},
</if>
<if test="password != null">
#{password},
</if>
</trim>
</insert>
< where > 标签
< where > 标签能够自动去除子句开头的
AND
或者OR
< where > 只会在子元素有内容的情况下才插入 where 子句
<select id="two">
select (username, password, age, gender, phone)
from user
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null">
and `username` = #{username}
</if>
<if test="password != null">
and `password` = #{password}
</if>
</where>
</select>
< set > 标签
< set > 标签能够自动删除 额外的逗号
<update id="four">
update userinfo
<set>
<if test="username != null">
username = #{username} ,
</if>
<if test="password != null">
`password` = #{password},
</if>
</set>
where id = 1
</update>
< foreach > 标签
<delete id="five">
delete from user where id in
<foreach collection="ids" open="(" close=")" separator="," item="e" >
#{e}
</foreach>
</delete>
< sql > 标签 & < include > 标签
< sql > 标签 : 定义可重用代码片段
< include > 标签 : 通过 refid 属性, 绑定要包含的代码片段
<select id="six1">
select username, password, age, gender, phone
from user
</select>
<sql id="e">
username, password, age, gender, phone
</sql>
<select id="six2">
select (
<include refid="e"></include>
)
from user
</select>