1. 程式人生 > >微服務架構實戰篇(三):Spring boot2.0 + Mybatis + PageHelper實現增刪改查和分頁查詢功能

微服務架構實戰篇(三):Spring boot2.0 + Mybatis + PageHelper實現增刪改查和分頁查詢功能

簡介

該專案主要利用Spring boot2.0 +Mybatis + PageHelper實現增刪改查和分頁查詢功能,快速搭建一套和資料庫互動的專案。

小工具一枚,歡迎使用和Star支援,如使用過程中碰到問題,可以提出Issue,我會盡力完善該Starter

版本基礎

  • Spring Boot:2.0.4
  • Mybatis:3.4.5
  • Druid:1.1.10
  • PageHelper:4.1.6

操作步驟

第一步:下載SpringBoot2.0+Swagger2專案

第二步:新增maven依賴(完整依賴)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itunion</groupId>
    <artifactId>spring-boot-starter-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-starter-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- 打war包用 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <!-- 打war包用 -->
        <finalName>demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 打war包用,maven打包的時候告訴maven不需要web.xml,否剛會報找不到web.xml錯誤 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <!-- -->
        </plugins>
    </build>
</project>

第三步:application.properties 配置(完整配置)

server.port=8081

server.servlet.context-path=/Demo

server.tomcat.accesslog.enabled=true

druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://127.0.0.1:3306/資料庫名稱?serverTimezone=Asia/Shanghai&allowMultiQueries=true
druid.username=資料庫使用者名稱
druid.password=資料庫密碼
druid.init-size=1
druid.min-idel=1
druid.max-active=5
druid.login.timeout.seconds=30
druid.query.timeout.seconds=30

swagger.enable=true

第四步:增加mybatis-config.xml檔案

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="true"/>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="600"/>
        <setting name="callSettersOnNulls" value="true"/>
    </settings>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="sqlserver"/>
        </plugin>
    </plugins>
    <mappers>
        <mapper resource="mappers/UserInfoMapper.xml"/>
    </mappers>
</configuration>

備註

  • PageHelper是分頁外掛
  • UserInfoMapper.xml 是我們本篇文章要實現丟使用者的增刪改查對映檔案

第五步:建立資料庫連線池DataSource(DatasourceConfig.java)

package com.itunion.springbootstartermybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.itunion.springbootstartermybatis.mapper")
public class DatasourceConfig {
    private static Logger log = LoggerFactory.getLogger(DatasourceConfig.class);
    @Value("${druid.driver}")
    private String driverClassName;
    @Value("${druid.url}")
    private String url;
    @Value("${druid.username}")
    private String username;
    @Value("${druid.password}")
    private String password;
    @Value("${druid.init-size}")
    private int initSize;
    @Value("${druid.min-idel}")
    private int minIdel;
    @Value("${druid.max-active}")
    private int maxActive;
    @Value("${druid.login.timeout.seconds}")
    private int loginTimeoutSeconds;
    @Value("${druid.query.timeout.seconds}")
    private int queryTimeoutSeconds;

    @Bean
    public DataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setInitialSize(initSize);
        ds.setMinIdle(minIdel);
        ds.setMaxActive(maxActive);
        ds.setLoginTimeout(loginTimeoutSeconds);
        ds.setQueryTimeout(queryTimeoutSeconds);
        return ds;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource());
        sqlSessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        sqlSessionFactory.setFailFast(true);
        return sqlSessionFactory.getObject();
    }

    public DataSourceTransactionManager dataSourceTransactionManager() {
        log.debug("> transactionManager");
        return new DataSourceTransactionManager(dataSource());
    }


    @PostConstruct
    public void postConstruct() {
        log.info("jdbc settings={}", this);
    }
}

