08 逆转工程

逆转工程

MyBatis Generator:
• 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
• 官方文档地址
http://www.mybatis.org/generator/
• 官方工程地址
https://github.com/mybatis/generator/releases

层次结构

mbg.xml放在最外面

MBG使用

使用步骤:

– 1)编写MBG的配置文件(重要几处配置)
1)jdbcConnection配置数据库连接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator 配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table 配置要逆向解析的数据表
tableName:表名
domainObjectName:对应的javaBean名

– 2)运行代码生成器生成代码

注意:

Context标签
targetRuntime=“MyBatis3“可以生成带条件的增删改查
targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问
题。

MBG配置文件

<?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>
	    <!-- targetRuntime="MyBatis3Simple":生成简单版的CRUD
                        MyBatis3:豪华版

    <context id="DB2Tables" targetRuntime="MyBatis3Simple">
        <!--jdbcConnection:指定如何连接到目标数据库-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!--类型解析器 -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--javaModelGenerator:指定javaBean的生成策略
            targetPackage="test.model":目标包名
            targetProject="\MBGTestProject\src":目标工程
        -->
        <javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--sqlMapGenerator:sql映射生成策略
         -->
        <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"  targetProject=".\conf">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--javaClientGenerator:指定mapper接口所在的位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao"  targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 指定要逆向分析哪些表:根据表要创建javaBean-->
        <table tableName="tbl_dept" domainObjectName="Department"></table>
        <table tableName="tbl_employee" domainObjectName="Employee"></table>
    </context>

</generatorConfiguration>

生成器代码

    public void test01() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("mbg.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);
    }

测试查询

    @Test
    public void testSimple() throws IOException {
        String resources = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resources);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession openSession = sqlSessionFactory.openSession(true);

        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            //xxxExample就是封装查询条件的
            //1、查询所有
            //List<Employee> emps = mapper.selectByExample(null);

            //2、查询员工名字中e字母的 和员工性别是1 的
            //封装员工查询条件的example
            EmployeeExample example = new EmployeeExample();
            //创建一个criteria 这个criteria就是拼装查询条件
            //select id, last_name, gender, email, d_id from tbl_employee
            // WHERE ( last_name like ? and gender = ? ) or email like %e%   再加上个or
            EmployeeExample.Criteria criteria = example.createCriteria();
            criteria.andLastNameLike("%e%");
            criteria.andGenderEqualTo("1");

            EmployeeExample.Criteria criteria2 = example.createCriteria();
            criteria2.andEmailLike("%e%");
            //and 放在一个criteria  or 的 创建新的criteria
            example.or(criteria2);

            List<Employee> employees = mapper.selectByExample(example);
            for (Employee e: employees){
                System.out.println(e);
            }

        } finally {
            openSession.close();
        }
    }

posted @   flypiggg  阅读(25)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示