使用mybatis-generator生成底层
环境
使用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&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和映射文件,已经生成了基本的增删改查和根据主键查找的方法
使用mybatis-generator生成底层的更多相关文章
- mybatis Generator生成代码及使用方式
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...
- Maven下用MyBatis Generator生成文件
使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...
- MyBatis Generator生成DAO——序列化
MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的. 还以为要手工加入(開始是手工加入的),今天遇到分页的问题,才发现生成的时候能够加入插件. 既然分页能够有插件.序列化是 ...
- 利用org.mybatis.generator生成实体类
springboot+maven+mybatis+mysql 利用org.mybatis.generator生成实体类 1.添加pom依赖: 2.编写generatorConfig.xml文件 ( ...
- MyBatis Generator 生成的example 使用 and or 简单混合查询
MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...
- 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化
Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...
- MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- 使用MyBatis Generator生成DAO
虽然MyBatis很方便,但是想要手写全部的mapper还是很累人的,好在MyBatis官方推出了自动化工具,可以根据数据库和定义好的配置直接生成DAO层及以下的全部代码,非常方便. 需要注意的是,虽 ...
- Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类
[前言] 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件,Mybatis-Generator的作用就是充当了一个代码生成器的角色,使用代码生成器不仅可以简化我 ...
随机推荐
- python基础——元组(tuple)
Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. tuple1 = () tuple2 = ...
- @ConfigurationProperties 注解使用姿势,这一篇就够了
在编写项目代码时,我们要求更灵活的配置,更好的模块化整合.在 Spring Boot 项目中,为满足以上要求,我们将大量的参数配置在 application.properties 或 applicat ...
- springboot整合elasticsearch(基于es7.2和官方high level client)
前言 最近写的一个个人项目(传送门:全终端云书签)中需要用到全文检索功能,目前 mysql,es 都可以做全文检索,mysql 胜在配置方便很快就能搞定上线(参考这里),不考虑上手难度,es 在全文检 ...
- JS中构造函数和普通函数有什么区别
JS中构造函数有普通函数有什么区别? 1.一般规则 构造函数都应该以 一个大写字母开头,eg: function Person(){...} 而非构造函数则应该以一个小写字母开头,eg: functi ...
- 泥瓦匠 5 年 Java 的成长感悟(下)
继续<泥瓦匠 5 年 Java 的成长感悟(上)>,大致包括下面几点: 学技术的心态 学技术的学法 工作的心态 工作的硬技能 工作的软实力 听点雷子的民谣,我就安静地感概感概.上次说写的, ...
- Python实现批量处理扫描特定目录
## 简述在渗透测试中遇到相同CMS站点时,搞定一个站点,相当于拿了一个站群的通用漏洞,所以我们首先需要标注站点的CMS类型,根据要求编写如下脚本 ## 要求1.访问特定目录,如:站点特定 /cmsa ...
- c#小灶——初识c#
提到c#,就不得不说.net,.net是微软开发的一个平台,简单来说,在这个平台上,可以编写.运行程序.可能很多人觉得这个平台离我们很遥远,其实不然,这个平台就一直在我们的windows操作系统里,默 ...
- 《HTTP权威指南》--阅读笔记(二)
URL的三部分: 1,方案 scheme 2,服务器位置 3,资源路径 URL语法: <scheme>://<user>:<password>@<host&g ...
- macos Mojave 出现网络出错 Frame Check Sequence: Bad checksum
问题描述:使用软电话外呼的时候出现Request Timeout . 端口监听之后通过 Wireshark发现错误:`Frame Check Sequence: Bad checksum`,查看wir ...
- vue面试题整理vuejs基础知识整理
初级参考 1.v-show 与 v-if 区别 v-show 是css隐藏,v-if是直接销毁和创建,所以频繁切换的适合用v-show 2.计算属性和 watch 的区别 计算属性是自动监听依赖值的变 ...