mybatis 3.5.0版本(四)

mybatis 3.5.0版本(一)

mybatis 3.5.0版本(二)

mybatis 3.5.0版本(三)

mybatis 3.5.0版本(四)

demo地址

逆向工程

导包

afd1f29324893b14584a1c7dca65cd27c63.jpg

创建 generator.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="mysqlTables" targetRuntime="MyBatis3">

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8"
                        userId="root"
                        password="root123">
        </jdbcConnection>

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

        <javaModelGenerator targetPackage="com.company.model" targetProject="./src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.company.mapper" targetProject="./src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.company.mapper" targetProject="./src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="user" domainObjectName="User"></table>
        <table tableName="orderdetail" domainObjectName="OrderDetail"></table>

    </context>
</generatorConfiguration>

执行代码

        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        File configFile = new File("src/generator.xml");
        ConfigurationParser configurationParser = new ConfigurationParser(warnings);
        Configuration configuration = configurationParser.parseConfiguration(configFile);
        DefaultShellCallback defaultShellCallback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, defaultShellCallback, warnings);
        myBatisGenerator.generate(null);

自动生成的文件

0b8749615340f4c62d78811298d8746e6b7.jpg

测试

    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
    UserExample userExample = new UserExample();
    Criteria c = userExample.createCriteria();
    c.andNameLike("%张%");
    List<Items> list = userMapper.selectByExample(userExample);

 

转载于:https://my.oschina.net/gwlCode/blog/3038419

版权声明:本文为weixin_34111819原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34111819/article/details/91893476