下图测试项目除了 com.xu.springboot 中的文件为创建项目是自带的其余全部代码为 Mybatis-Plus 代码自动生成 生成。
作者在项目中使用了 阿里的 druid 数据连接池,可以根据自己的实际情况使用其他数据库连接池。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
<relativePath />
parent>
<groupId>com.xu.erpgroupId>
<artifactId>erpartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>ERPname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
############################################################
# 服务端口配置
############################################################
server.port=8888
############################################################
# 静态资源配置
############################################################
spring.resources.static-locations=classpath:/templates,classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
############################################################
# thymelea 配置
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
############################################################
# MySQL 配置
############################################################
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource #SpringBoot默认数据源
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://192.168.0.111:3333/erp?serverTimezone=UTC&characterEncoding=utf8
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=60000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
############################################################
# mybatis-plus 配置
############################################################
mybatis-plus.type-aliases-package=com.xu.erp.entity
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
package com.xu.erp.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.extension.activerecord.Model;
/**
*
*
*
*
* @author hyacinth
* @since 2019-12-11
*/
public class Currency extends Model<Currency> {
private static final long serialVersionUID = 1L;
private Integer id;
private String tag;
private String name;
public Integer getId() {
return id;
}
public Currency setId(Integer id) {
this.id = id;
return this;
}
public String getTag() {
return tag;
}
public Currency setTag(String tag) {
this.tag = tag;
return this;
}
public String getName() {
return name;
}
public Currency setName(String name) {
this.name = name;
return this;
}
@Override
protected Serializable pkVal() {
return this.id;
}
}
package com.xu.erp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xu.erp.entity.Currency;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
*
* Mapper 接口
*
*
* @author hyacinth
* @since 2019-12-11
*/
@Mapper
public interface CurrencyMapper extends BaseMapper<Currency> {
//这个函数的函数名要和 StudentMapper.xml 中 id 一样
public List<Currency> all();
@Select("select * from currency where id=#{id}")
public List<Currency> mySelectById(Integer id);
}
<mapper namespace="com.xu.erp.mapper.CurrencyMapper">
<resultMap id="BaseResultMap" type="com.xu.erp.entity.Currency">
<id column="id" property="id" />
<result column="tag" property="tag" />
<result column="name" property="name" />
resultMap>
<sql id="Base_Column_List">
id, tag, name
sql>
<select id="all" resultType="com.xu.erp.entity.Currency">
select * from currency;
select>
mapper>
package com.xu.erp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xu.erp.entity.Currency;
import java.util.List;
/**
*
* 服务类
*
*
* @author hyacinth
* @since 2019-12-11
*/
public interface CurrencyService extends IService<Currency> {
public List<Currency> all();
public List<Currency> selectById(Integer id);
}
package com.xu.erp.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.erp.entity.Currency;
import com.xu.erp.mapper.CurrencyMapper;
import com.xu.erp.service.CurrencyService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
/**
*
* 服务实现类
*
*
* @author hyacinth
* @since 2019-12-11
*/
@Primary
@Service
public class CurrencyServiceImpl extends ServiceImpl<CurrencyMapper, Currency> implements CurrencyService {
@Autowired
private CurrencyMapper mapper;
@Override
public List<Currency> all() {
return mapper.all();
}
@Override
public List<Currency> selectById(Integer id) {
return mapper.mySelectById(id);
}
}
package com.xu.erp.entity.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xu.erp.service.CurrencyService;
/**
*
* 前端控制器
*
*
* @author hyacinth
* @since 2019-12-11
*/
@RestController
@RequestMapping("/erp/currency")
public class CurrencyController {
@Autowired
private CurrencyService service;
@RequestMapping("/test")
public Object test() {
return service.all();
}
@RequestMapping("/select")
public Object select() {
return service.selectById(1);
}
}
package com.xu.erp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = {"com.xu.erp.*","com.xu.erp.service.impl"})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2019-12-11 15:44:30.448 INFO 14316 --- [ main] com.xu.erp.Application : Starting Application on ZWJ0R16WWO114LL with PID 14316 (E:\SourceCode\Eclipse-2019-06\ERP\target\classes started by Administrator in E:\SourceCode\Eclipse-2019-06\ERP)
2019-12-11 15:44:30.454 INFO 14316 --- [ main] com.xu.erp.Application : The following profiles are active: test
2019-12-11 15:44:34.737 INFO 14316 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http)
2019-12-11 15:44:34.757 INFO 14316 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-11 15:44:34.757 INFO 14316 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-11 15:44:34.951 INFO 14316 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-12-11 15:44:34.951 INFO 14316 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4304 ms
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.3.0
2019-12-11 15:44:38.139 INFO 14316 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-11 15:44:38.608 INFO 14316 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ''
2019-12-11 15:44:38.614 INFO 14316 --- [ main] com.xu.erp.Application : Started Application in 9.14 seconds (JVM running for 9.967)
2019-12-11 15:45:01.337 INFO 14316 --- [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-12-11 15:45:01.337 INFO 14316 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-12-11 15:45:01.354 INFO 14316 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
2019-12-11 15:45:01.543 INFO 14316 --- [nio-8888-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-12-11 15:45:02.263 INFO 14316 --- [nio-8888-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.