天天看点

spring boot mybatis生成模型文件(generator)mybatis-generator:generate

今天说明一下spring boot mybatis整合后使用generator生成模型文件,其中包括三个文件:

说明:本文使用user_info数据库表做说明,前提是已经实现spring boot mybatis整合

1,【dao层接口文件】Mapper.java文件

2,【mapper.xml文件】mapper.xml文件

3,【model对象也就是javabean文件】bean.java文件

一,实现步骤:

pom.xml文件

1,在

<build>
       <plugins>
       </plugins>
</build>
           

中新增配置

<plugin>  
                <groupId>org.mybatis.generator</groupId>  
                <artifactId>mybatis-generator-maven-plugin</artifactId>  
                <version>1.3.5</version>  
                <dependencies>   
                    <dependency>  
                        <groupId>org.mybatis.generator</groupId>  
                        <artifactId>mybatis-generator-core</artifactId>  
                        <version>1.3.5</version>  
                    </dependency>  
                </dependencies>  
                <executions>  
                    <execution>  
                        <id>Generate MyBatis Artifacts</id>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>generate</goal>  
                        </goals>  
                    </execution>  
                </executions>  
                <configuration>  
                    <!--允许移动生成的文件 -->  
                    <verbose>true</verbose>  
                    <!-- 是否覆盖 -->  
                    <overwrite>true</overwrite>  
                    <!-- 自动生成的配置 -->  
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>  
            </plugin>  
           

2,在src/main/resources目录下新建xml配置文件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>
 		<!-- jdbc连接的mysql或者oracle的连接包jar,最好是配置为绝对路径 -->
     <classPathEntry
             location="F:\MavenRepository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar"/>
     <context id="my" targetRuntime="MyBatis3">
         <commentGenerator>
             <property name="suppressDate" value="false"/>
             <property name="suppressAllComments" value="true"/>
         </commentGenerator>
 		<!-- 配置数据源信息,多数据源需要配置多份文件,对应需要修改数据源信息,可以使不同主机的数据库,也可以是同一数据库服务器上的不同数据库名 -->
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password="root">    
        </jdbcConnection>   
 		<!-- 配置生成的model对象也就是javabean,对应数据库表属性的javabean生成到的包路径和绝对代码路径,只需要配置到工程的/src/main/java就可以了,具体的路径由包路径指定。
 			由于多数据源,所以我在model后面加了一层以示区分不同数据源的信息 -->
         <javaModelGenerator targetPackage="com.mycompany.mavenspringboot.model" targetProject="F:\SVNFile\mavenSpringBoot\model">
             <property name="enableSubPackages" value="true"/>
             <property name="trimStrings" value="true"/>
         </javaModelGenerator>
 		<!-- mapper.xml文件生成路劲和包名 -->
         <sqlMapGenerator targetPackage="mapper" targetProject="F:\SVNFile\mavenSpringBoot\model">
             <property name="enableSubPackages" value="true"/>
         </sqlMapGenerator>
 		<!-- dao层接口文件 -->
         <javaClientGenerator targetPackage="com.mycompany.mavenspringboot.mapper" targetProject="F:\SVNFile\mavenSpringBoot\model" type="XMLMAPPER">
             <property name="enableSubPackages" value="true"/>
         </javaClientGenerator>
 		<!-- 数据库里面的表名信息和配置,多张表需要配置多条信息 
                mybatis-generator:generate
                -->
            <table tableName="user_info" domainObjectName="UserInfo"
                enableCountByExample="false" enableUpdateByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false"
                selectByExampleQueryId="false">
            </table> 
     </context>
 </generatorConfiguration>
           

到这里,配置已经完成

下面我已netbeans8.0作为演示,生成结果(由于笔者使用Netbeans开发工具,其他开发工具可参照,因为此操作与开发工具影响不大)

3,点击pom.xml文件右键 ->运行Maven-->目标,然后出现

spring boot mybatis生成模型文件(generator)mybatis-generator:generate
spring boot mybatis生成模型文件(generator)mybatis-generator:generate
spring boot mybatis生成模型文件(generator)mybatis-generator:generate

在目标中输入,点击确定

spring boot mybatis生成模型文件(generator)mybatis-generator:generate
spring boot mybatis生成模型文件(generator)mybatis-generator:generate

结果已经生成在对象你设置的文件目录下。

spring boot mybatis生成模型文件(generator)mybatis-generator:generate

下面给出文件示例;

1,【dao层接口文件】UserInfoMapper.java文件

package com.mycompany.mavenspringboot.mapper;

import com.mycompany.mavenspringboot.model.UserInfo;

