1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > MyBatis-Generato代码生成器(独立版)

MyBatis-Generato代码生成器(独立版)

时间:2020-01-05 03:02:20

相关推荐

MyBatis-Generato代码生成器(独立版)

项目背景

这个是在mybatis大规模应用的时候自己研究出来的一个mybatis代码生成器,好久没有用了,官方也没有怎么大规模更新,毕竟好用的民间工具太多,好用的是大家的思维和sql。mybatis的强大之处在于把任何复杂的sql,包括多表关联,包括多重计算,处理之后映射到任意一个实体上,以达到查询的目的。

相关资料下载

百度网盘: /s/10h1n9PNX891O19YerX4PqQ 密码: pta3CSDN /download/moshowgame/10424926

建表语句

CREATE TABLE `sys_user` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',`username` varchar(255) DEFAULT NULL COMMENT '账号',`password` varchar(255) DEFAULT NULL COMMENT '密码',`user_type` int(11) DEFAULT NULL COMMENT '用户类型',`createtime` datetime DEFAULT NULL COMMENT '创建时间',`lastupdatetime` datetime DEFAULT NULL COMMENT '最后更新时间',PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;

配置Generator的xml

自动生成entity/XML/mapper/DAO的配置,注释我写得很详细了

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN" "/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!-- MyBatis代码生成器 @author Moshow --><!-- 需要修改的地方是①②③④,然后执行批处理即可 --><!-- classPathEntry:数据库的JDBC驱动,①location换成你自己的mysql类库位置 --><!-- maven一般在C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\,也可以自己下载一个并指定 --><!-- <classPathEntrylocation="E:\MyEclipse Professional \.metadata\.me_tcat7\webapps\zkungfu\WEB-INF\lib\ojdbc6.jar" /> --><classPathEntrylocation="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar" /><context id="DB2Tables" targetRuntime="MyBatis3"><!-- 去除自动生成的注释 --><commentGenerator><property name="suppressAllComments" value="false" /></commentGenerator><!-- 数据库连接,②userId&password改成自己的配置,如果是oracle还需要修改目录下的tnsnames.ora --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"userId="root" password="root"></jdbcConnection><!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"connectionURL="jdbc:oracle:thin:@localhost:1521:OrderTest"userId="xxx" password="xxx"> </jdbcConnection>--><javaTypeResolver><property name="forceBigDecimals" value="true" /><!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --></javaTypeResolver><!-- targetProject:③修改自动生成代码的位置,包括DAO/entity/XML,一般生成在项目下面的src或者test --><!-- 生成vo对象 --><javaModelGenerator targetPackage="com.tcbj.checksys.user.domain"targetProject="D:\workspace\temp"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /><!--targetProject 生成的Java Bean放置在哪个项目的哪个目录下 targetPackage 生成的Java Bean的包名一个有用的属性 <property name="trimStrings" value="true" /> 从数据库返回的值被清理前后的空格<property name="enableSubPackages" value="false" /> 是否在包名后加上scheme名称--></javaModelGenerator><!-- 生成用于查询的Example对象 --><sqlMapGenerator targetPackage="com.tcbj.checksys.user.persistence"targetProject="D:\workspace\temp"><property name="enableSubPackages" value="true" /><!--targetProject 生成的 SqlMap.xml 文件放置在哪个项目的哪个目录下 targetPackage 生成的SqlMap.xml 文件的包名 <property name="enableSubPackages" value="false" />是否在包名后加上scheme名称--></sqlMapGenerator><!-- 生成DAO的类文件以及配置文件 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.tcbj.checksys.user.persistence" targetProject="D:\workspace\temp"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- tableName:④用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --><!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。 --><table tableName="sys_user" domainObjectName="User"enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /></context></generatorConfiguration>

开始生成

接下来进入到目录手工运行@mybatisGenerator.bat

如果提示“The specified target project directory D:\Workspaces\temp does not exist”是因为指定的生成目录不存在,记得要创建

