MyBatis

MyBatis

Mybaits 是一个优秀的 持久层 框架, 用于简化 JDBC 开发

简单的说 : Mabatis 是更简单完成程序和数据库交互的框架, 也就是更简单的操作和读取数据库的工具


#{ } & ${ }

#{ } : 预编译 SQL
${ } : 即时 SQL

SQL 的执行流程

  1. 解析语法, 校验 SQL 有没有问题
  2. SQL 优化编译, 制定执行计划
  3. 执行SQL
  1. 性能区别 : 对于预编译 SQL, 在编译一次过后, 会将编译后的 SQL 语句缓存起来, 后面再次执行这条语句时, 不会重新编译 (只是替换参数), 省去了解析优化等过程, 因此可以提高性能
  2. SQL 注入 : 通过操作输入的数据来修改实现定义好的 SQL 语句, 以达到执行代码, 对服务器进行攻击. ${ } 存在 SQL 注入问题, #{ } 可以解决 SQL 注入问题.

${ } 的使用场景

  1. 排序 : select * from user order by id ${ };
  2. 模糊匹配 : like '%${key}%' – 可以使用内置函数 concat( ) 替代
  3. 分库分表传 表名

数据库连接池 – 池化技术

数据库连接池负责分配, 管理, 和释放数据库连接. 它允许应用程序重复使用一个现有的数据库连接, 而不是每次使用都要重新简历, 使用完毕就释放销毁.

作用 :

  1. 减少了网络开销
  2. 资源重用
  3. 提升了系统的性能

MyBatis 默认使用的数据库连接池是 Hikari .


数据库表和类对象的参数匹配

  1. SQL 语句中使用 as 起别名
  2. 结果映射
<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>

在这里插入图片描述

  1. 配置文件中开启 驼峰命名
    驼峰命名规则 : 表中字段: 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>

  • 26
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值