【Mybatis】【1】generate批量生成实体类,数据库接口类和mapper

时间:2021-10-07 21:05:50

前言:

1,实体类之类如果自己写的话,比较繁琐,还容易出错,所以用generate自动生成

2,int类型可能会生成为short类型,建议不要手动改回int类。因为下次生成又是short类型了,可能会对代码有影响,每次都更改又比较麻烦

正文:

用法:点击generate.bat即可执行,如果报错,需要看执行的信息,就用命令行执行

注:有的时候再次生成mapper之后,启动项目会报错。删除生成的mapper文件,重新生成一次就好了

jar包:ojdbc6.jar(这是oracle的驱动jar包,mysql是mysql-connector-java-3.1.12-bin.jar),mybatis-generator-core-1.3.2.jar(网盘链接:https://pan.baidu.com/s/1sE3wAbbcXLCmMvY_OfSw5g 提取码:qy2p )

generatorConfig.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="G:\lib\ojdbc6.jar" /> <!-- 数据库驱动jar包 -->
<context id="context1">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin> <!-- 翻页 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection connectionURL="jdbc:oracle:thin:@172.1.1.0:1521/test" driverClass="oracle.jdbc.driver.OracleDriver" userId="test" password="test" /> <!-- 数据库账号密码 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.bf.doctor.entity" targetProject="G:\BFDoctor\Source\src\main\java" /> <!-- 实体类 -->
<sqlMapGenerator targetPackage="mybatis.mapper" targetProject="G:\BFDoctor\Source\src\main\resources" /> <!-- 数据库xml文件 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bf.doctor.dao.mapper" targetProject="G:\BFDoctor\Source\src\main\java" /><!-- 数据库接口文件 --> <table tableName="A" /> <!-- 表名 --> <!--
<table tableName="A" />
<table tableName="B" />
<table tableName="C" />
--> <!-- 全部表名,用完不要删除,之后生成单个表就拿出去 -->
</context> </generatorConfiguration>

generate.bat

java -jar G:\lib\mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

生成的文件:

实体类Test

package com.bf.doctor.entity;

public class Test {
private String id; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
}
}

映射TestExample

package com.bf.doctor.entity;

import java.util.ArrayList;
import java.util.List; public class TestExample {
protected String orderByClause; protected boolean distinct; protected List<Criteria> oredCriteria; public TestExample() {
oredCriteria = new ArrayList<Criteria>();
} public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
} public String getOrderByClause() {
return orderByClause;
} public void setDistinct(boolean distinct) {
this.distinct = distinct;
} public boolean isDistinct() {
return distinct;
} public List<Criteria> getOredCriteria() {
return oredCriteria;
} public void or(Criteria criteria) {
oredCriteria.add(criteria);
} public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
} public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
} protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
} public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
} protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria; protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
} public boolean isValid() {
return criteria.size() > 0;
} public List<Criterion> getAllCriteria() {
return criteria;
} public List<Criterion> getCriteria() {
return criteria;
} protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
} protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
} protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
} public Criteria andIdIsNull() {
addCriterion("ID is null");
return (Criteria) this;
} public Criteria andIdIsNotNull() {
addCriterion("ID is not null");
return (Criteria) this;
} public Criteria andIdEqualTo(String value) {
addCriterion("ID =", value, "id");
return (Criteria) this;
} public Criteria andIdNotEqualTo(String value) {
addCriterion("ID <>", value, "id");
return (Criteria) this;
} public Criteria andIdGreaterThan(String value) {
addCriterion("ID >", value, "id");
return (Criteria) this;
} public Criteria andIdGreaterThanOrEqualTo(String value) {
addCriterion("ID >=", value, "id");
return (Criteria) this;
} public Criteria andIdLessThan(String value) {
addCriterion("ID <", value, "id");
return (Criteria) this;
} public Criteria andIdLessThanOrEqualTo(String value) {
addCriterion("ID <=", value, "id");
return (Criteria) this;
} public Criteria andIdLike(String value) {
addCriterion("ID like", value, "id");
return (Criteria) this;
} public Criteria andIdNotLike(String value) {
addCriterion("ID not like", value, "id");
return (Criteria) this;
} public Criteria andIdIn(List<String> values) {
addCriterion("ID in", values, "id");
return (Criteria) this;
} public Criteria andIdNotIn(List<String> values) {
addCriterion("ID not in", values, "id");
return (Criteria) this;
} public Criteria andIdBetween(String value1, String value2) {
addCriterion("ID between", value1, value2, "id");
return (Criteria) this;
} public Criteria andIdNotBetween(String value1, String value2) {
addCriterion("ID not between", value1, value2, "id");
return (Criteria) this;
}
} public static class Criteria extends GeneratedCriteria { protected Criteria() {
super();
}
} public static class Criterion {
private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() {
return condition;
} public Object getValue() {
return value;
} public Object getSecondValue() {
return secondValue;
} public boolean isNoValue() {
return noValue;
} public boolean isSingleValue() {
return singleValue;
} public boolean isBetweenValue() {
return betweenValue;
} public boolean isListValue() {
return listValue;
} public String getTypeHandler() {
return typeHandler;
} protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
} protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
} protected Criterion(String condition, Object value) {
this(condition, value, null);
} protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
} protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}

数据库接口TestMapper

package com.bf.doctor.dao.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds; public interface TestMapper {
int countByExample(TestExample example); int deleteByExample(TestExample example); int insert(Test record); int insertSelective(Test record); List<Test> selectByExampleWithRowbounds(TestExample example, RowBounds rowBounds); List<Test> selectByExample(TestExample example); int updateByExampleSelective(@Param("record") Test record, @Param("example") TestExample example); int updateByExample(@Param("record") Test record, @Param("example") TestExample example);
}

