DAY15 分页、注解开发、lombok、动态sql、缓存

一、分页,一种是在sql中实现(推荐使用),一种是在java中实现

<select id="getUserByLimit" parameterType="map" resultMap="user">
    select * from `user` limit #{startIndex},#{pageSize}
</select>
@Test
    public void test2(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper userDao = sqlSession.getMapper(UserMapper.class);
    HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
    stringIntegerHashMap.put("startIndex",0);
    stringIntegerHashMap.put("pageSize",2);
    List<User> userByLimit = userDao.getUserByLimit(stringIntegerHashMap);
    for (User user : userByLimit) {
        System.out.println(user);
    }
    sqlSession.close();
}

二、注解开发

@Select("select * from user where id = #{id}")
User getUserById(@Param("id")  int id);

@Param("id") 引用类型的参数或String类型,需要加上,引用类型不需要加,两个以上的基本类型必须加,

即使用这种注解,在config中也要绑定mapper,class等于接口

<mappers>
    <mapper class="com.rzgsl.dao.UserMapper"/>
</mappers>

不绑定不行。congfig 中绑定了谁,才会扫描谁

三、lombok

@Data,放在类上,作用在所有属性上,放在属性上,作用在该属性上

不支持构造器重载,需要手动添加。

缺点:改变了java语言的书写,减少了本该存在的东西,不便于阅读,如果到处充斥着这种插件,会极大降低java语言的严谨性,迟早被其他语言取代。

四、studentMapper接口和studentMapper.xml存放问题

接口放在java文件夹下的dao文件夹,xml放在resource文件夹下的dao文件夹,在config配置mapper中,填写dao/studentMapper.xml,注意:resource文件夹下也应该能展开,如果展不开,一级一级的建,不要加.一次建完,生成的target文件,会自动把接口和xml文件放在同一个包里。

五、多对一  association 社团 property 财物

方法1、子查询

<select id="getStudent" resultMap="StudentTeacher">
    select * form student
</select>
<resultMap id="StudentTeacher" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher" />
</resultMap>
    <select id="getTeacher" resultMap="Teacher">
        select * from teacher where id = #{tid}
    </select>

方法2、连表查询  推荐使用

<select id="getStudent" resultMap="StudentTeacher">
    select s.id sid,s.name sname,t.name tname
    from student s, teacher t
    where s.tid = t.id
</select>
<resultMap id="StudentTeacher" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>

六、一对多

方法一、

<select id="getTeacher" resultMap="TeacherStudent">
    select * from teacher where id = #{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
    <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentById" column="id"/>
</resultMap>
<select id="getStudentById" resultType="Student">
    select * from student where tid = #{tid}
</select>

方法二、

<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid = t.id and t.id=#{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="student" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>

七、动态sql,本质动态sql还是sql语句,只是增加一个逻辑代码,根据不同的条件生成不同的sql语句

1、if(where标签,可以自动去除第一个可以插入的and)

<select id="queryBlogIf" parameterType="map" resultType="blog">
    select * from blog where 1=1
    <if test="title != null">
        and title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</select>
<select id="queryBlogIf" parameterType="map" resultType="blog">
    select * from blog
    <where>
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </where>
</select>

2、choose(when,otherwise)和java的 swich case类似

</select>
    <select id="queryBlogChoose" parameterType="map" resultType="blog">
        select * from blog
        <where>
            <choose>
                <when test="title != null">
                    title = #{title}
                </when>
                <when test="author != null">
                    author = #{author}
                </when>
                <otherwise>
                    and views = #{views}
                </otherwise>
            </choose>
        </where>      
    </select>

3、trim(where,set)set会拼接set 语句,并且删除掉无关的逗号

 

4、foreach

5、SQL片段   sql标签

把相同的sql提取出来,用的地方再includ引用

<sql id="when-title-author">
    <choose>
        <when test="title != null">
            title = #{title}
        </when>
        <when test="author != null">
            author = #{author}
        </when>
        <otherwise>
            and views = #{views}
        </otherwise>
    </choose>
</sql>

 

<select id="queryBlogChoose" parameterType="map" resultType="blog">
    select * from blog
    <where>
       <include refid="when-title-author"></include>
    </where>
</select>

七、缓存,一级缓存默认开启(sqlsession级别,一个用户一个缓存),二级缓存需要设置(一级缓存丢出来的东西给二级缓存,二级缓存不同的sqlSession都可以使用,前提是要在同一个namespace,也就是Mapper.xm中才有效)

注意事项:在mybatis的paramaType类型,是与java不同的,比如_int表示int。在接口中要把形参前面加上@param(这是规范)

1、一级缓存:测试步骤:1、开启日志   2、查询两次相同的数据   3、

缓存失效的前提:1、查询不同的东西,2、增删改操作,3、手动清理

一级缓存相当于一个map,用的时候取

2、二级缓存:开启步骤1、在config.xml中开启二级缓存

<setting name="cacheEnabled" value="true"/>

2、在当前Mapper.xml中增加<cache/>,就开启了。

容易出现问题:报错,NotSerializableException,没有序列化接口。需要先将实体类 implements Serializable(也是规范要求)

结论:所有的数据都会先放到一级缓存中,只有session提交或者失效后,才会转到二级缓存中

用户新建查询任务,先查二级,再查一级,再查数据库

3、自定义缓存 Ehcache(现在用redis做缓存,了解即可,实际不会用)

1、导包  2、<cache type="">引用  3、配置缓存的xml文件,设置策略  4、实现cache接口

redis缓存就是K V键值对保存

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值