近日基于mybatis-plus的应用做了应用体验,现将主要步骤进行记录,以便后日使用可参考借鉴
1.新建springboot应用,仅仅保留pom.xml文件
a.基础依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.14</version> <relativePath/> </parent>
b.GAV设置 <groupId>org.tjgeo.mybtisx</groupId> <artifactId>mybatisxtest</artifactId> <version>1.0-SNAPSHOT</version>
c.设置pom类型
<packaging>pom</packaging>
d.依赖管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<!-- db连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.16</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- h2database-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<!-- pager-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
</dependency>
</dependencies>
</dependencyManagement>
e.基础依赖
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
f.公共插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.新增公共模块test00
2.1模块新建
a.使用父依赖 <parent> <groupId>org.tjgeo.mybtisx</groupId> <artifactId>mybatisxtest</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> b.设置gav <artifactId>test00</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> c.插件设置 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <classifier>exec</classifier> </configuration> </plugin> </plugins> </build> e.依赖设置 <dependencies> <!-- db连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- h2database--> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- pager--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> </dependencies>
2.2公共代码
2.2.1AjaxQuery
package com.tjgeo.mybatisxtest.test00.json; /** * 请求参数 */ public class AjaxQuery<T> { /** * 认证KEY */ private String key; /** * 查询参数 */ private T data; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
2.2.2AjaxResult
package com.tjgeo.mybatisxtest.test00.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; /** * Ajax返回快速封装 * * @param <T> * @author liskying */ public class AjaxResult<T> { /** * 状态码 */ private String status; /** * 提示语 */ private String msg; /** * 数据集合 */ private T data; /** * @param status * @param msg * @param data */ public AjaxResult(String status, String msg, T data) { this.status = status; this.msg = msg; this.data = data; } /** * @param status * @param msg */ public AjaxResult(String status, String msg) { this.status = status; this.msg = msg; } /** * 成功返回数据 * * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> success(T data) { return new AjaxResult<>(AjaxStatus.SUCCESS.status, AjaxStatus.SUCCESS.msg, data); } /** * 成功返回数据 * * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> success(String msg) { return new AjaxResult<>(AjaxStatus.SUCCESS.status, msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> success() { return new AjaxResult<>(AjaxStatus.SUCCESS.status, AjaxStatus.SUCCESS.msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> getSuccess(T data) { return new AjaxResult<>(AjaxStatus.GET_SUCCESS.status, AjaxStatus.GET_SUCCESS.msg, data); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> getSuccess(String msg) { return new AjaxResult<>(AjaxStatus.GET_SUCCESS.status, msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> getSuccess() { return new AjaxResult<>(AjaxStatus.GET_SUCCESS.status, AjaxStatus.GET_SUCCESS.msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> addSuccess() { return new AjaxResult<T>(AjaxStatus.ADD_SUCCESS.status, AjaxStatus.ADD_SUCCESS.msg); } /** * @param data * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> addSuccess(T data) { return new AjaxResult<>(AjaxStatus.ADD_SUCCESS.status, AjaxStatus.ADD_SUCCESS.msg, data); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> deleteSuccess() { return new AjaxResult<>(AjaxStatus.DELETE_SUCCESS.status, AjaxStatus.DELETE_SUCCESS.msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> updateSuccess() { return new AjaxResult<>(AjaxStatus.UPDATE_SUCCESS.status, AjaxStatus.UPDATE_SUCCESS.msg); } /** * @param data * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> updateSuccess(T data) { return new AjaxResult<>(AjaxStatus.UPDATE_SUCCESS.status, AjaxStatus.UPDATE_SUCCESS.msg, data); } /** * @param msg * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> fail(String msg) { return new AjaxResult<>(AjaxStatus.FAIL.status, msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> fail() { return new AjaxResult<>(AjaxStatus.FAIL.status, AjaxStatus.FAIL.msg); } /** * @param msg * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> getFail(String msg) { return new AjaxResult<>(AjaxStatus.GET_FAIL.status, msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> getFail() { return new AjaxResult<>(AjaxStatus.GET_FAIL.status, AjaxStatus.FAIL.msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> addFail() { return new AjaxResult<>(AjaxStatus.ADD_FAIL.status, AjaxStatus.ADD_FAIL.msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> deleteFail() { return new AjaxResult<>(AjaxStatus.DELETE_FAIL.status, AjaxStatus.DELETE_FAIL.msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> updateFail() { return new AjaxResult<>(AjaxStatus.UPDATE_FAIL.status, AjaxStatus.UPDATE_FAIL.msg); } /** * @param msg * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> error(String msg) { return new AjaxResult<>(AjaxStatus.ERROR.status, msg); } /** * @param <T> 返回数据类型 * @return json对象 */ public static <T> AjaxResult<T> error() { return new AjaxResult<>(AjaxStatus.ERROR.status, AjaxStatus.ERROR.msg); } /** * @return */ public String getStatus() { return status; } /** * @param status */ public void setStatus(String status) { this.status = status; } /** * @return */ public String getMsg() { return msg; } /** * @param msg */ public void setMsg(String msg) { this.msg = msg; } /** * */ public T getData() { return data; } /** * */ public void setData(T data) { this.data = data; } /** * 如果字段为null,该字段不显示 */ @Override public String toString() { return JSON.toJSONString(this); } /** * 返回全部字段,包括null */ public String toAllString() { return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue); } }
2.2.3AjaxStatus
package com.tjgeo.mybatisxtest.test00.json; /** * 状态 */ public enum AjaxStatus { /** * */ FAIL("101", "失败"), /** * */ GET_FAIL("111", "查询失败"), /** * */ ADD_FAIL("121", "添加失败"), /** * */ DELETE_FAIL("131", "删除失败"), /** * */ UPDATE_FAIL("141", "修改失败"), /** * */ SUCCESS("100", "成功"), /** * */ GET_SUCCESS("110", "查询成功"), /** * */ ADD_SUCCESS("120", "添加成功"), /** * */ DELETE_SUCCESS("130", "删除成功"), /** * */ UPDATE_SUCCESS("140", "修改成功"), /** * */ ERROR("201", "错误"), /** * */ USER_NOFOUND("211", "用户不存在"), /** * */ ERROR_ACCOUNT("212", "账号或密码错误"), /** * */ USER_EXIST("213", "用户已存在"), /** * */ USER_LOCK("214", "账号被锁定,请联系管理员"), /** * */ IP_LOCK("215", "IP 被锁定,请联系管理员"), /** * */ PARAM_ERROR("303", "参数错误"), /** * */ Token_Expired("1044", "token Invalid expired"); /** * 状态码 */ public String status; /** * 提示语 */ public String msg; AjaxStatus(String status, String msg) { this.status = status; this.msg = msg; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
2.2.5AjaxPageQuery
package com.tjgeo.mybatisxtest.test00.json; /** * 分页查询 * * @param <T> * @author liskying */ public class AjaxPageQuery<T> extends AjaxQuery<T> { /** * 分页页码 */ private int pageIndex; /** * 分页大小 */ private int pageSize; /** * 排序 */ private String sort; /** * */ public AjaxPageQuery() { pageIndex = 1; pageSize = 20; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
2.2.5启动设置
package com.tjgeo.mybatisxtest.test00; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @author liskying */ @SpringBootApplication public class Test00Application { public static void main(String[] args) { SpringApplication.run(Test00Application.class, args); } }
3.新增业务模块test01
3.1pom设置
<?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"> <parent> <artifactId>mybatisxtest</artifactId> <groupId>org.tjgeo.mybtisx</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>test01</artifactId> <packaging>jar</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <!-- spring-boot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> </dependency> <dependency> <groupId>org.tjgeo.mybtisx</groupId> <artifactId>test00</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> </dependencies> </project>
3.2资源配置
a.application.yaml
spring: profiles: active: lwxdeepin mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mybatis: type-aliases-package: com.tjgeo.mybatisxtest.dao mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # pagehelper pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql #showSql logging: level: top:
b.application-lwxdeepin.yaml
server: port: 8180 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 username: root password: 123456
b.数据库设置
create table XtDictionary ( Id varchar(36) not null primary key, XtType varchar(100) null, XtName varchar(100) null, XtValue varchar(100) null, XtSort int null ) collate = utf8_general_ci; create index XtDictionary_xtType_IDX on XtDictionary (XtType);
4.模板自动类生成
5.API测试
标签:status,AjaxResult,return,记实,Api,plus,msg,public,AjaxStatus From: https://www.cnblogs.com/oumi/p/17609692.html