环境

​ 使用springboot2,jdk1.8,idea

在pom引入相关依赖

<!--mybatise-generator-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--在此处指定配置文件位置-->
<configurationFile>src/main/resources/generatorConfig/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<!--mybatise-generator-->
<dependencies>
<!--在此处引入所需依赖-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</dependencies>
</plugin>

注意:

mybatis-generator版本如果和mysql差距過大,可能在生成代码的过程中引起报错

在Resource中配置配置文件

<?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>
<!--注意这里的targetRuntime="MyBatis3Simple",指定了不生成Example相关内容-->
<context id="MysqlTables" targetRuntime="MyBatis3Simple"> <commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator> <!-- jdbc链接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"
userId="root" password="123456">
</jdbcConnection> <javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 生成PO类的位置 -->
<javaModelGenerator targetPackage="com.huang.po"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <!-- mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.party.community.template"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 指定要生成的表,主鍵,po类名 -->
<table tableName="Admins" domainObjectName="Admins">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="a_id" sqlStatement="MySql" identity="true"/>
</table> </context>
</generatorConfiguration>

注意:

​ 1.存放生成代码的指定包或者文件夹可以不用提前建好,只要路径没错插件会顺便一起建好

​ 2.根指定路径的不同,部分生成代码可能会需要更改。比如映射配置文件,

​ namespace的生成是根据上面的配置而生成的,如果你在生成代码后还想要移动mapper的話,要记得修改对应路径

生成代码

代开Maven菜单,点击Plugins里的mybatis-generator,即可自动生成

目录结构

生成的代码

java代码

package com.party.community.po;

import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class User {
private String u_idcard; private String u_unsername; private String u_tel; private String u_pwd; private String u_avator; private String u_sex; private Date u_birthday; private String u_history; private String u_remark; public String getU_idcard() {
return u_idcard;
} public void setU_idcard(String u_idcard) {
this.u_idcard = u_idcard == null ? null : u_idcard.trim();
} public String getU_unsername() {
return u_unsername;
} public void setU_unsername(String u_unsername) {
this.u_unsername = u_unsername == null ? null : u_unsername.trim();
} public String getU_tel() {
return u_tel;
} public void setU_tel(String u_tel) {
this.u_tel = u_tel == null ? null : u_tel.trim();
} public String getU_pwd() {
return u_pwd;
} public void setU_pwd(String u_pwd) {
this.u_pwd = u_pwd == null ? null : u_pwd.trim();
} public String getU_avator() {
return u_avator;
} public void setU_avator(String u_avator) {
this.u_avator = u_avator == null ? null : u_avator.trim();
} public String getU_sex() {
return u_sex;
} public void setU_sex(String u_sex) {
this.u_sex = u_sex == null ? null : u_sex.trim();
} public Date getU_birthday() {
return u_birthday;
} public void setU_birthday(Date u_birthday) {
this.u_birthday = u_birthday;
} public String getU_history() {
return u_history;
} public void setU_history(String u_history) {
this.u_history = u_history == null ? null : u_history.trim();
} public String getU_remark() {
return u_remark;
} public void setU_remark(String u_remark) {
this.u_remark = u_remark == null ? null : u_remark.trim();
}
}

生成的po,已写好get和set方法

package com.party.community.mapper;

