【Java】Windows环境下Mybatis快速生成映射代码工具类介绍
来源:互联网 发布:淘宝分享有礼在哪里 编辑:程序博客网 时间:2024/06/07 14:36
1.环境介绍
目标连接mysql 映射mapper 和JavaBean类。目前在windows环境下面进行测试。
2.准备jar包
mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.6.jar
3.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="D:\generator\mysql-connector-java-5.1.6.jar" /><!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" /> --><context id="DB2Tables" targetRuntime="MyBatis3"><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><!-- 数据库链接URL、用户名、密码 --><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="xxxxxx" password="xxxxxx"><!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="aaa" password="aaa">--></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 生成模型的包名和位置 --><javaModelGenerator targetPackage="com.naton.model" targetProject="D:\generator\src"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 生成的映射文件包名和位置 --><sqlMapGenerator targetPackage="com.naton.mapping" targetProject="D:\generator\src"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 生成DAO的包名和位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="com.naton.dao" targetProject="D:\generator\src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要生成那些表(更改tableName和domainObjectName就可以) --><table tableName="tb_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /></context></generatorConfiguration>
4.脚本命令:cmd 切换到当前文件的目录下,执行:
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
5.查看文件
具体生成的代码如下,后续就可以根据项目使用情况在进行调整。
package com.naton.dao;import com.naton.model.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);}package com.naton.model;public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); }}<?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.naton.dao.UserMapper" > <resultMap id="BaseResultMap" type="com.naton.model.User" > <id column="ID" property="id" jdbcType="INTEGER" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > ID, username, password </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from tb_user where ID = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from tb_user where ID = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.naton.model.User" > insert into tb_user (ID, username, password ) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.naton.model.User" > insert into tb_user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > ID, </if> <if test="username != null" > username, </if> <if test="password != null" > password, </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> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.naton.model.User" > update tb_user <set > <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> </set> where ID = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.naton.model.User" > update tb_user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} where ID = #{id,jdbcType=INTEGER} </update></mapper>
知行办公,专业移动办公平台 https://zx.naton.cn/
原创团队
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;【运营】运维艄公,897221533@qq.com;
【产品设计】流浪猫,364994559@qq.com;【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;【测试】土镜问道,847071279@qq.com;
【数据】fox009521,42151960@qq.com;【安全】保密,你懂的。
阅读全文
0 0
- 【Java】Windows环境下Mybatis快速生成映射代码工具类介绍
- MyBatis代码生成工具
- [Mybatis]代码生成工具
- Mybatis 代码生成工具
- Mybatis代码生成工具
- mybatis自动生成映射代码
- java代码ping服务IP工具类(Windows,Linux环境)
- eclipse下安装mybatis-generator代码反向生成工具
- MyEclipse下安装MyBatis Generator代码反向生成工具
- Abator代码生成工具 - -Eclipse下Ibatis|Mybatis代码生成工具 利器
- Mybatis 自动生成代码工具
- Mybatis 自动生成代码工具
- mybatis代码自动生成工具
- Mybatis 自动生成代码工具
- 【Mybatis】自动生成代码工具
- MyBatis-generator 代码生成工具
- mybatis代码自动生成工具
- mybatis反向生成代码工具
- 爸比,什么是机器学习,机器能是如何学习的呀?
- android API 参考大全
- 网易2017内推笔试1:藏宝图 [python]
- 为什么我的MATLAB激活成功后打开还是激活界面
- 跟着实例学习ZooKeeper的用法: Leader选举
- 【Java】Windows环境下Mybatis快速生成映射代码工具类介绍
- 兔子问题C语言
- Caffe编译问题"locale::facet::_S_create_c_locale name not valid"
- win10下TortoiseSVN 1.9.7-x64不显示图标
- Qt软件发布问题记录
- 在android studio导入应用原码出现的error: protocol fault (couldn't read status): Connection reset by peer
- 修改限值 scada系统
- 1005. Spell It Right (20)
- 本地服务器安装 GITLAB 服务