11、MyBatis的逆向工程

使用官方网站的Mapper自动生成工具mybatis-generator-core-1.3.2来生成类和mapper映射文件。
链接:https://pan.baidu.com/s/1nuYaYST 密码:ur5m
将generatorSqlmapCustom导入工作空间。


在这里我们使用java工程生成逆向工程:

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 GeneratorSqlmap {

    public void generator() throws Exception{

        List warnings = new ArrayList();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("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();
        }

    }

}

这些代码在官方网站都有:
http://www.mybatis.org/generator/running/runningWithJava.html

上面这段java代码我们不需要去管,重点看generatorConfig.xml:




<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
            password="mysql">
        jdbcConnection>
        

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="cn.itcast.ssm.po"
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="cn.itcast.ssm.mapper" 
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.itcast.ssm.mapper" 
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        <table schema="" tableName="user">table>
        <table schema="" tableName="orders">table>
        <table schema="" tableName="items">table>
        <table schema="" tableName="orderdetail">table>

        
    context>
generatorConfiguration>

将上面的配置文件修改适合自己的参数,运行程序:
11、MyBatis的逆向工程_第1张图片

测试逆向工程

工程中的环境,诸如sqlmapconfig.xml、applicationContext.xml可以参考前面一篇:
10、MyBatis与Spring的整合
使用扫描包的方式配置mapper接口:


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="cn.xpu.hcp.mapper"/>
bean>

测试插入:

@Autowired
@Qualifier("userMapper")
private UserMapper userMapper;

@Test
public void testInsert() {
    User user = new User();
    user.setUsername("曹操");
    user.setSex("1");
    user.setBirthday(new Date());
    user.setAddress("成都市");
    userMapper.insert(user);
}

11、MyBatis的逆向工程_第2张图片

根据id查询:

@Test
public void testSelectById(){
    User user = userMapper.selectByPrimaryKey(1);
    System.out.println(user.getUsername()+"\t"+user.getSex()+"\t"+user.getBirthday());
}

11、MyBatis的逆向工程_第3张图片

测试模糊查询:

@Test
public void testSelectByLike(){
    //创建user对象的扩展类,设置查询条件
    UserExample userExample = new UserExample();
    //我们可以进行链式编程,添加任何我们希望的条件
    userExample.createCriteria().andUsernameLike("%王%");

    //查询数据
    List list = userMapper.selectByExample(userExample);
    for (User user : list) {
        System.out.println(user);
    }
}

这里写图片描述

注意:

  • 如果我们修改了数据库表,那么我们就需要重新生成逆向工程;
  • 逆向工程生成的代码只能做单表查询;
  • 我们不能在生成的代码上进行扩展;
  • 一张表生成四个文件。

你可能感兴趣的:(mybatis,逆向工程,mybatis教程)