mybatis动态sql_缓存_关系映射_注解

mybatis动态sql_缓存_关系映射_注解:

mybatis:

动态SQL:

if:

用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件。

<select id="sel" resultType="user"> 
    select * from t_user where 1=1 
    <if test="username != null and username != ''"> 
        and username=#{username} </if> 
    <if test="password != null and password != ''">
            and password=#{password} </if> 
</select>
 select * from emp
        <where>
            <if test="ename!=null and ename !=''">
                and ename=#{ename}
            </if>
            <if test="deptno!=0 and ename !=''">
                and deptno=#{deptno}
            </if>
        </where>
where标签:
  1. 如果没有条件, 不会生成 where 关键字

  2. 如果有条件, 会自动添加 where 关键字

  3. 如果第一个条件中有 and, 去除之

 select * from emp
        <where>
            <if test="ename!=null and ename !=''">
                and ename=#{ename}
            </if>
            <if test="deptno!=0 and ename !=''">
                and deptno=#{deptno}
            </if>
        </where>
choose…when…otherwise标签:

相当于java语言中的switch …case…default,when标签自带break

 select * from emp
        <where>
            <choose>
                <when test="ename!=null and deptno!=''" >
                    ename=#{ename}
                </when>
                <when test="deptno!=0 and deptno!=''">
                    deptno=#{deptno}
                </when>
                <otherwise></otherwise>
            </choose>
        </where>
set:

常用于更新语句中.

  1. 满足条件时, 会自动添加 set 关键字

  2. 会去除 set 子句中多余的逗号

  3. 不满足条件时, 不会生成 set 关键字

update emp
	<set>
       <if test="ename!=null and ename!=''" >
             ename=#{ename},
       </if>
       <if test="deptno!=0 and deptno!=''">
             deptno=#{deptno},
        </if>
            empno=#{empno}
    </set>
        where empno=#{empno}
trim:
  1. prefix, 在前面添加内容

  2. prefixOverrides, 从前面去除内容

  3. suwix, 向后面添加内容

  4. suwixOverrides, 从后面去除内容

     <select id="findTrim" resultType="Student">
            select * from student
            <trim prefix="where" prefixOverrides="jkl" suffix="and 1=1" suffixOverrides="opo">
            jkl
                <if test="sname!=null and sname !=''">
                    and sanme=#{sname}
                </if>
            opo
            </trim>
    </select>
    
bind:

用来进行模糊查询。

<select id="findByName" parameterType="String" resultType="Student">
     select * from student
    <where>
        <if test="sname!=null and sname!=''">
             <bind name="sname" value="'%'+sname+'%'"/>
                 sname like #{sname}
        </if>
     </where>
</select>
foreach:
  1. collection: 待遍历的集合

  2. open: 设置开始符号

  3. item: 迭代变量

  4. separator: 项目分隔符

  5. close: 设置结束符

select * from student where id in(
            <foreach collection="list" item="item" separator="," >
                #{item}
            </foreach>
        )
sql…include:

用于提取SQL语句的。

 <sql id="haha">sid,sname,s.cid,cname</sql>
    
     <select id="findAll" resultMap="student_map">
        select <include refid="haha"/> from student s join clazz c on s.cid=c.cid
    </select>

Mybatis缓存机制:

一级缓存:

在SqlSession生命周期中有效,

线程级别的缓存。

SqlSession关闭,缓存清空

​ 在同一个 SqlSession 中, Mybatis 会把执行的方法和参数通过算法生成缓存的键值, 将键值和结果存放在一个 Map 中, 如果后续的键值一样, 直接从 Map中获取数据;

​ 不同的 SqlSession 之间的缓存是相互隔离的;任何的 UPDATE, INSERT, DELETE 语句都会清空缓存。

用一个 SqlSession, 可以通过配置使得在查询前清空缓存flushCache=“true”

session.clearCache(); //session缓存刷新

 <select id="testFind" resultType="com.shsxt.pojo.Emp" flushCache="true">   //缓存刷新
二级缓存:

进程级别的缓存,SqlSessionFactory的缓存

在一个SqlSession生命周期中有效,可以在多个SqlSession生命周期中共享

默认关闭的,需要在使用的时候为某个命名空间

在session第一次关闭的时候进行缓存

<!--主配置文件中-->
<!--默认开启项目下的二级缓存-->
         <setting name="cacheEnabled" value="true"/>
    
mapper文件中:
     <!--开启二级缓存,要求实体类进行序列化-->
    <cache/>

列名和属性名不一致问题:

列别名:
select ename name from emp 
使用resultMap:
<resultMap id="student_map" type="student">
        <id column="sid" property="sid"/>     <!--主键-->
        <result column="sname" property="sname"/>
        <result column="age" property="age"/>
        <result column="cid" property="cid"/>
</resultMap>

关系映射查询:

一对一:

association:

 <resultMap id="student_map" type="student">
        <id column="sid" property="sid"/>     <!--主键-->
        <result column="sname" property="sname"/>
        <result column="age" property="age"/>
        <result column="cid" property="cid"/>
        <association property="clazz" javaType="Clazz">
        <id column="cid" property="cid"/>         <!--主键-->
        <result column="cname" property="cname"/>
        </association>
    </resultMap>
一对多:

collections:

<resultMap id="clazz_map" type="Clazz">
        <id property="cid" column="cid"/>
        <result property="cname" column="cname"/>
        <collection property="list" javaType="list" ofType="Student">
            <id property="sid" column="sid"/>
            <result property="sname" column="sname"/>
            <result property="age" column="age"/>
            <result property="cid" column="cid"/>
        </collection>
    </resultMap>

注解开发:

@select

​ 查询语句

 @Select("select * from emp")
List<Emp>testfind2();

@Insert

​ 插入语句

@Insert("insert into student values(#{sid},#{sname},#{age})")
    public int register(Student student);

@Update

​ 更新语句

@Update("Update student set sname=#{sname} where sid=#{sid}")
public int update(@Param("sname") String sname,@Param("sid") int sid);

@Delete

​ 删除语句

 @Delete("Delete student where sid=#{sid}")
    public int delete(Student student);

@results

​ 相对于xml中resultMap标签

@result

​ 相当于xml中resultMap的子标签id或result

@One:

​ 一对一映射

 @Select("select * from clazz")
    @Results(value = {
            @Result(property = "cid",column = "cid",id = true),
            @Result(property = "cname",column = "cname"),
            @Result(property = "studentList", many = @Many(select = "com.shsxt.mappers.StudentMapper.findByCid"),column = "cid")
    })
    public List<Clazz>findAll();

@Many:

​ 一对多映射

@Select("select sid,sname,cid from student ")
    @Results(value = {
            @Result(column = "sid", property = "sid",id = true),
            @Result(column = "sname",property = "sname"),
            @Result(column = "cid", property = "cid"),
            @Result(property = "clazz",one = @One(select = "com.shsxt.mappers.ClazzMapper.findById"), column="cid")

    })
    List<Student>findAll();

逆向工程:

mybatis-generator是一款mybatis自动代码生成工具,可以

通过配置,快速生成pojo,mapper和xml文件.

官方网址:http://mybatis.org/generator/configreference/ xmlconfig.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值