美文网首页
mybatis逆向工程

mybatis逆向工程

作者: Specime | 来源:发表于2018-02-24 10:57 被阅读0次

    依赖包

     <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.1.0</version>
        </dependency>
    
        <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-core</artifactId>
          <version>1.3.5</version>
        </dependency>
    

    Java代码

    public class MBG {
    
        public static void main(String[] args) throws Exception {
            List<String> warnings = new ArrayList<>();
            boolean overwrite = true;
            File configFile = new File("mbg.xml");//xml文件放在src目录下
            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);
        }
    }
    
    • 注意:如果是多module的父子工程,xml文件路径需要指定module名。
      如:File configFile = new File("module1/src/mbg.xml");
      包括xml文件内容指定生成代码路径时也要加上module名,在下面给出的示例中把 .\ 换成module名即可

    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/db_name" userId="mysql"
                            password="password">
            </jdbcConnection>
          <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:db_name"
                userId="oracle"
                password="password">
            </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.java.common.po"
                                targetProject=".\src\main\java">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- targetProject:mapper映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="com.java.common.mapper"
                             targetProject=".\src\main\java">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            <!-- targetPackage:mapper接口生成的位置 -->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.java.common.dao"
                                 targetProject=".\src\main\java">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
            <!-- 指定数据库表 -->
            <table schema="" tableName="u_permission"></table>
            <table schema="" tableName="u_role"></table>
            <table schema="" tableName="u_role_permission"></table>
            <table schema="" tableName="u_user"></table>
            <table schema="" tableName="u_user_role"></table>
    
            <!-- 有些表的字段需要指定java类型
             <table schema="" tableName="">
                <columnOverride column="" javaType="" />
            </table> -->
        </context>
    </generatorConfiguration>
    

    生成Example的使用

    @Test  
        public void testFindUserByName(){  
      
            //通过criteria构造查询条件  
            UserExample userExample = new UserExample();  
            userExample.setOrderByClause("username asc"); //asc升序,desc降序排列  
            userExample.setDistinct(false); //去除重复,true是选择不重复记录,false反之  
            UserExample.Criteria criteria = userExample.createCriteria(); //构造自定义查询条件  
            criteria.andUsernameEqualTo("张三");  
      
            //自定义查询条件可能返回多条记录,使用List接收  
            List<User> users = userMapper.selectByExample(userExample);  
      
            System.out.println(users);  
        }  
    

    说明

    Mybatis逆向工程会生成实例及实例对应的example(用于添加条件,相当于where后的部分)  
    xxxExample example = new xxxExample();  
    Criteria criteria = example.createCriteria();  
    方法说明:  
    // 1.添加升序排列条件,DESC为降序  
    example.setOrderByClause("字段名ASC")  
    // 2.去除重复,boolean类型,true为选择不重复的记录  
    example.setDistinct(false)  
    // 3.添加字段xxx为null的条件  
    criteria.andXxxIsNull  
    // 4.添加字段xxx不为null的条件  
    criteria.andXxxIsNotNull  
    // 5.添加xxx字段等于value条件  
    criteria.andXxxEqualTo(value)  
    // 6.添加xxx字段不等于value条件  
    criteria.andXxxNotEqualTo(value)  
    // 7.添加xxx字段大于value条件  
    criteria.andXxxGreaterThan(value)  
    // 8.添加xxx字段大于等于value条件  
    criteria.andXxxGreaterThanOrEqualTo(value)  
    // 9.添加xxx字段小于value条件  
    criteria.andXxxLessThan(value)  
    // 10.添加xxx字段小于等于value条件  
    criteria.andXxxLessThanOrEqualTo(value)  
    // 11.添加xxx字段值在List  
    criteria.andXxxIn(List)  
    // 12.不添加xxx字段值在List  
    criteria.andXxxNotIn(List)  
    // 13.添加xxx字段值在之间  
    criteria.andXxxBetween(value1,value2)  
    // 14.添加xxx字段值不在之间  
    criteria.andXxxNotBetween(value1,value2)  
    

    在指定数据库表时添加如下属性则不生成Example代码

     <table tableName="u_user" schema="" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
                   enableSelectByExample="false" selectByExampleQueryId="false" >
                   </table>
    

    相关文章

      网友评论

          本文标题:mybatis逆向工程

          本文链接:https://www.haomeiwen.com/subject/vokqxftx.html