使用 Mybatis Generator 生成代码
程序员文章站
2022-05-08 11:02:57
...
1、导入依赖包
在maven中添加依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
在maven插件中添加
<plugins>
<!--mybatis****-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
完成后插件中会有mybatis-generator
2、编写generatorConfig.xml文件
在resources中新建generator文件夹然后新建generatorConfig.xml
<?xml version="1.0"?>
<!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="DB2Table" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<!-- 是否去除自动生成的注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接配置-->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/cms"
userId="root"
password="root">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!--实体类生成位置-->
<javaModelGenerator targetPackage="com.yhq.entity"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--mybatis映射xml文件的位置-->
<sqlMapGenerator targetPackage="mapper2" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--mapper接口生成的位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yhq.mapper2"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--配置需要生成的表的信息-->
<table tableName="t_article" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="user" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
3、使用插件生成代码
实体类
mapper接口
mapper.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.yhq.mapper2.TArticleMapper">
<resultMap id="BaseResultMap" type="com.yhq.entity.TArticle">
<id column="aid" jdbcType="INTEGER" property="aid" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="content" jdbcType="VARCHAR" property="content" />
<result column="author" jdbcType="VARCHAR" property="author" />
<result column="source" jdbcType="VARCHAR" property="source" />
<result column="createTime" jdbcType="TIMESTAMP" property="createtime" />
<result column="cid" jdbcType="INTEGER" property="cid" />
</resultMap>
<sql id="Base_Column_List">
aid, title, content, author, source, createTime, cid
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_article
where aid = #{aid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_article
where aid = #{aid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yhq.entity.TArticle">
insert into t_article (aid, title, content,
author, source, createTime,
cid)
values (#{aid,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR},
#{author,jdbcType=VARCHAR}, #{source,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP},
#{cid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yhq.entity.TArticle">
insert into t_article
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="aid != null">
aid,
</if>
<if test="title != null">
title,
</if>
<if test="content != null">
content,
</if>
<if test="author != null">
author,
</if>
<if test="source != null">
source,
</if>
<if test="createtime != null">
createTime,
</if>
<if test="cid != null">
cid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="aid != null">
#{aid,jdbcType=INTEGER},
</if>
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
<if test="content != null">
#{content,jdbcType=VARCHAR},
</if>
<if test="author != null">
#{author,jdbcType=VARCHAR},
</if>
<if test="source != null">
#{source,jdbcType=VARCHAR},
</if>
<if test="createtime != null">
#{createtime,jdbcType=TIMESTAMP},
</if>
<if test="cid != null">
#{cid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yhq.entity.TArticle">
update t_article
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
<if test="content != null">
content = #{content,jdbcType=VARCHAR},
</if>
<if test="author != null">
author = #{author,jdbcType=VARCHAR},
</if>
<if test="source != null">
source = #{source,jdbcType=VARCHAR},
</if>
<if test="createtime != null">
createTime = #{createtime,jdbcType=TIMESTAMP},
</if>
<if test="cid != null">
cid = #{cid,jdbcType=INTEGER},
</if>
</set>
where aid = #{aid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yhq.entity.TArticle">
update t_article
set title = #{title,jdbcType=VARCHAR},
content = #{content,jdbcType=VARCHAR},
author = #{author,jdbcType=VARCHAR},
source = #{source,jdbcType=VARCHAR},
createTime = #{createtime,jdbcType=TIMESTAMP},
cid = #{cid,jdbcType=INTEGER}
where aid = #{aid,jdbcType=INTEGER}
</update>
</mapper>
上一篇: 文章推荐系统(三)
推荐阅读
-
IDEA mybatis-generator逆向工程生成代码
-
详解在IDEA中使用MyBatis Generator逆向工程生成代码
-
MyBatis_Generator插件的安装以及简单使用方法(图解)
-
C#使用itextsharp生成PDF文件的实现代码
-
IDEA mybatis-generator逆向工程生成代码
-
详解在IDEA中使用MyBatis Generator逆向工程生成代码
-
基于Mybatis plus 自动代码生成器的实现代码
-
MySQL与MSSQl使用While语句循环生成测试数据的代码
-
Spring和MyBatis整合自动生成代码里面text类型遇到的坑
-
详解使用MyBatis Generator自动创建代码