第六步:建立UserInfoMapper.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.itunion.springbootstartermybatis.mapper.UserInfoMapper">
  <resultMap id="BaseResultMap" type="com.itunion.springbootstartermybatis.model.UserInfo">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="content" jdbcType="VARCHAR" property="content" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
  </resultMap>
  <sql id="Base_Column_List">
    id, user_name, password, sex, content, create_date, update_date
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user_info
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user_info
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.itunion.springbootstartermybatis.model.UserInfo">
    insert into user_info (id, user_name, password, 
      sex, content, create_date, 
      update_date)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{sex,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, 
      #{updateDate,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.itunion.springbootstartermybatis.model.UserInfo">
    insert into user_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="userName != null">
        user_name,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="content != null">
        content,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        #{content,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null">
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateDate != null">
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.itunion.springbootstartermybatis.model.UserInfo">
    update user_info
    <set>
      <if test="userName != null">
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        sex = #{sex,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null">
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.itunion.springbootstartermybatis.model.UserInfo">
    update user_info
    set user_name = #{userName,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR},
      create_date = #{createDate,jdbcType=TIMESTAMP},
      update_date = #{updateDate,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>


    <select id="getUserInfoList" parameterType="com.itunion.springbootstartermybatis.web.dto.UserInfoReqListDto" resultType="com.itunion.springbootstartermybatis.web.dto.UserInfoDto">
        select
        id, user_name as userName, password, sex, content, create_date as createDate, update_date as updateDate
        from user_info
    </select>
</mapper>
  • 實現了對使用者表的增刪改查和批量查詢sql語句

第七步:編寫UserInfo實體類

package com.itunion.springbootstartermybatis.model;

import java.util.Date;

public class UserInfo {
    private Integer id;

    private String userName;

    private String password;

    private String sex;

    private String content;

    private Date createDate;

    private Date updateDate;

    此處省略get/set方法
}

第八步:編寫UserInfoMapper

package com.itunion.springbootstartermybatis.mapper;
import com.itunion.springbootstartermybatis.model.UserInfo;
import com.itunion.springbootstartermybatis.web.dto.UserInfoReqListDto;
import com.itunion.springbootstartermybatis.web.dto.UserInfoDto;
import java.util.List;

public interface UserInfoMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(UserInfo record);

    int insertSelective(UserInfo record);

    UserInfo selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(UserInfo record);

    int updateByPrimaryKey(UserInfo record);

    /**
     * 獲取使用者資訊集合
     * @return
     */
    List<UserInfoDto> getUserInfoList(UserInfoReqListDto userInfoReqListDto);
}

第九步:編寫UserInfoService介面和介面實現

UserInfoService.java

package com.itunion.springbootstartermybatis.service;

import com.github.pagehelper.PageInfo;
import com.itunion.springbootstartermybatis.web.dto.UserInfoDto;
import com.itunion.springbootstartermybatis.web.dto.UserInfoReqListDto;

import java.util.List;

/**
 * 使用者資料服務
 * Created by lin on 2018年06月07日21:12:04
 */
public interface UserInfoService {
    /**
     * 新增使用者資訊
     */
    int addUserInfo(UserInfoDto record);

    /**
     * 查詢所有使用者資訊
     */
    List<UserInfoDto> getUserInfoList();

    /**
     * 根據使用者ID刪除使用者資訊
     */
    int delUserInfoById(Integer id);

    /**
     * 根據使用者ID修改使用者資訊
     */
    int modifyUserInfoById(UserInfoDto record);

    /**
     * 分頁查詢
     */
    PageInfo getUserInfoListPage(UserInfoReqListDto userInfoReqListDto);
}

UserInfoServiceImpl.java

