一、簡介 在使用mybatis時我們需要重覆的去創建pojo類、mapper文件以及dao類並且需要配置它們之間的依賴關係,比較麻煩且做了大量的重覆工作,mybatis官方也發現了這個問題, 因此給我們提供了mybatis generator工具來幫我們自動創建pojo類、mapper文件以及dao ...
一、簡介
在使用mybatis時我們需要重覆的去創建pojo類、mapper文件以及dao類並且需要配置它們之間的依賴關係,比較麻煩且做了大量的重覆工作,mybatis官方也發現了這個問題,
因此給我們提供了mybatis generator工具來幫我們自動創建pojo類、mapper文件以及dao類並且會幫我們配置好它們的依賴關係
mybatis-geneator是一款mybatis自動代碼生成工具,可以通過配置,快速生成mapper和xml文件以及部分dao service controller簡單的增刪改查,極大的提高了我們的開發效率。
二、maven需要的主要依賴(由於使用springboot模塊化開發,依賴太多,不一一列舉)
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>Generate MyBatis Files</id> <goals> <goal>generate</goal> </goals> <phase>generate</phase> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </execution> </executions>
三、generator.properties配置文件(本案例資料庫只有一個表)
# generator.controller.module 可以設置為空,為空不生成代碼
generator.controller.module=project-web
# generator.service.module 可以設置為空,為空不生成代碼
generator.service.module=project-service
# generator.dao.module 不允許為空
generator.dao.module=project-dao
generator.controller.package.prefix=edu.nf.project
generator.service.package.prefix=edu.nf.project
generator.dao.package.prefix=edu.nf.project
generator.table.prefix=city_
generator.table.list=
generator.database=weather
generator.jdbc.driver=com.mysql.cj.jdbc.Driver
generator.jdbc.url=jdbc:mysql://localhost:3306/weather?useSSL=false&useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&serverTimezone=UTC
generator.jdbc.username=root
generator.jdbc.password=root
四、生成器(Generator)
/**
* 代碼生成類
*
* @author ywb
*/
public class Generator {
private static String CONTROLLER_MODULE = PropertiesFileUtil.getInstance("generator").get("generator.controller.module");
private static String SERVICE_MODULE = PropertiesFileUtil.getInstance("generator").get("generator.service.module");
private static String DAO_MODULE = PropertiesFileUtil.getInstance("generator").get("generator.dao.module");
private static String CONTROLLER_PACKAGE_PREFIX = PropertiesFileUtil.getInstance("generator").get("generator.controller.package.prefix");
private static String SERVICE_PACKAGE_PREFIX = PropertiesFileUtil.getInstance("generator").get("generator.service.package.prefix");
private static String DAO_PACKAGE_PREFIX = PropertiesFileUtil.getInstance("generator").get("generator.dao.package.prefix");
private static String TABLE_PREFIX = PropertiesFileUtil.getInstance("generator").get("generator.table.prefix");
private static String TABLE_LIST = PropertiesFileUtil.getInstance("generator").get("generator.table.list");
private static String DATABASE = PropertiesFileUtil.getInstance("generator").get("generator.database");
private static String JDBC_DRIVER = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.driver");
private static String JDBC_URL = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.url");
private static String JDBC_USERNAME = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.username");
private static String JDBC_PASSWORD = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.password");
/**
* 需要insert後返回主鍵的表配置,key:表名,value:主鍵名
*/
private static Map<String, String> LAST_INSERT_ID_TABLES = new HashMap<>();/**
* 自動代碼生成
*/
public static void main(String[] args) throws Exception {
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD,
CONTROLLER_MODULE, SERVICE_MODULE, DAO_MODULE,
DATABASE, TABLE_PREFIX, TABLE_LIST,
CONTROLLER_PACKAGE_PREFIX, SERVICE_PACKAGE_PREFIX, DAO_PACKAGE_PREFIX,
LAST_INSERT_ID_TABLES);
}
五、自動生成出來的mapper
package edu.nf.project.mapper;
import edu.nf.project.model.CityInfo;
import edu.nf.project.model.CityInfoExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface CityInfoMapper {
long countByExample(CityInfoExample example);
int deleteByExample(CityInfoExample example);
int deleteByPrimaryKey(Integer cityId);
int insert(CityInfo record);
int insertSelective(CityInfo record);
List<CityInfo> selectByExample(CityInfoExample example);
CityInfo selectByPrimaryKey(Integer cityId);
int updateByExampleSelective(@Param("record") CityInfo record, @Param("example") CityInfoExample example);
int updateByExample(@Param("record") CityInfo record, @Param("example") CityInfoExample example);
int updateByPrimaryKeySelective(CityInfo record);
int updateByPrimaryKey(CityInfo record);
}
五、自動生成出來的Model
package edu.nf.project.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import org.springframework.format.annotation.DateTimeFormat;
/**
*
* city_info
*
* @mbg.generated
*/
public class CityInfo implements Serializable {
/**
* 城市編號
*
* city_info.city_id
*/
@ApiModelProperty(value = "城市編號")
private Integer cityId;
/**
* 城市名稱
*
* city_info.city_name
*/
@ApiModelProperty(value = "城市名稱")
private String cityName;
/**
* 城市編碼
*
* city_info.city_code
*/
@ApiModelProperty(value = "城市編碼")
private String cityCode;
/**
* 省份
*
* city_info.province
*/
@ApiModelProperty(value = "省份")
private String province;
private static final long serialVersionUID = 1L;
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName == null ? null : cityName.trim();
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode == null ? null : cityCode.trim();
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province == null ? null : province.trim();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", cityId=").append(cityId);
sb.append(", cityName=").append(cityName);
sb.append(", cityCode=").append(cityCode);
sb.append(", province=").append(province);
sb.append("]");
return sb.toString();
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
CityInfo other = (CityInfo) that;
return (this.getCityId() == null ? other.getCityId() == null : this.getCityId().equals(other.getCityId()))
&& (this.getCityName() == null ? other.getCityName() == null : this.getCityName().equals(other.getCityName()))
&& (this.getCityCode() == null ? other.getCityCode() == null : this.getCityCode().equals(other.getCityCode()))
&& (this.getProvince() == null ? other.getProvince() == null : this.getProvince().equals(other.getProvince()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCityId() == null) ? 0 : getCityId().hashCode());
result = prime * result + ((getCityName() == null) ? 0 : getCityName().hashCode());
result = prime * result + ((getCityCode() == null) ? 0 : getCityCode().hashCode());
result = prime * result + ((getProvince() == null) ? 0 : getProvince().hashCode());
return result;
}
}
package edu.nf.project.model;
import java.util.ArrayList;
import java.util.List;
public class CityInfoExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public CityInfoExample() {
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 andCityIdIsNull() {
addCriterion("city_id is null");
return (Criteria) this;
}
public Criteria andCityIdIsNotNull() {
addCriterion("city_id is not null");
return (Criteria) this;
}
public Criteria andCityIdEqualTo(Integer value) {
addCriterion("city_id =", value, "cityId");
return (Criteria) this;
}
public Criteria andCityIdNotEqualTo(Integer value) {
addCriterion("city_id <>", value, "cityId");
return (Criteria) this;
}
public Criteria andCityIdGreaterThan(Integer value) {
addCriterion("city_id >", value, "cityId");
return (Criteria) this;
}
public Criteria andCityIdGreaterThanOrEqualTo(Integer value) {
addCriterion("city_id >=", value, "cityId");
return (Criteria) this;
}
public Criteria andCityIdLessThan(Integer value) {
addCriterion("city_id <", value, "cityId");
return (Criteria) this;
}
public Criteria andCityIdLessThanOrEqualTo(Integer value) {
addCriterion("city_id <=", value, "cityId");
return (Criteria) this;
}
public Criteria andCityIdIn(List<Integer> values) {
addCriterion("city_id in", values, "cityId");
return (Criteria) this;
}
public Criteria andCityIdNotIn(List<Integer> values) {
addCriterion("city_id not in", values, "cityId");
return (Criteria) this;
}
public Criteria andCityIdBetween(Integer value1, Integer value2) {
addCriterion("city_id between", value1, value2, "cityId");
return (Criteria) this;
}
public Criteria andCityIdNotBetween(Integer value1, Integer value2) {
addCriterion("city_id not between", value1, value2, "cityId");
return (Criteria) this;
}
public Criteria andCityNameIsNull() {
addCriterion("city_name is null");
return (Criteria) this;
}
public Criteria andCityNameIsNotNull() {
addCriterion("city_name is not null");
return (Criteria) this;
}
public Criteria andCityNameEqualTo(String value) {
addCriterion("city_name =", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameNotEqualTo(String value) {
addCriterion("city_name <>", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameGreaterThan(String value) {
addCriterion("city_name >", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameGreaterThanOrEqualTo(String value) {
addCriterion("city_name >=", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameLessThan(String value) {
addCriterion("city_name <", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameLessThanOrEqualTo(String value) {
addCriterion("city_name <=", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameLike(String value) {
addCriterion("city_name like", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameNotLike(String value) {
addCriterion("city_name not like", value, "cityName");
return (Criteria) this;
}
public Criteria andCityNameIn(List<String> values) {
addCriterion("city_name in", values, "cityName");
return (Criteria) this;
}
public Criteria andCityNameNotIn(List<String> values) {
addCriterion("city_name not in", values, "cityName");
return (Criteria) this;
}
public Criteria andCityNameBetween(String value1, String value2) {
addCriterion("city_name between", value1, value2, "cityName");
return (Criteria) this;
}
public Criteria andCityNameNotBetween(String value1, String value2) {
addCriterion("city_name not between", value1, value2, "cityName");
return (Criteria) this;
}
public Criteria andCityCodeIsNull() {
addCriterion("city_code is null");
return (Criteria) this;
}
public Criteria andCityCodeIsNotNull() {
addCriterion("city_code is not null");
return (Criteria) this;
}
public Criteria andCityCodeEqualTo(String value) {
addCriterion("city_code =", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeNotEqualTo(String value) {
addCriterion("city_code <>", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeGreaterThan(String value) {
addCriterion("city_code >", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeGreaterThanOrEqualTo(String value) {
addCriterion("city_code >=", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeLessThan(String value) {
addCriterion("city_code <", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeLessThanOrEqualTo(String value) {
addCriterion("city_code <=", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeLike(String value) {
addCriterion("city_code like", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeNotLike(String value) {
addCriterion("city_code not like", value, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeIn(List<String> values) {
addCriterion("city_code in", values, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeNotIn(List<String> values) {
addCriterion("city_code not in", values, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeBetween(String value1, String value2) {
addCriterion("city_code between", value1, value2, "cityCode");
return (Criteria) this;
}
public Criteria andCityCodeNotBetween(String value1, String value2) {
addCriterion("city_code not between", value1, value2, "cityCode");
return (Criteria) this;
}
public Criteria andProvinceIsNull() {
addCriterion("province is null");
return (Criteria) this;
}
public Criteria andProvinceIsNotNull() {
addCriterion("province is not null");
return (Criteria) this;
}
public Criteria andProvinceEqualTo(String value) {
addCriterion("province =", value, "province");
return (Criteria) this;
}
public Criteria andProvinceNotEqualTo(String value) {
addCriterion("province <>", value, "province");
return (Criteria) this;
}
public Criteria andProvinceGreaterThan(String value) {
addCriterion("province >", value, "province");
return (Criteria) this;
}
public Criteria andProvinceGreaterThanOrEqualTo(String value) {
addCriterion("province >=", value, "province");
return (Criteria) this;
}
public Criteria andProvinceLessThan(String value) {
addCriterion("province <", value, "province");
return (Criteria) this;
}
public Criteria andProvinceLessThanOrEqualTo(String value) {
addCriterion("province <=", value, "province");
return (Criteria) this;
}
public Criteria andProvinceLike(String value) {
addCriterion("province like", value, "province");
return (Criteria) this;
}
public Criteria andProvinceNotLike(String value) {
addCriterion("province not like", value, "province");
return (Criteria) this;
}
public Criteria andProvinceIn(List<String> values) {
addCriterion("province in", values, "province");
return (Criteria) this;
}
public Criteria andProvinceNotIn(List<String> values) {
addCriterion("province not in", values, "province");
return (Criteria) this;
}
public Criteria andProvinceBetween(String value1, String value2) {
addCriterion("province between", value1, value2, "province");
return (Criteria) this;
}
public Criteria andProvinceNotBetween(String value1, String value2) {
addCriterion("province not between", value1, value2, "province");
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);
}
}
}
六、自動生成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="edu.nf.project.mapper.CityInfoMapper">
<resultMap id="BaseResultMap" type="edu.nf.project.model.CityInfo">
<id column="city_id" jdbcType="INTEGER" property="cityId" />
<result column="city_name" jdbcType="VARCHAR" property="cityName" />
<result column="city_code" jdbcType="VARCHAR" property="cityCode" />
<result column="province" jdbcType="VARCHAR" property="province" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</whe