MyBatis--注解、缓存配置、其他工具

一、注解

前面熟悉了MyBatis的基本使用,针对一些简单的sql语句,如对一张表的增删改查,可以使用MyBatis提供的注解,省去写映射文件的步骤,复杂的多表查询,还是建议使用映射文件的方式

1. Select注解

使用Select注解可以实现数据库查询操作

实现根据部门编号查询部门

实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept implements Serializable {
    /**
     * 部门编号
     */
    private Integer deptno;
    /**
     * 部门名称
     */
    private String dname;
    /**
     * 地址
     */
    private String loc;
}

定义接口方法:

public interface DeptMapper {
    /**
     * 根据部门编号查询部门
     * @param deptno
     * @return
     */
    @Select("select * from dept where deptno = #{deptno}")
    Dept findDeptByDeptno(int deptno);
}

SQL语句中使用参数的方式和映射文件中相同,#{}的方式,也可以使用注解指定参数的名称

public interface DeptMapper {
    /**
     * 根据部门编号查询部门
     * @param deptno
     * @return
     */
    @Select("select * from dept where deptno = #{no}")
    Dept findDeptByDeptno(@Param("no") int deptno);
}

测试方法:

    //根据部门编号查询部门
    @Test
    public void test1() {
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = mapper.findDeptByDeptno(10);
        System.out.println(dept);
    }
2. Insert注解

实现新增一个部门信息

定义接口方法:

    /**
     * 新增一个部门信息
     *
     * @param dept
     * @return
     */
    @Insert("insert into dept values(#{deptno},#{dname},#{loc})")
    int addDept(Dept dept);

sql参数使用属性名

测试方法:

    //新增一个部门信息
    @Test
    public void test2() {
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = new Dept(50,"ALGORITHM","Paris");
        int rows = mapper.addDept(dept);
        System.out.println(rows);

        sqlSession.commit();
    }
3. Update注解

实现根据部门编号更新部门信息

定义接口方法:

    /**
     * 根据部门编号更新部门信息
     * @param dept
     * @return
     */
    @Update("update dept set dname = #{dname}, loc = #{loc} where deptno = #{deptno}")
    int updateDept(Dept dept);

测试方法:

    //根据部门编号更新部门信息
    @Test
    public void test3() {
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = new Dept(50,"ALGORITHM","Tokyo");
        int rows = mapper.updateDept(dept);
        System.out.println(rows);

        sqlSession.commit();
    }
4. Delete注解

实现根据部门编号删除部门

定义接口方法:

    /**
     * 根据部门编号删除部门
     * @param deptno
     * @return
     */
    @Delete("delete from dept where deptno = #{deptno}")
    int deleteDeptbyDeptno(int deptno);

测试方法:

    //根据部门编号删除部门
    @Test
    public void test4() {
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        int rows = mapper.deleteDeptbyDeptno(50);
        System.out.println(rows);

        sqlSession.commit();
    }

二、缓存配置

MyBatis自带一级缓存和二级缓存,还支持第三方缓存,如redis、ehcache

1. 一级缓存

一级缓存默认开启,存在于每个SqlSession对象中,对于完全相同的查询,如果一级缓存中存在,那么不会走数据库,直接返回

针对test1方法,通过sqlSession获取两个Mapper,并执行相同操作,最后看查询结果是否为同一对象:

    //根据部门编号查询部门
    @Test
    public void test1() {
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = mapper.findDeptByDeptno(10);
        System.out.println(dept);
        DeptMapper mapper2 = sqlSession.getMapper(DeptMapper.class);
        Dept dept2 = mapper2.findDeptByDeptno(10);
        System.out.println(dept2);

        System.out.println(dept == dept2);
    }

结果:

2. 二级缓存

二级缓存可以跨SqlSession,条件是由同一个SqlSessionFactory创建,二级缓存默认关闭,开启它需要以下步骤

  • 2.1 全局开关

在MyBatis核心配置文件中配置,支持二级缓存

    <settings>
...
        <!--开启二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
  • 2.2 在mapper中开启二级缓存
  • java中开启方式:
    在接口上使用CacheNamespace注解