package com.itunion.springbootstartermybatis.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itunion.springbootstartermybatis.mapper.UserInfoMapper;
import com.itunion.springbootstartermybatis.model.UserInfo;
import com.itunion.springbootstartermybatis.service.UserInfoService;
import com.itunion.springbootstartermybatis.web.dto.UserInfoDto;
import com.itunion.springbootstartermybatis.web.dto.UserInfoReqListDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 使用者資訊-服務提供方
 * Created by lin on 2018年06月07日21:48:13
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    private final static Logger LOGGER = LoggerFactory.getLogger(UserInfoServiceImpl.class);
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int addUserInfo(UserInfoDto record) {
        LOGGER.info("進入使用者資訊-服務提供方-UserInfoServiceImpl.addUserInfo[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(record, userInfo);
        userInfo.setUpdateDate(new Date());
        return userInfoMapper.insertSelective(userInfo);
    }

    @Override
    public List<UserInfoDto> getUserInfoList() {
        LOGGER.info("進入使用者資訊-服務提供方-UserInfoServiceImpl.getUserInfoList[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        return userInfoMapper.getUserInfoList(null);
    }

    @Override
    public int delUserInfoById(Integer id) {
        LOGGER.info("進入使用者資訊-服務提供方-UserInfoServiceImpl.delUserInfoById[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        return userInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int modifyUserInfoById(UserInfoDto record) {
        LOGGER.info("進入使用者資訊-服務提供方-UserInfoServiceImpl.modifyUserInfoById[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(record, userInfo);
        userInfo.setUpdateDate(new Date());
        return userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public PageInfo getUserInfoListPage(UserInfoReqListDto userInfoReqListDto) {
        LOGGER.info("進入使用者資訊-服務提供方-UserInfoServiceImpl.getUserInfoListPage[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        PageHelper.startPage(userInfoReqListDto.getPage(), userInfoReqListDto.getSize());
        List<UserInfoDto> userInfoDtos = userInfoMapper.getUserInfoList(userInfoReqListDto);
        PageInfo<UserInfoDto> userInfoDtoPageInfo = new PageInfo<>(userInfoDtos);
        return userInfoDtoPageInfo;
    }
}

第十步:編寫UserInfoController

package com.itunion.springbootstartermybatis.web.controller;

import com.github.pagehelper.PageInfo;
import com.itunion.springbootstartermybatis.common.UniformResultTemplate;
import com.itunion.springbootstartermybatis.service.UserInfoService;
import com.itunion.springbootstartermybatis.web.dto.UserInfoDto;
import com.itunion.springbootstartermybatis.web.dto.UserInfoReqListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 使用者資訊-移動端消費方
 * Created by lin on 2018年06月07日22:02:07
 */
@Controller
@RequestMapping("userInfo")
@Api(description = "測試移動")
public class UserInfoController {
    private Logger LOGGER = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    UserInfoService userInfoService;

    @ApiOperation(value = "getUserInfoList", notes = "查詢所有使用者資訊")
    @RequestMapping(value = "getUserInfoList", method = RequestMethod.GET)
    @ResponseBody
    public UniformResultTemplate<List<UserInfoDto>> getUserInfoList() {
        LOGGER.info("進入使用者資訊-移動端消費方-UserInfoController.getUserInfoList[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UniformResultTemplate<List<UserInfoDto>> uniformResultTemplate = new UniformResultTemplate<>();
        uniformResultTemplate.setCode(100);
        uniformResultTemplate.setMessage("查詢所有使用者資訊成功!");
        uniformResultTemplate.setResult(userInfoService.getUserInfoList());
        return uniformResultTemplate;
    }

    @ApiOperation(value = "getUserInfoListPage", notes = "查詢所有使用者資訊-分頁")
    @ApiImplicitParam(name = "userInfoReqListDto", value = "{\"page\":\"1\",\"size\":\"2\"}")
    @RequestMapping(value = "getUserInfoListPage", method = RequestMethod.POST)
    @ResponseBody
    public UniformResultTemplate<PageInfo> getUserInfoList(@RequestBody UserInfoReqListDto userInfoReqListDto) {
        LOGGER.info("進入使用者資訊-移動端消費方-UserInfoController.getUserInfoListPage[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UniformResultTemplate<PageInfo> uniformResultTemplate = new UniformResultTemplate<>();
        uniformResultTemplate.setCode(100);
        uniformResultTemplate.setMessage("查詢所有使用者資訊成功分頁!");
        uniformResultTemplate.setResult(userInfoService.getUserInfoListPage(userInfoReqListDto));
        return uniformResultTemplate;
    }

    @ApiOperation(value = "addUserInfo", notes = "新增使用者資訊")
    @ApiImplicitParam(name = "userInfoDto", value = "{\"userName\":\"測試使用者名稱\",\"password\":\"000000\",\"sex\":1,\"content\":\"這裡是IT實戰聯哦~~~\"}")
    @RequestMapping(value="addUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public UniformResultTemplate<String> addUserInfo(@RequestBody UserInfoDto userInfoDto) {

    LOGGER.info("進入使用者資訊-移動端消費方-UserInfoController.addUserInfo[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UniformResultTemplate<String> uniformResultTemplate = new UniformResultTemplate<>();
        Integer num = userInfoService.addUserInfo(userInfoDto);
        if(num > 0){
            uniformResultTemplate.setCode(100);
            uniformResultTemplate.setMessage("新增使用者資訊成功!");
            uniformResultTemplate.setResult(num+"");
        }else{
            uniformResultTemplate.setCode(400);
            uniformResultTemplate.setMessage("新增使用者資訊失敗!");
            uniformResultTemplate.setResult(num+"");
        }
        return uniformResultTemplate;
    }


    @ApiOperation(value="delUserInfoById", notes="根據使用者ID刪除使用者資訊")
    @ApiImplicitParam(name = "id", value = "4" , paramType="path" , dataType="Integer")
    @RequestMapping(value="delUserInfoById/{id}", method = RequestMethod.POST)
    @ResponseBody
    public UniformResultTemplate<String> deleteKdgVipItem(@PathVariable Integer id) {
        LOGGER.info("進入使用者資訊-移動端消費方-UserInfoController.delUserInfoById[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UniformResultTemplate<String> uniformResultTemplate = new UniformResultTemplate<>();
        Integer num = userInfoService.delUserInfoById(id);
        if(num > 0){
            uniformResultTemplate.setCode(100);
            uniformResultTemplate.setMessage("根據使用者ID刪除使用者資訊成功!");
            uniformResultTemplate.setResult(num+"");
        }else{
            uniformResultTemplate.setCode(400);
            uniformResultTemplate.setMessage("根據使用者ID刪除使用者資訊失敗!");
            uniformResultTemplate.setResult(num+"");
        }
        return uniformResultTemplate;
    }

    @ApiOperation(value = "modifyUserInfo", notes = "修改使用者資訊")
    @ApiImplicitParam(name = "userInfoDto", value = "{\"id\":10,\"userName\":\"測試修改使用者名稱\",\"password\":55555,\"sex\":1,\"content\":\"這裡是最新的IT實戰聯哦~~~\"}")
    @RequestMapping(value="modifyUserInfo", method = RequestMethod.POST, produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public UniformResultTemplate<String> modifyUserInfo(@RequestBody UserInfoDto userInfoDto) {
        LOGGER.info("進入使用者資訊-移動端消費方-UserInfoController.modifyUserInfo[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "]");
        UniformResultTemplate<String> uniformResultTemplate = new UniformResultTemplate<>();
        Integer num = userInfoService.modifyUserInfoById(userInfoDto);
        if(num > 0){
            uniformResultTemplate.setCode(100);
            uniformResultTemplate.setMessage("修改使用者資訊成功!");
            uniformResultTemplate.setResult(num+"");
        }else{
            uniformResultTemplate.setCode(400);
            uniformResultTemplate.setMessage("修改使用者資訊失敗!");
            uniformResultTemplate.setResult(num+"");
        }
        return uniformResultTemplate;
    }
}

第十一步:SpringBootApplication 啟動

package com.itunion.springbootstartermybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class SpringBootStarterMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootStarterMybatisApplication.class, args);
    }
}

第十二步:訪問Swagger介面地址

http://127.0.0.1:8080/Demo/api

如下圖所示:

微服務架構實戰篇(三):Spring boot2.0 + Mybatis實現增刪改查功能.jpg

  • 埠號已自己配置為準

貢獻者

更多精彩內容可以關注“IT實戰聯盟”公*眾*號哦~~~