SSM框架代码自动生成

项目目录

这里写图片描述
这里写图片描述

主要用到两个文件:CodeGeneratorUtil.java \ mbg.xml

CodeGeneratorUtil.java

package sei.util;
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 CodeGeneratorUtil {
    public static void main(String[] args) throws Exception{
        List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("mbg.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);
           System.out.println("运行完成");
    }

}

mbg.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="DB2Tables" targetRuntime="MyBatis3">

    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://bj-cdb-go7o8rhh.sql.tencentcdb.com:7275/newmydb"
        userId="root"
        password="WSbfCKDRJ7BzNs9A">
    </jdbcConnection>

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

    <javaModelGenerator targetPackage="sei.pojo" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="sei.mapping"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="sei.dao"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <table schema="DB2ADMIN" tableName="addr_batch" domainObjectName="AddrBatch"></table> 

  </context>
</generatorConfiguration>

我每次自动生成表需要修改的文件及内容记录一下

SystemHasUser.java

//部分代码
    private Integer countyId;

    private County county;

    private City city;       

    private User user;

    private LoginRemark loginRemark;

    private System system;

    private Company company;

    private DataTable userStatus; 

    public DataTable getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(DataTable userStatus) {
        this.userStatus = userStatus;
    }

SystemHasUserExample.java

protected Integer start;
    protected Integer limit;

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

SystemHasUserMapper.java

List<Long> selectIdByExample(SystemHasUserExample example);

SystemHasUserMapper.xml

// 只截取了部分代码
<association property="userStatus" column="userStatus_id" select="sei.dao.DataTableMapper.selectUserStatusByCode"></association>
    <association property="company" column="Id" select="sei.dao.CompanyMapper.selectByPrimaryKey"></association>
    <association property="county" column="county_id" select="sei.dao.CountyMapper.selectByPrimaryKey"></association>
    <association property="city" column="city_id" select="sei.dao.CityMapper.selectByPrimaryKey"></association>
    <association property="user" column="User_Id" select="sei.dao.UserMapper.selectByPrimaryKey"></association>
    <association property="system" column="System_Id" select="sei.dao.SystemMapper.selectByPrimaryKey"></association>
    <association property="loginRemark" column="remark_id" select="sei.dao.LoginRemarkMapper.selectByPrimaryKey"></association>


<if test="start != null and limit !=null and limit!=0">
     limit #{start},#{limit}
    </if>
    <if test="start == null and limit !=null and limit!=0">
     limit #{limit}
    </if>

useGeneratedKeys="true" keyColumn="Id" keyProperty="id"

<select id="selectIdByExample" resultType="long">
   select
    <if test="distinct" >
      distinct
    </if>
    id
    from system_has_user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
发布了15 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/BrotherBear2008/article/details/79784114