import com.party.community.po.User;
import java.util.List; public interface UserMapper {
int deleteByPrimaryKey(String u_idcard); int insert(User record); User selectByPrimaryKey(String u_idcard); List<User> selectAll(); int updateByPrimaryKey(User record); User selectByPhone(String phone); }

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.party.community.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.party.community.po.User" >
<id column="u_idcard" property="u_idcard" jdbcType="VARCHAR" />
<result column="u_unsername" property="u_unsername" jdbcType="VARCHAR" />
<result column="u_tel" property="u_tel" jdbcType="VARCHAR" />
<result column="u_pwd" property="u_pwd" jdbcType="VARCHAR" />
<result column="u_avator" property="u_avator" jdbcType="VARCHAR" />
<result column="u_sex" property="u_sex" jdbcType="VARCHAR" />
<result column="u_birthday" property="u_birthday" jdbcType="TIMESTAMP" />
<result column="u_history" property="u_history" jdbcType="VARCHAR" />
<result column="u_remark" property="u_remark" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from user
where u_idcard = #{u_idcard,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.party.community.po.User" >
<selectKey resultType="java.lang.String" keyProperty="u_idcard" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into user (u_unsername, u_tel, u_pwd,
u_avator, u_sex, u_birthday,
u_history, u_remark)
values (#{u_unsername,jdbcType=VARCHAR}, #{u_tel,jdbcType=VARCHAR}, #{u_pwd,jdbcType=VARCHAR},
#{u_avator,jdbcType=VARCHAR}, #{u_sex,jdbcType=VARCHAR}, #{u_birthday,jdbcType=TIMESTAMP},
#{u_history,jdbcType=VARCHAR}, #{u_remark,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.party.community.po.User" >
update user
set u_unsername = #{u_unsername,jdbcType=VARCHAR},
u_tel = #{u_tel,jdbcType=VARCHAR},
u_pwd = #{u_pwd,jdbcType=VARCHAR},
u_avator = #{u_avator,jdbcType=VARCHAR},
u_sex = #{u_sex,jdbcType=VARCHAR},
u_birthday = #{u_birthday,jdbcType=TIMESTAMP},
u_history = #{u_history,jdbcType=VARCHAR},
u_remark = #{u_remark,jdbcType=VARCHAR}
where u_idcard = #{u_idcard,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select u_idcard, u_unsername, u_tel, u_pwd, u_avator, u_sex, u_birthday, u_history,
u_remark
from user
where u_idcard = #{u_idcard,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select u_idcard, u_unsername, u_tel, u_pwd, u_avator, u_sex, u_birthday, u_history,
u_remark
from user
</select>
<select id="selectByPhone" resultType="User">
select *
from user
where u_tel = #{phone}
</select> </mapper>

生成的mapper和映射文件,已经生成了基本的增删改查和根据主键查找的方法

最新文章

  1. 基于git diff进行的eslint代码检测
  2. 在asp.net WebForms中使用路由Route
  3. 006_Salesforce Sharing 使用说明
  4. HDU 4314 Save the dwarfs (DP) ---转载
  5. Apache是目前应用最广的Web服务器,PHP3是一种类似ASP的脚本语言
  6. jquery ajax 总是还未等到success回调就刷掉了,就进入了onError函数的错误案例分析
  7. 黑马程序员-集合(二)contains()方法的内部探索
  8. POJ 1195 Mobile phones (二维树状数组或线段树)
  9. ThinkPHP - URL - 伪静态 - 路由 - 重写
  10. Quill编辑器介绍及扩展
  11. 个人作业3——个人总结(Alpha阶段)
  12. IIS 应用程序池自动停止
  13. PHP7链接MySQL
  14. FFmpeg and x264 Encoding Guide
  15. 全新定义!免费开源ERP平台如何玩转工业互联网
  16. python class属性
  17. java 学习笔记
  18. HTML 中的 href\src\url
  19. 8款压箱底的Mac屏幕截图和录音录像工具软件,请你务必低调使用
  20. 使用JWT的RSA256加密做为用户认证, 测试性能

热门文章

  1. python课堂整理12---递归
  2. React入门理解demo
  3. ubuntu/deepin 下下载wxpython
  4. 自定义SWT控件一之自定义单选下拉框
  5. Mac OS 安装mysqlclient 遇到的坑~
  6. abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十二)
  7. Qt Socket 收发图片——图像拆包、组包、粘包处理
  8. ASP.NET Web项目发布选项:“允许更新此预编译站点” 详解
  9. manifest.json 解析--手机web app开发笔记(三-2)
  10. 剑指offer-链表