• mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件


    Mybatis的逆向工程非常简单,只要一个配置文件和一个Main方法就可以实现,下面以maven工程为例:

    (1)在pom.xml中引入依赖包

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

    (2)在 generatorConfig.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/xssm" userId="root" password="root">
            </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.xuebusi.xssm.pojo" targetProject=".srcmainjava">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- targetProject:mapper映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="com.xuebusi.xssm.mapper" targetProject=".srcmainjava">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            <!-- targetPackage:mapper接口生成的位置 -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.xuebusi.xssm.mapper" targetProject=".srcmainjava">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
            <!-- 指定数据库表,比如这里指定了数据库中的一张名为x_user的表,每一个table标签对应一张表 -->
            <table schema="" tableName="x_user"></table>
        </context>
    </generatorConfiguration>

    (3)编写Main方法,在main方法中指定要读取的generatorConfig.xml 配置文件的位置

    package com.xuebusi.xssm.common.generator;
    
    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;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * mybatis逆向工程
     * 根据配置生成mybatis的实体类、接口以及映射Mapper.xml文件
     */
    public class GeneratorMain {
    
        public void generator() throws Exception {
    
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            //指定 逆向工程配置文件
            File configFile = new File("src/main/java/com/xuebusi/xssm/common/generator/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 {
                GeneratorMain generatorSqlmap = new GeneratorMain();
                generatorSqlmap.generator();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }

    (4)运行main方法,可以生成4个文件,一个实体类,一个Example类,一个Mapper接口类,一个Mapper.xml映射文件

    其中Mapper接口类中是一些增删改查的方法,而Example类用来封装一些条件。

    具体使用方法举例:

    package com.xuebusi.xssm.service.impl;
    
    import com.xuebusi.xssm.mapper.XUserMapper;
    import com.xuebusi.xssm.pojo.XUser;
    import com.xuebusi.xssm.service.XUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * Created by SYJ on 2017/12/18.
     */
    @Service
    public class XUserServiceImpl implements XUserService {
    
        @Autowired
        private XUserMapper userMapper;
    
        /**
         * 根据id查询用户
         * @param id
         * @return
         */
        @Override
        public XUser selectByPrimaryKey(Integer id) {
            XUser user = userMapper.selectByPrimaryKey(id);
            return user;
        }
    
        /**
         * 添加用户
         * @param user
         * @return
         */
        @Override
        public int insert(XUser user) {
            return userMapper.insert(user);
        }
    
        /**
         * 查询所有用户
         * @return
         */
        @Override
        public List<XUser> findAll() {
            return userMapper.selectByExample(null);
        }
    }
  • 相关阅读:
    句柄
    类,方法,抽象方法,接口
    Enum类型
    String 为什么是不可变的
    大数据怎么排序
    oracle创建表空间 导入数据库
    eclipse常见小问题
    eclipse创建项目
    存储过程
    在VMware通过挂载系统光盘搭建本地yum仓库
  • 原文地址:https://www.cnblogs.com/jun1019/p/8073229.html
Copyright © 2020-2023  润新知