Spring+SpringMVC+Mybatis(开发必备技能)04、mybatis自动生成mapper_dao_model(包含工具与视频讲解) 纯绿色版本、配套使用视频,100%运行成功

2022-11-30 16:33:18 浏览数 (1)

Spring SpringMVC Mybatis(开发必备技能)

04、mybatis自动生成mapper_dao_model(包含工具与视频讲解) 纯绿色版本、配套使用视频,100%运行成功

百度网盘链接:链接:https://pan.baidu.com/s/11cO4roAQ108D9E06IHrmGw 提取码:h63e

视频教程链接:

common_utils配套使用视频_哔哩哔哩_bilibili

目录

1、配置文件:

2、脚本文件:

3、执行效果:

生成mapper.xml文件示例:

生成model文件示例:

生成dao层文件示例:


1、配置文件:

其中修改数据库账号密码和链接就行

代码语言:javascript复制
<?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>
	<!-- 数据库驱动包位置 -->
	<classPathEntry location="mysql-connector-java-5.1.24-bin.jar"/>
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<!-- 生成的代码注释,加入下面的配置控制 false:生成、true:不生成 -->
		<commentGenerator>
			<property name="suppressAllComments" value="false"/>
			<property name="suppressDate" value="false"/>
		</commentGenerator>
		
		<!-- 数据库连接URL、用户名、密码 -->
		<!-- Oracle连接 
		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
			userId="tiger" password="123456">
		</jdbcConnection>
		-->
		<!-- MySQL连接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" 
			userId="root" password="root">
		</jdbcConnection>
		
		<!-- java类型处理器 -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
		
		<!-- Model位置 -->
		<javaModelGenerator targetPackage="main.java.com.item.model" targetProject="D:laoshifu_hongmuxiangxun">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		
		<!-- mapper位置 -->
		<sqlMapGenerator targetPackage="main.resources.mapper" targetProject="D:laoshifu_hongmuxiangxun">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		
		<!-- dao位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="main.java.com.item.dao" targetProject="D:laoshifu_hongmuxiangxun">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		
		<!-- %代表生成所有的表,如果只想生成某表,自己改成表名字就行 -->
		<table tableName="%" schema="SCOTT" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" ></table>
	</context>
</generatorConfiguration>

2、脚本文件:

代码语言:javascript复制
chcp 65001
echo off
title AutoMybatis_laoshifu
mkdir D:laoshifu_hongmuxiangxun
cls
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
start D:laoshifu_hongmuxiangxun
echo 如果生成正确,代码会生成在D盘的根目录下,结构为,java/com与java/resources可以直接复制,看到successfully代表生成完毕,按任意键结束。
pause

执行文件:

3、执行效果:

生成并打开对应生成文件夹位置:

生成mapper.xml文件示例:

代码语言:javascript复制
<?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="main.java.com.item.dao.UsersMapper" >
  <resultMap id="BaseResultMap" type="main.java.com.item.model.Users" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="createDate" property="createdate" jdbcType="TIMESTAMP" />
    <result column="userName" property="username" jdbcType="VARCHAR" />
    <result column="introduce" property="introduce" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    id, createDate, userName, introduce
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    select 
    <include refid="Base_Column_List" />
    from users
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    delete from users
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="main.java.com.item.model.Users" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    insert into users (id, createDate, userName, 
      introduce)
    values (#{id,jdbcType=INTEGER}, #{createdate,jdbcType=TIMESTAMP}, #{username,jdbcType=VARCHAR}, 
      #{introduce,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="main.java.com.item.model.Users" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    insert into users
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="createdate != null" >
        createDate,
      </if>
      <if test="username != null" >
        userName,
      </if>
      <if test="introduce != null" >
        introduce,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="createdate != null" >
        #{createdate,jdbcType=TIMESTAMP},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="introduce != null" >
        #{introduce,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="main.java.com.item.model.Users" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    update users
    <set >
      <if test="createdate != null" >
        createDate = #{createdate,jdbcType=TIMESTAMP},
      </if>
      <if test="username != null" >
        userName = #{username,jdbcType=VARCHAR},
      </if>
      <if test="introduce != null" >
        introduce = #{introduce,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="main.java.com.item.model.Users" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 28 16:28:01 CST 2022.
    -->
    update users
    set createDate = #{createdate,jdbcType=TIMESTAMP},
      userName = #{username,jdbcType=VARCHAR},
      introduce = #{introduce,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

生成model文件示例:

代码语言:javascript复制
package main.java.com.item.model;

import java.util.Date;

public class Users {
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column users.id
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    private Integer id;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column users.createDate
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    private Date createdate;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column users.userName
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    private String username;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column users.introduce
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    private String introduce;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column users.id
     *
     * @return the value of users.id
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public Integer getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column users.id
     *
     * @param id the value for users.id
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column users.createDate
     *
     * @return the value of users.createDate
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public Date getCreatedate() {
        return createdate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column users.createDate
     *
     * @param createdate the value for users.createDate
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public void setCreatedate(Date createdate) {
        this.createdate = createdate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column users.userName
     *
     * @return the value of users.userName
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public String getUsername() {
        return username;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column users.userName
     *
     * @param username the value for users.userName
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column users.introduce
     *
     * @return the value of users.introduce
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public String getIntroduce() {
        return introduce;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column users.introduce
     *
     * @param introduce the value for users.introduce
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    public void setIntroduce(String introduce) {
        this.introduce = introduce == null ? null : introduce.trim();
    }
}

生成dao层文件示例:

代码语言:javascript复制
package main.java.com.item.dao;

import main.java.com.item.model.Users;

public interface UsersMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table users
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table users
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    int insert(Users record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table users
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    int insertSelective(Users record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table users
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    Users selectByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table users
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    int updateByPrimaryKeySelective(Users record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table users
     *
     * @mbggenerated Sat May 28 16:28:01 CST 2022
     */
    int updateByPrimaryKey(Users record);
}

0 人点赞