mybatis的逆向工程

1、生成的思路:通过Java类和xml文件生成

2、需要的jar包

3、步骤

 a、mapper生成配置文件

     在generatorConfig.xml中配置mapper生成的详细信息,注意改下几点:

       1、  添加要生成的数据库表

       2、  po文件所在包路径

       3、  mapper文件所在包路径

<?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/mybaits" userId="root"
            password="admin">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
            userId="yycg"
            password="yycg">
        </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.wang.pojo"
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.wang.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.wang.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table tableName="orders"></table>
        <table tableName="user"></table>
        <!-- <table schema="" tableName="sys_user"></table>
        <table schema="" tableName="sys_role"></table>
        <table schema="" tableName="sys_permission"></table>
        <table schema="" tableName="sys_user_role"></table>
        <table schema="" tableName="sys_role_permission"></table> -->
        
        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>

   修改配置文件:jdbcConnection、javaModelGenerator、sqlMapGenerator、javaClientGenerator、指定数据库表

b、 使用java类生成mapper文件

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;


public class StartServer {
        public void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("generator.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 {
            StartServer startServer = new StartServer();
            startServer.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

}

 

记住:逆向工程生成的代码是追加,而且多不会显示,但是会导致代码无法运行

 c、 第三步:拷贝生成的mapper文件到工程中指定的目录中

     1、Mapper.xml

    Mapper.xml的文件拷贝至mapper目录内

     2、Mapper.java

    Mapper.java的文件拷贝至mapper 目录内

注意:mapper xml文件和mapper.java文件在一个目录内且文件名相同。

      逆向工程:(生成的都是对单表的操作)

 

生成的逆向工程分析:

以:user表为了例:生成了po类、po对应的 example类(其内部有内部类Criteria,由于查询等操作)、生成对单表操作的mapper.xml和对应的mapper接口。

测试:

通过主键获取:

    @Test
    public void testFindUserById() throws Exception{
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        
        User user = userMapper.selectByPrimaryKey(1);
        System.out.println(user);
    }

 

DEBUG [main] - ==>  Preparing: select id, username, birthday, sex, address from user where id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)

 

  是通过占位符的方式传入

通过名字和性别获取

    @Test
    public void testFindUserByNameAndSex() throws Exception{
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUsernameLike("%王%");
        criteria.andSexEqualTo("1");
        List<User> users = userMapper.selectByExample(example);
        System.out.println(users);
    }

 

注意:使用like的时候,是要自己写%

 

posted @ 2017-04-03 09:44  mslog  阅读(302)  评论(0编辑  收藏  举报