Springboot + Mybatis reverse engineering

Mybatis engineering, automatic generation of entity classes and methods used CRUD.

 

  • 1.pom.xml introduction of class
<! - function mapper common basis for deletions mabatis package change investigation ->
<dependency>
<the groupId> tk.mybatis </ the groupId>
<the artifactId> Boot-mapper-Spring-Starter </ the artifactId>
<Version> 2.1.5 </ Version>
<Exclusions>
<Exclusion>
<the groupId> org.springframework.boot </ the groupId>
<the artifactId> Boot-Starter-Spring-JDBC </ the artifactId> <-! been introduced this, it is here exceptions out its own default ->
</ Exclusion>
</ Exclusions>
</ dependency>
<-! reverse Engineering ->
<dependency>
<groupId> org.mybatis.generator </ groupId>
<artifactId> the mybatis -generator-core </ artifactId>
<version>1.3.5</version>
<scope>provided</scope>
</dependency>

 

  • 2. Profiles: \ src \ main \ resources \ resources \ 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>
    <properties resource="application.properties"></properties>

    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="D:\sqljdbc4.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <!--<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=gkzp" userId="sa" password="nsoft">
        </jdbcConnection>-->
        <jdbcConnection driverClass="${spring.datasource.druid.driverClassName}" connectionURL="${spring.datasource.druid.url}" userId="${spring.datasource.druid.username}" password="${spring.datasource.druid.password}">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.generator.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.generator.mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.generator.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <!--
        <table tableName="HR_Recruit_entryInfo_base"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Recruit_entryInfo_education"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Recruit_entryInfo_family"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Recruit_entryInfo_other"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Recruit_entryInfo_work"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

        <table tableName="Dept"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Post_Type"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Recurit_Plan_Needs"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="HR_Recurit_Plan_Return"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
      -->
        <table tableName="sys_user"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>
源代码

 

  • 3.配置Java类

 \src\main\java\com\nsoft\gkzp\system\sysuser\service\SysUserServiceImpl.java 

File configFile文件参数指定generatorConfig.xml配置文件位置  
package com.nsoft.gkzp.util;
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;

public class GeneratorSqlmap {
    public void generator() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("./src/main/resources/resources/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);
    }
    /**
     * 运行这个main方法就可以进行逆向工程
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行上面的main方法,就会自动生成三个文件(以sys_User表为例)(如下图)

create table sys_user(
    id              int                identity(1,1)  primary key not null,    --id主键 自增长
    loginName     nvarchar(50)    NOT NULL ,                                    --登录用户名
    password      nvarchar(50)    NOT NULL ,                                    --登录密码
    email          nvarchar(255)    ,                                        --用户邮箱    (邮箱登录)
    phone         nvarchar(15)  ,                                        --用户手机号(手机号登录)
    createTime    datetime default CONVERT(varchar(100), GETDATE(), 20),    --用户创建时间
    pwdChangeTime datetime default CONVERT(varchar(100), GETDATE(), 20),    --改密码时间(初始值为用户创建时间)
    code          varchar(255) ,                                            --'激活码'
    usertype      int            NOT NULL default 2,                            --用户类型(0系统管理员 1、内网用户 2、普通用户)    
    state          int           NOT NULL default 0 ,                        --用户激活状态:0表示未激活,1表示激活
    nstatusid      int default 1                                                --用户状态 0无效 1有效
);
表sys_user

另:SysUser实体类中,例如loginname字段均为小写的,如果表字段设计为login_name的话,生成的实体类此字段映射为loginName,就会大写。同理表名也是如此。

  • 4.实际应用

生成后,可以将

4.1  接口类SysUserMapper.java、映射文件UserMapper.xml 两个文件删除了,用不到。  实体类SysUser.java复制粘贴到你要放实际放的地方就可以了。

4.2  实际的dao层接口类继承   Mapper<com.generator.model.SysUser>     ,继承后,基础的增删改查操作mybatis已经自动生成并封装,不需要写了,这里只写其他业务逻辑方法就可以了。

   注意:1.这里<com.generator.model.SysUser>为你生成实体类存放位置,如你在4.1步骤将SysUser.java实体类复制粘贴到其他位置,那么这里就写新位置的路径

              2. 继承的Mapper为 tk.mybatis.mapper.common.Mapper。

在service实现类,就可以直接使用mybatis自动生成并封装的基础的增删改成操作,如下图。

 

 

Guess you like

Origin www.cnblogs.com/zdyang/p/11654188.html