提示以下这个是因为已经生成过了,所以每次都会重新覆盖,如果是加了新字段,你可以直接copy实体,然后mapper的xml自己加就可以了。

Existing file D:\workspace\temp\com\tcbj\checksys\user\domain\User.java was overwrittenExisting file D:\workspace\temp\com\tcbj\checksys\user\persistence\UserMapper.java was overwritten

如果你觉得生成一大堆注释很烦,可以调整一下,把这个suppressAllComments的值设置为true,就可以少一堆啰嗦的注释,比较干净

<!-- 去除自动生成的注释 --><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator>

验收生成结果

package com.tcbj.checksys.user.domain;import java.util.Date;public class User {private Integer id;private String username;private String password;private Integer userType;private Date createtime;private Date lastupdatetime;//这里对应的set、get我先删了,生成是有的,我们看下精华部分就行}

package com.tcbj.checksys.user.persistence;import com.tcbj.checksys.user.domain.User;public interface UserMapper {int deleteByPrimaryKey(Integer id);int insert(User record);int insertSelective(User record);User selectByPrimaryKey(Integer id);int updateByPrimaryKeySelective(User record);int updateByPrimaryKey(User record);}

mapper才是mybatis的精华

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.tcbj.checksys.user.persistence.UserMapper"><resultMap id="BaseResultMap" type="com.tcbj.checksys.user.domain.User"><id column="id" jdbcType="INTEGER" property="id" /><result column="username" jdbcType="VARCHAR" property="username" /><result column="password" jdbcType="VARCHAR" property="password" /><result column="user_type" jdbcType="INTEGER" property="userType" /><result column="createtime" jdbcType="TIMESTAMP" property="createtime" /><result column="lastupdatetime" jdbcType="TIMESTAMP" property="lastupdatetime" /></resultMap><sql id="Base_Column_List">id, username, password, user_type, createtime, lastupdatetime</sql><select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">select <include refid="Base_Column_List" />from sys_userwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">delete from sys_userwhere id = #{id,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.tcbj.checksys.user.domain.User">insert into sys_user (id, username, password, user_type, createtime, lastupdatetime)values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{userType,jdbcType=INTEGER}, #{createtime,jdbcType=TIMESTAMP}, #{lastupdatetime,jdbcType=TIMESTAMP})</insert><insert id="insertSelective" parameterType="com.tcbj.checksys.user.domain.User">insert into sys_user<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">id,</if><if test="username != null">username,</if><if test="password != null">password,</if><if test="userType != null">user_type,</if><if test="createtime != null">createtime,</if><if test="lastupdatetime != null">lastupdatetime,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="id != null">#{id,jdbcType=INTEGER},</if><if test="username != null">#{username,jdbcType=VARCHAR},</if><if test="password != null">#{password,jdbcType=VARCHAR},</if><if test="userType != null">#{userType,jdbcType=INTEGER},</if><if test="createtime != null">#{createtime,jdbcType=TIMESTAMP},</if><if test="lastupdatetime != null">#{lastupdatetime,jdbcType=TIMESTAMP},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.tcbj.checksys.user.domain.User">update sys_user<set><if test="username != null">username = #{username,jdbcType=VARCHAR},</if><if test="password != null">password = #{password,jdbcType=VARCHAR},</if><if test="userType != null">user_type = #{userType,jdbcType=INTEGER},</if><if test="createtime != null">createtime = #{createtime,jdbcType=TIMESTAMP},</if><if test="lastupdatetime != null">lastupdatetime = #{lastupdatetime,jdbcType=TIMESTAMP},</if></set>where id = #{id,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.tcbj.checksys.user.domain.User">update sys_userset username = #{username,jdbcType=VARCHAR},password = #{password,jdbcType=VARCHAR},user_type = #{userType,jdbcType=INTEGER},createtime = #{createtime,jdbcType=TIMESTAMP},lastupdatetime = #{lastupdatetime,jdbcType=TIMESTAMP}where id = #{id,jdbcType=INTEGER}</update></mapper>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。