Spring整合MyBatis

MyBatis:优秀的持久层框架,用于简化JDBC开发
持久层
1.负责将数据保存到数据库的那一层代码
2.javaEE三层架构:表现层,业务层,持久层
JDBC缺点
1.硬编码
注册驱动
获取连接
2.操作繁琐
手动设置参数
手动封装结果集请添加图片描述
MyBatis简化
请添加图片描述
MyBatis快速入门

查询user表中所有数据
请添加图片描述
1.根据数据库写出对应的实体类(为实体类写出get和set方法)
2.写好MyBatis配置文件(连接数据库的相关信息)
3.写好实体类的映射文件(实体类对应的sql操作)
4.将配置文件中写入实体类的映射文件
5.返回需要的结果集

Mapper代理开发
请添加图片描述
1.创建代理接口
2.将代理文件放入对应路径资源目录下
3.可简化mapper在配置文件中的引入
4.将mapper接口配置文件命名空间改为接口路径
5.通过sqlSession通过实体接口获取mapper
6.通过获取的mapper进行数据库操作

MyBatis核心配置文件
environment:数据源(可以连接不同的数据库)

    <environments default="development"> 这里是默认加载的数据源id
        <environment id="development">  这里是数据源id,可以创建多个
            <transactionManager type="JDBC"/> 事务的管理方式(不用改)之后会被Spring接管
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>

配置数据库连接环境信息,可以配置多个environment,通过default属性切换到不同的environment

MyBatis可以给实体类起别名

<typeAliases>
        <package name="com.example.demo.pojo"/>
    </typeAliases>

在这个类路径下的所有包的实体类都可以直接写类名,不需要带包名
要注意标签顺序

查询结果映射

    <resultMap id="brandResultMap" type="Brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>

    <select id="selectAll" resultMap="brandResultMap">
        select * from tb_brand;
    </select>

避免使用sql片段进行麻烦的操作
id:完成主键字段的映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射

带参数的查询

    <select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand where id = #{id}
    </select>

通过#{}来获取id对应函数的参数
而${}会通过拼参数,不安全,有sql注入的问题

参数传递的时候:#{}
表名或者列名不固定的情况下:${}会存在sql注入的问题

特殊字符处理
1.转义字符
2.CDATA区

<select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand where id <![CDATA[
            <
        ]]> #{id}
    </select>

条件查询
1.散装参数:如果方法中有多个参数,需要使用@Param(“SQL参数占位符”)
2.对象参数
3.map集合参数请添加图片描述

查询-多条件-动态条件查询

if条件判断(test逻辑表达式)
问题:

    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where
        <if test="status != null">
            status = #{status}
        </if>
        <if test="companyName != null and companyName != ''">
            and company_name like #{companyName}
        </if>
        <if test="brandName != null and brandName != ''">
            and brand_name like #{brandName}
        </if>
    </select>

如果注释掉第一个参数,第二个参数就是以and开头,就会出错
问题(解决方式):
1.使用1=1恒等式,后面所有条件加上and
2.替换where关键字

查询-单条件-动态条件查询
where标签有条件就加上,无条件自动删去where,where第一个条件中有and自动去掉
choose标签(when和otherwise)

<where>
            <choose>
                <when test="status!=null">
                    status = #{status}
                </when>
                <when test="companyName != null and companyName != ''">
                    company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName != ''">
                    brand_name like #{brandName}
                </when>
            </choose>
        </where>

添加
openSession默认开启事务,需要手动提交
通过sqlSession.commit()
openSession(true):可以设置为自动提交事务(关闭事务)

主键返回

 <insert id="add" useGeneratedKeys="true" keyProperty="id">

keyProperty:指向id的属性的名称
useGeneratedKeys=“true” keyProperty=“id”
useGeneratedKeys设置为 true 时,表示如果插入的表id以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键id返回。
useGeneratedKeys参数只针对 insert 语句生效,默认为 false;

修改全部字段

    <update id="update">
        update tb_brand
        set brand_name=#{brandName},
        company_name=#{companyName},
        ordered=#{ordered},
        description=#{description},
        status=#{status}
        where id = #{id}
    </update>

修改动态字段

    <update id="update">
        update tb_brand
        set 
        <if test="brandName != null and brandName != ''">
            brand_name=#{brandName},
        </if>
        <if test="companyName != null and companyName != ''">
            company_name=#{companyName},
        </if>
        <if test="ordered != null and ordered != ''">
            ordered=#{ordered},
        </if>
        <if test="description != null and description != ''">
            description=#{description},
        </if>
        <if test="status != null and status != ''">
            status=#{status}
        </if>
        where id = #{id}
    </update>

问题
1.所有的if标签不存在
2.最后一个if标签不存在,就会多出一个逗号
解决方法
set标签

删除
删除一个

    <delete id="deleteById">
        delete from tb_brand where id = #{id}
    </delete>

批量删除

    <delete id="deleteByIds">
        delete from tb_brand where id
        in
            <foreach collection="ids" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
    </delete>

mybatis会将数组封装为一个map集合
1.array=数组
2.使用@Param改变map集合默认的key名称

MyBatis参数传递
单个参数
1.POJO类型:直接使用,属性名的参数占位符名称一致
2.Map集合:直接使用,键名和参数占位符名称一致
3.Collection:封装为Map集合
map.put(“agr0”,collection集合)
map.put(“collection”,collection集合)
4.List
map.put(“agr0”,list集合)
map.put(“collection”,list集合)
map.put(“list”,list集合)
5.Array
map.put(“arg0”,array数组)
map.put(“array”,array数组)
6.其他类型:直接使用
多个参数:封装为Map集合
高版本idea会自动加入配置文件,这样就不需要加入@Param,可以通过变量名找到对应值
@Param注解,替换Map集合中默认的arg键名

注解完成增删改查
注解完成简单功能
配置文件完成复杂功能

spring整合mybatis
主要整合MyBatis的核心配置文件

MyBatis演变
xml配置文件中的mapper标签,配置扫描配置最终实现的自动代理的形式实现的dao的实现类的

一开始只需要一个mapper.xml配置文件和mybatis.xml配置文件
通过sqlSession.selectList(“test.selectAll”);来调用test为命名空间,selectAll为对应id
之后使用代理开发的方式,创建相应的mapper.xml对应的mapper接口,通过sqlSession获取mapper代理对象来调用方法
再之后使用注解开发,只需要mapper的接口,不需要mapper.xml和配置文件中对mapper文件路径的配置
在spring整合mybatis之前一直需要xml配置文件
spring整合mybatis之后使用mybatiscongig.class类来取代xml配置文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值