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.1.7.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> <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.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--web相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis相关--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!--mysql相关--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.yml</include> </includes> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建application.yml文件
server: port: 8080 spring: datasource: username: root password: 082112 #url中database为对应的数据库名称 url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.example.demo.entity #showSql logging: level: com.example.demo.mapper: debug
在service包中新建实现类UserService.java
package com.example.demo.service; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserInfo(int id) { return userMapper.getUserInfo(id); } public int deleteById(int id) { return userMapper.deleteById(id); } public int Update(User user) { return userMapper.update(user); } public User save(User user) { int save = userMapper.save(user); return user; } public List<User> selectAll() { return userMapper.selectAll(); } }
在controller包中新建访问类UserController.java
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/testBoot") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "getUser/{id}", method = RequestMethod.GET) public String GetUser(@PathVariable int id) { return userService.getUserInfo(id).toString(); } @RequestMapping(value = "/delete", method = RequestMethod.GET) public String delete(int id) { int result = userService.deleteById(id); if (result >= 1) { return "删除成功"; } else { return "删除失败"; } } @RequestMapping(value = "/update", method = RequestMethod.POST) public String update(User user) { int result = userService.Update(user); if (result >= 1) { return "修改成功"; } else { return "修改失败"; } } @RequestMapping(value = "/insert", method = RequestMethod.POST) public User insert(User user) { return userService.save(user); } @RequestMapping("/selectAll") @ResponseBody public List<User> ListUser() { return userService.selectAll(); } }