一:项目搭建框架:
代码实现:
Controller:
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("/hello")
public String hello() {
return demoService.hello();
}
@GetMapping("/query")
public List<User> query() {
return demoService.query();
}
@GetMapping("/insert")
public void insert(User user) {
demoService.insert(user);
}
@GetMapping("/del")
public void del(Long id) {
demoService.del(id);
}
@GetMapping("/update")
public void update(User user) {
demoService.update(user);
}
}
DAO:
@Repository
public interface DemoDao {
List<User> query();
void del(Long id);
void insert(User user);
void update(User user);
}
SERVICE:
@Service
public class DemoService {
@Autowired
private DemoDao demoDao;
public String hello() {
return "Hello World !";
}
public List<User> query() {
return this.demoDao.query();
}
@Transactional
public void insert(User user) {
user.setName("three");
demoDao.insert(user);
}
@Transactional
public void del(Long id) {
demoDao.del(id);
}
@Transactional
public void update(User user) {
demoDao.update(user);
}
}
Mapper.xml
在settings元素中设置的全局useGeneratedKeys参数对于xml映射器无效。如果希望在xml映射器中执行添加记录之后返回主键ID,则必须在xml映射器中明确设置useGeneratedKeys参数值为true
<?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.example.demo.db.dao.DemoDao*">--命名空间
<select id="query" parameterType="java.util.Map" resultType="com.example.demo.db.entity.User">
select * from t_user
</select>
<insert id="insert" parameterType="com.example.demo.db.entity.User">
INSERT INTO `user`.`t_user` ( `name` )
VALUES(
#{name}
);
</insert>
<delete id="del" parameterType="com.example.demo.db.entity.User">
delete from t_user where id=#{id}
</delete>
<update id="update" parameterType="com.example.demo.db.entity.User">
UPDATE `user`.`t_user` SET `name` = #{name} WHERE `id` = #{id};
</update>
</mapper>
yml的配置文件:
`server:
port: 8081 //端口号
servlet:
context-path: /demo
spring:
jackson:
default-property-inclusion: non_null
time-zone: GMT+8
date-format: yyyy-MM-dd
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
thymeleaf:
mode: LEGACYHTML5
cache: false
redis:
cluster:
nodes: 10.92.1.16:7000,10.92.1.16:7001,10.92.1.16:7002,10.92.1.16:7003,10.92.1.16:7004,10.92.1.16:7005
max-redirects: 3
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC //数据库的配置
username: root
password: root
hikari:
minimum-idle: 5
maximum-pool-size: 15
auto-commit: true
idle-timeout: 30000
pool-name: DatebookHikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.tkamc.process.db.entity
`
pom的配置依赖:
<?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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>