@CacheNamespace
public interface DeptMapper {
  • 映射文件中开启方式:
    使用cache标签,并为需要开启缓存的查询设置useCache="true"
<mapper namespace="com.aruba.mapper.DeptMapper">
    <cache/>

<!--    Dept findDeptByDeptno(@Param("no") int deptno);-->
    <select id="findDeptByDeptno" resultType="dept" useCache="true">
        select * from dept where deptno = #{no}
    </select>
</mapper>

另外实体类需要实现序列化接口,因为有可能存储在磁盘中

  • 2.3 SqlSession的commit或close操作

SqlSession会首先去二级缓存中查找,如果不存在,就查询数据库,commit()或者close()的时候将数据放入到二级缓存

测试方法:

public class Test1 {
    private SqlSession sqlSession;
    private SqlSession sqlSession2;

    @Before
    public void init() throws IOException {
        SqlSessionFactoryBuilder sb = new SqlSessionFactoryBuilder();
        // 将配置文件作为参数传入
        SqlSessionFactory sqlSessionFactory = sb.build(Resources.getResourceAsStream("sqlMapConfig.xml"));
        sqlSession = sqlSessionFactory.openSession();
        sqlSession2 = sqlSessionFactory.openSession();
    }

    //根据部门编号查询部门 -- 二级缓存
    @Test
    public void test5() {
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = mapper.findDeptByDeptno(10);
        System.out.println(dept);
        sqlSession.commit();

        DeptMapper mapper2 = sqlSession2.getMapper(DeptMapper.class);
        Dept dept2 = mapper2.findDeptByDeptno(10);
        System.out.println(dept2);
    }

    @After
    public void release() {
        if (sqlSession != null)
            sqlSession.close();
        
        if (sqlSession2 != null)
            sqlSession2.close();
    }
}

结果:


只执行了一条sql

3. 第三方缓存

涉及分布式系统时,MyBatis自带的缓存就不能满足需求了,下面使用Ehcache作为第三方缓存

导入依赖:

        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.2</version>
        </dependency>

在resources目录下创建配置文件ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="D:\aruba\ehcache" />
    <defaultCache
            maxElementsInMemory="1000"
            maxElementsOnDisk="10000000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>

</ehcache>

二级缓存配置:

  • Java中:
    CacheNamespace注解,指定使用缓存的Class
@CacheNamespace(implementation = EhcacheCache.class)
public interface DeptMapper {
  • 映射文件中:
    cache标签,指定缓存的全类名
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

三、逆向工程

MyBatis官方提供了一个逆向工程,利用工具自动生成表对应的实体类、映射文件以及接口,并自动实现了一些基本的增删改查

1. 导入依赖:
        <!-- 代码生成工具jar -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
2. 在resources目录下新建generatorConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
           connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
           password="123">
        </jdbcConnection> -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
           NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.aruba.pojo"
                            targetProject=".\reverseMyBatis\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.aruba.mapper"
                         targetProject=".\reverseMyBatis\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.aruba.mapper"
                             targetProject=".\reverseMyBatis\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 指定生成的数据库表 -->
        <table tableName="dept" domainObjectName="Dept"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false" >
            <columnOverride column="id" javaType="Integer" />
        </table>

    </context>
</generatorConfiguration>
3. 运行下面代码:

需要手动修改下,编译后target目录下generatorConfig.xml的绝对路径

public class GeneratorSqlmap {
    public void generator() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;

        // 需要手动修改
        File configFile = new File("F:\\idelworkspace\\mybatis_study\\reverseMyBatis\\target\\classes\\generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);

    }

    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

生成结果:



自带了一些实现:

public interface DeptMapper {
    int deleteByPrimaryKey(Integer deptno);

    int insert(Dept record);

    int insertSelective(Dept record);

    Dept selectByPrimaryKey(Integer deptno);

    int updateByPrimaryKeySelective(Dept record);

    int updateByPrimaryKey(Dept record);
}

项目地址:

https://gitee.com/aruba/mybatis_study.git

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 230,247评论 6 543
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,520评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,362评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,805评论 1 317
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,541评论 6 412
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,896评论 1 328
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,887评论 3 447
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 43,062评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,608评论 1 336
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,356评论 3 358
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,555评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,077评论 5 364
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,769评论 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,175评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,489评论 1 295
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,289评论 3 400
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,516评论 2 379

推荐阅读更多精彩内容