1、pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>yu</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</name>
<description>springboot-demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 自定义依赖 -->
<!--Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<!-- 自定义依赖 结束-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、application.yml
server:
port: 8080
servlet:
context-path: /
tomcat:
uri-encoding: utf-8
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/db_mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
# 初始化时建立物理连接的个数
initial-size: 5
# 最大连接池数量
max-active: 20
# 最小连接池数量
min-idle: 5
# 获取连接时最大等待时间
max-wait: 60000
# 是否缓存preparedStatement,也就是PSCache。
pool-prepared-statements: false
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
max-pool-prepared-statement-per-connection-size: -1
# 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。
validation-query: SELECT 'x'
# 单位:秒,检测连接是否有效的超时时间。
validation-query-timeout: 1
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-on-borrow: false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-on-return: false
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
test-while-idle: true
# 有两个含义:1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
time-between-eviction-runs-millis: 6000
# 连接保持空闲而不被驱逐的最小时间
min-evictable-idle-time-millis: 1800000
# 监控页面相关配置
stat-view-servlet:
url-pattern: /druid/*
login-username: admin
login-password: admin
# filter相关配置
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
type: com.alibaba.druid.pool.DruidDataSource
devtools:
add-properties: false
thymeleaf:
cache: false
mode: HTML
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
enabled: true
check-template: false
check-template-location: false
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: always
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: yu.entity
#驼峰
configuration:
map-underscore-to-camel-case: true
use-generated-keys: false
logging:
level:
web: debug
3、创建 User.java
import lombok.Data;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
public class User {
private Integer id;
private String name;
private String pwd;
private String description;
private String phone;
private Date create_time;
private Date update_time;
}
4、创建 UserMapper.java
import org.apache.ibatis.annotations.Mapper;
import yu.entity.User;
import java.util.List;
@Mapper
public interface UserMapper {
public List<User> findAll();
}
5、创建UserService.java
package yu.service;
import yu.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
6、创建 UserServiceImpl.java
package yu.service;
import org.springframework.stereotype.Service;
import yu.entity.User;
import yu.mapper.UserMapper;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
7、创建 UserController.java
package yu.controller;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import yu.service.UserService;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class UserController {
@Resource
UserService userService;
@ResponseBody
@RequestMapping("/index")
String index(){
List list= userService.findAll();
return JSON.toJSONString(list);
}
}
8、创建 UserMapper.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">
<!--namespace=绑定一个对应的Mapper接口-->
<mapper namespace="yu.mapper.UserMapper">
<!--select查询语句-->
<select id="findAll" resultType="User">
select * from t_user
</select>
</mapper>
9、修改 SpringbootApplication.java
package yu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import yu.utils.GetHostIP;
//使用MapperScan批量扫描所有的Mapper接口
//@MapperScan(basePackages = "yu.mapper")
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) throws Exception{
// SpringApplication.run(SpringbootApplication.class, args);
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Environment environment = context.getBean(Environment.class);
System.out.println("SpringBoot + Mybatis + Security + thymeleaf 启动成功");
System.out.println("前台页面,请访问:http://" + GetHostIP.getWlanIp()+":"+environment.getProperty("server.port")+environment.getProperty("server.servlet.context-path"));
System.out.println("后台页面,请访问:http://" +GetHostIP.getRealIP()+":"+environment.getProperty("server.port")+"/admin");
}
}