mybatis-geneator

2019-08-09 00:00:00 mybatis

一、简介

在使用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} </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 close=")" collection="criterion.value" item="listItem" open="(" 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="(" prefixOverrides="and" suffix=")">
            <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 close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List"> city_id, city_name, city_code, province </sql>
  <select id="selectByExample" parameterType="edu.nf.project.model.CityInfoExample" resultMap="BaseResultMap"> select <if test="distinct"> distinct </if>
    <include refid="Base_Column_List" /> from city_info <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null"> order by ${orderByClause} </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from city_info where city_id = #{cityId,jdbcType=INTEGER} </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from city_info where city_id = #{cityId,jdbcType=INTEGER} </delete>
  <delete id="deleteByExample" parameterType="edu.nf.project.model.CityInfoExample"> delete from city_info <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="edu.nf.project.model.CityInfo"> insert into city_info (city_id, city_name, city_code, province) values (#{cityId,jdbcType=INTEGER}, #{cityName,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}, #{province,jdbcType=VARCHAR}) </insert>
  <insert id="insertSelective" parameterType="edu.nf.project.model.CityInfo"> insert into city_info <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="cityId != null"> city_id, </if>
      <if test="cityName != null"> city_name, </if>
      <if test="cityCode != null"> city_code, </if>
      <if test="province != null"> province, </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="cityId != null"> #{cityId,jdbcType=INTEGER}, </if>
      <if test="cityName != null"> #{cityName,jdbcType=VARCHAR}, </if>
      <if test="cityCode != null"> #{cityCode,jdbcType=VARCHAR}, </if>
      <if test="province != null"> #{province,jdbcType=VARCHAR}, </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="edu.nf.project.model.CityInfoExample" resultType="java.lang.Long"> select count(*) from city_info <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map"> update city_info <set>
      <if test="record.cityId != null"> city_id = #{record.cityId,jdbcType=INTEGER}, </if>
      <if test="record.cityName != null"> city_name = #{record.cityName,jdbcType=VARCHAR}, </if>
      <if test="record.cityCode != null"> city_code = #{record.cityCode,jdbcType=VARCHAR}, </if>
      <if test="record.province != null"> province = #{record.province,jdbcType=VARCHAR}, </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map"> update city_info set city_id = #{record.cityId,jdbcType=INTEGER}, city_name = #{record.cityName,jdbcType=VARCHAR}, city_code = #{record.cityCode,jdbcType=VARCHAR}, province = #{record.province,jdbcType=VARCHAR} <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="edu.nf.project.model.CityInfo"> update city_info <set>
      <if test="cityName != null"> city_name = #{cityName,jdbcType=VARCHAR}, </if>
      <if test="cityCode != null"> city_code = #{cityCode,jdbcType=VARCHAR}, </if>
      <if test="province != null"> province = #{province,jdbcType=VARCHAR}, </if>
    </set> where city_id = #{cityId,jdbcType=INTEGER} </update>
  <update id="updateByPrimaryKey" parameterType="edu.nf.project.model.CityInfo"> update city_info set city_name = #{cityName,jdbcType=VARCHAR}, city_code = #{cityCode,jdbcType=VARCHAR}, province = #{province,jdbcType=VARCHAR} where city_id = #{cityId,jdbcType=INTEGER} </update>
</mapper>

七、以及service dao controller(这里只复制controller)

package edu.nf.project.controller; import com.wang.common.base.BaseController; import com.wang.common.model.base.result.DataResult; import com.wang.common.uitl.IdGenerator; import com.wang.common.model.page.PageInfoResult; import com.wang.common.model.page.PageParam; import edu.nf.project.model.CityInfo; import edu.nf.project.model.CityInfoExample; import edu.nf.project.service.CityInfoService; import com.github.pagehelper.PageInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; /** * controller * Created by Administrator on 2019-06-20T11:55:04.44. * * @author Administrator */ @RestController @ResponseBody @RequestMapping(value = "/cityInfo") @Api(value = "控制器", description = "管理") public class CityInfoController extends BaseController { private static final Logger LOGGER = LoggerFactory.getLogger(CityInfoController.class); @Resource private CityInfoService cityInfoService; /** * 列表 * * @param pageParam 分页参数 * @return DataResult */ @GetMapping(value = "/list") @ApiOperation(value = "列表") public PageInfoResult<CityInfo> list(PageParam pageParam) { CityInfoExample cityInfoExample = new CityInfoExample(); List<CityInfo> cityInfoList = cityInfoService.selectByExampleForStartPage(cityInfoExample, pageParam.getPageNum(), pageParam.getPageSize()); long total = cityInfoService.countByExample(cityInfoExample); return new PageInfoResult<>(cityInfoList, total); } /** * 添加 * * @param cityInfo 数据 * @return DataResult */ @PostMapping(value = "/add") @ApiOperation(value = "添加") @RequiresPermissions("cityInfo:add") public DataResult add(@RequestBody CityInfo cityInfo) { cityInfo.setCityId((int)(IdGenerator.getId())); cityInfoService.insertSelective(cityInfo); return new DataResult("success", "保存成功"); } /** * 删除 * * @param id 主键 * @return DataResult */ @GetMapping(value = "/delete") @ApiOperation(value = "删除") @RequiresPermissions("cityInfo:delete") public DataResult delete(Long id) { cityInfoService.deleteByPrimaryKey(id); return new DataResult("success", "删除成功"); } /** * 修改 * * @param cityInfo 数据 * @return DataResult */ @PostMapping(value = "/update") @ApiOperation(value = "修改") @RequiresPermissions("cityInfo:update") public DataResult update(@RequestBody CityInfo cityInfo) { CityInfoExample cityInfoExample = new CityInfoExample(); cityInfoExample.createCriteria().andCityIdEqualTo(cityInfo.getCityId()); cityInfoService.updateByExampleSelective(cityInfo, cityInfoExample); return new DataResult("success", "修改成功"); } /** * 详情 * * @param id 主键 * @return DataResult */ @GetMapping(value = "/detail") @ApiOperation(value = "详情") @RequiresPermissions("cityInfo:detail") public DataResult<CityInfo> detail(Long id) { CityInfo cityInfo = cityInfoService.selectByPrimaryKey(id); return new DataResult<>(cityInfo); } }

 八、配图(本案例用springboot 项目构建,模块化开发)

《mybatis-geneator》

 

《mybatis-geneator》

《mybatis-geneator》

 

相关文章