数据库mapper

<?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.bf.doctor.dao.mapper.TestMapper" >
<resultMap id="BaseResultMap" type="com.bf.doctor.entity.Test" >
<result column="ID" property="id" jdbcType="VARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
ID
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.bf.doctor.entity.TestExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from TEST
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<delete id="deleteByExample" parameterType="com.bf.doctor.entity.TestExample" >
delete from TEST
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.bf.doctor.entity.Test" >
insert into TEST (ID)
values (#{id,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.bf.doctor.entity.Test" >
insert into TEST
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.bf.doctor.entity.TestExample" resultType="java.lang.Integer" >
select count(*) from TEST
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update TEST
<set >
<if test="record.id != null" >
ID = #{record.id,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update TEST
set ID = #{record.id,jdbcType=VARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<select resultMap="BaseResultMap" parameterType="com.bf.doctor.entity.TestExample" id="selectByExampleWithRowbounds" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from TEST
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
</mapper>

参考博客:

1,mybatis三剑客之mybatis-generator生成数据对象和时间戳优化 - xyphf的博客 - CSDN博客
https://blog.csdn.net/xyphf/article/details/81229636

【Mybatis】【1】generate批量生成实体类,数据库接口类和mapper的更多相关文章

  1. Springboot mybatis generate 自动生成实体类和Mapper

    https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...

  2. SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码

    1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...

  3. 使用eclipse插件mybatis generator来自动生成实体类及映射文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...

  4. myBatis执行测试批量删除,出现测试类正常显示,但数据库数据没变

    一般在测试myBatis运行正常,但数据库数据不变时,有可能是SQL语句有问题,检查SQL语句没问题,但数据库依然没变,就说明myBatis方法执行后并未提交到数据库,可尝试在测试类添加   sess ...

  5. CodeSmith批量生成实体

    保存以下文件为ModelBatch.cst <%@ Register Name="Model" Template="D:\Q\web\LHWYVISIT\trunk ...

  6. Mybatis自动生成实体类、dao接口和mapping映射文件

    由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类.dao接口以及它的映射文件,然后直接拷贝到 ...

  7. 使用MyBatis Generator自动生成实体、mapper和dao层

    原文链接 通过MyBatis Generator可以自动生成实体.mapper和dao层,记录一下怎么用的. 主要步骤: 关于mybatis从数据库反向生成实体.DAO.mapper: 参考文章:ht ...

  8. FluentData-新型轻量级ORM 利用T4模板 批量生成多文件 实体和业务逻辑 代码

    FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.Model 利用T4模板,[MultipleOutputHelper.ttinclude]批量生成多文件 ...

  9. T4批量生成多文件

    http://www.cnblogs.com/zengxiangzhan/p/3250105.html Manager.ttinclude <#@ assembly name="Sys ...

随机推荐

  1. ContentProvider*档案馆&comma;以及获取联系人电话的示例

    Android官方文档介绍的数据存储方式共有五种,sqlite,SharedPreferences,网络存储,外储存储,文件存储,但是这些数据都无法进行共享,那么我们就引入了今天的主角:Content ...

  2. json数组的序列化和反序列化json数组的序列化和反序列化

    如题,我就不多说了,自己看代码的,很好理解 using System; using System.Collections.Generic; using System.Web; using System ...

  3. Hadoop学习(1)-- 入门介绍

    Hadoop是Apache基金会开发的一个分布式系统基础架构,是时下最流行的分布式系统架构之一.用户可以在不了解分布式底层的情况下,在Hadoop上快速进行分布式应用的开发,并利用集群的计算和存储能力 ...

  4. 源码安装zabbix

    源码安装zabbix 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.     欢迎加入:高级运维工程师之路 598432640 前言:参考网上多篇源码安装的连接,自己把安装过程丢在这 ...

  5. 特殊情形的Riemann引理

    设 $f(x)$ 是 $[0,\infty)$ 上的单调函数, 则对任意固定的 $a$, 有 $\dps{\vlm{n}\int_0^a f(x)\sin nx\rd x =0}$; 若同时还有 $\ ...

  6. c缺陷与陷阱笔记-第一章 词法陷阱

    1.运算符的贪心性,匹配最长的运算符,例如 n-->0,从-开始,-是运算符,--是运算符,-->就不是,所以是 n -- > 0,--是 a---b,-是,--是,,---不是,所 ...

  7. 数组求最大最小值和排序java实现

    public class ArrayDemo05 { public static void main(String[] args) {     int list01[]={67,89,87,69,90 ...

  8. String与StringBuild、StringBuffer的区别

    String与StringBuild.StringBuffer的区别相信困扰了好多新入门的JAVA程序员,而这也是笔试和面试的一道常见题型,如何全面的回答该问题,变得尤为重要. 首先我们需要清楚一点, ...

  9. JUnit【1】断言用法之assertEquals&sol;True&sol;False&sol;ArrayEquals

    前段时间去亚信面试,被问到写一个冒泡排序,心想这多新鲜,刷刷几下写好.面试官突然问,你怎么对这个程序进行单元测试?    单元测试?!    懵圈...      单元测试      代码是为了什么, ...

  10. 20175202 《Java程序设计》第三周学习总结

    20175209 2018-2019-2 <Java程序设计>第三周学习总结 教材知识点总结 1.编程语言发展阶段: 面向机器语言——面向过程语言——面向对象语言. 2.类声明: 类名必须 ...