public interface UserInfoMapper {
    int deleteByPrimaryKey(Integer userid);

    int insert(UserInfo record);

    int insertSelective(UserInfo record);

    UserInfo selectByPrimaryKey(Integer userid);

    int updateByPrimaryKeySelective(UserInfo record);

    int updateByPrimaryKey(UserInfo record);
}
           

2,【mapper.xml文件】UserInfoMapper.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mycompany.mavenspringboot.mapper.UserInfoMapper">
  <resultMap id="BaseResultMap" type="com.mycompany.mavenspringboot.model.UserInfo">
    <id column="userid" jdbcType="INTEGER" property="userid" />
    <result column="loginname" jdbcType="VARCHAR" property="loginname" />
    <result column="loginpwd" jdbcType="VARCHAR" property="loginpwd" />
    <result column="nickname" jdbcType="VARCHAR" property="nickname" />
    <result column="status" jdbcType="TINYINT" property="status" />
  </resultMap>
  <sql id="Base_Column_List">
    userid, loginname, loginpwd, nickname, status
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user_info
    where userid = #{userid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user_info
    where userid = #{userid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.mycompany.mavenspringboot.model.UserInfo">
    insert into user_info (userid, loginname, loginpwd, 
      nickname, status)
    values (#{userid,jdbcType=INTEGER}, #{loginname,jdbcType=VARCHAR}, #{loginpwd,jdbcType=VARCHAR}, 
      #{nickname,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="com.mycompany.mavenspringboot.model.UserInfo">
    insert into user_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userid != null">
        userid,
      </if>
      <if test="loginname != null">
        loginname,
      </if>
      <if test="loginpwd != null">
        loginpwd,
      </if>
      <if test="nickname != null">
        nickname,
      </if>
      <if test="status != null">
        status,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userid != null">
        #{userid,jdbcType=INTEGER},
      </if>
      <if test="loginname != null">
        #{loginname,jdbcType=VARCHAR},
      </if>
      <if test="loginpwd != null">
        #{loginpwd,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.mycompany.mavenspringboot.model.UserInfo">
    update user_info
    <set>
      <if test="loginname != null">
        loginname = #{loginname,jdbcType=VARCHAR},
      </if>
      <if test="loginpwd != null">
        loginpwd = #{loginpwd,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        nickname = #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=TINYINT},
      </if>
    </set>
    where userid = #{userid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.mycompany.mavenspringboot.model.UserInfo">
    update user_info
    set loginname = #{loginname,jdbcType=VARCHAR},
      loginpwd = #{loginpwd,jdbcType=VARCHAR},
      nickname = #{nickname,jdbcType=VARCHAR},
      status = #{status,jdbcType=TINYINT}
    where userid = #{userid,jdbcType=INTEGER}
  </update>
</mapper>
           

3,【model对象也就是javabean文件】UserInfo.java文件

package com.mycompany.mavenspringboot.model;

public class UserInfo {
    private Integer userid;

    private String loginname;

    private String loginpwd;

    private String nickname;

    private Byte status;

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname == null ? null : loginname.trim();
    }

    public String getLoginpwd() {
        return loginpwd;
    }

    public void setLoginpwd(String loginpwd) {
        this.loginpwd = loginpwd == null ? null : loginpwd.trim();
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname == null ? null : nickname.trim();
    }

    public Byte getStatus() {
        return status;
    }

    public void setStatus(Byte status) {
        this.status = status;
    }
}
           

二,其中要注意的重点:

1,执行pom文件的命令已经在generatorConfig.xml配置文件中(防止自己拼写时候出现错误)

spring boot mybatis生成模型文件(generator)mybatis-generator:generate
spring boot mybatis生成模型文件(generator)mybatis-generator:generate

2,两类文件包名的设置,必须是您实际项目对应文件模型的包名,这样生成的xml文件才会正确。xml文件的包名可以随实际情况设置

spring boot mybatis生成模型文件(generator)mybatis-generator:generate
spring boot mybatis生成模型文件(generator)mybatis-generator:generate

3,生成文件路径:建议不要设置为您实际项目的项目路径(src/main/java),否则生成的文件会替换您原有的文件,如果原有的文件已经做过修改,那么就会倒是之前的修改不存在了

4,测试数据如下

DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
  `userid` int(10) NOT NULL AUTO_INCREMENT,
  `loginname` varchar(255) DEFAULT NULL,
  `loginpwd` varchar(255) DEFAULT NULL,
  `nickname` varchar(255) DEFAULT NULL,
  `status` tinyint(2) DEFAULT '0',
  PRIMARY KEY (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
           

继续阅读