前言
今天分享一个用于mybatis生成代码的神器,经过简单的配置我们可以自动生成数据库里相应的表的model,dao,mapper文件,先上图看下效果
效果
-
假如我们数据库有这样一张class表
-
通过简单配置我们就可以自动生成相应的代码文件:
-
实体类:
public class Cla { private Integer cid; private String cname; private String clocation; public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname == null ? null : cname.trim(); } public String getClocation() { return clocation; } public void setClocation(String clocation) { this.clocation = clocation == null ? null : clocation.trim(); } }
-
接口:
public interface ClaMapper { int deleteByPrimaryKey(Integer cid); int insert(Cla record); int insertSelective(Cla record); Cla selectByPrimaryKey(Integer cid); int updateByPrimaryKeySelective(Cla record); int updateByPrimaryKey(Cla record); }
-
mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "./mybatis-3-mapper.dtd" > <mapper namespace="com.lys.dao.ClaMapper" > <resultMap id="BaseResultMap" type="com.lys.model.Cla" > <id column="cId" property="cid" jdbcType="INTEGER" /> <result column="cName" property="cname" jdbcType="VARCHAR" /> <result column="cLocation" property="clocation" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > cId, cName, cLocation </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from class where cId = #{cid,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from class where cId = #{cid,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.lys.model.Cla" > insert into class (cId, cName, cLocation ) values (#{cid,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{clocation,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.lys.model.Cla" > insert into class <trim prefix="(" suffix=")" suffixOverrides="," > <if test="cid != null" > cId, </if> <if test="cname != null" > cName, </if> <if test="clocation != null" > cLocation, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="cid != null" > #{cid,jdbcType=INTEGER}, </if> <if test="cname != null" > #{cname,jdbcType=VARCHAR}, </if> <if test="clocation != null" > #{clocation,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.lys.model.Cla" > update class <set > <if test="cname != null" > cName = #{cname,jdbcType=VARCHAR}, </if> <if test="clocation != null" > cLocation = #{clocation,jdbcType=VARCHAR}, </if> </set> where cId = #{cid,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.lys.model.Cla" > update class set cName = #{cname,jdbcType=VARCHAR}, cLocation = #{clocation,jdbcType=VARCHAR} where cId = #{cid,jdbcType=INTEGER} </update> </mapper>
-
做法
首先我们要准备这样一个工具:链接:https://pan.baidu.com/s/1kUGwQUtIHYCFImArBPF32A
提取码:1314
下载解压后是这样的:
进入lib文件:(注意:根据自己电脑上安装MySQL的版本,里面的驱动包可以自己选择性替换,如果驱动包的版本和MySQL的版本不对应,会出错的)
打开generator.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>
<!-- 修改相应数据库驱动-->
<classPathEntry location="mysql-connector-java-8.0.17.jar"/>
<context id="mysqltables" targetRuntime="MyBatis3" defaultModelType="conditional">
<!-- 注释生成-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/school?useSSL=FALSE&serverTimezone=Asia/Shanghai" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--自动生成的文件的位置可以根据实际需要进行更改调整-->
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.lys.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.lys.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lys.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成哪些表? 配置相应的表名和实体类的名-->
<table tableName="class" domainObjectName="Cla" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
<!-- java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite -->
配置好保存上面的文件,然后打开cmd,进入lib目录下:
然后输入指令:
看到成功的提示,就OK了
然后进入当前的src目录下,就能看到自动生成的代码文件了
总结
通过上面工具生成部分代码,在需要的地方直接粘贴过去就行,一定程度上可以减少些重复性工作,当然,工具不唯一,还有其他工具,感兴趣的小伙伴可以去尝试一下哦