- 首先看目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/64474706b9f7e35059fe4479a9997267.png)
- 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">
<mapper namespace="com.mini.program.program.dao.UserDao">
<resultMap id="findAllUser" type="com.mini.program.program.entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
<select id="getAllUser" resultType="User" resultMap="findAllUser">
SELECT * FROM myuser
</select>
</mapper>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456789
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.typeAliasesPackage=com.mini.program.program.entity
- 主程序的入口加上
@MapperScan("com.mini.program.program.dao")
- dao:
@Repository public interface UserDao { List<User> getAllUser(); }
- entity
@Data
public class User {
private int id;
private String username;
private String password;
}
@Service
public class UserServices {
@Autowired
UserDao userDao;
public List<User> getAllUser() {
return userDao.getAllUser();
}
}
@RestController
public class UserController {
@Autowired
UserServices userServices;
@ResponseBody
@RequestMapping("/getAllUser")
public List<User> findAllUser(){
if (userServices.getAllUser()!= null){
return userServices.getAllUser();
}else {
return null;
}
}
}
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mini.program</groupId>
<artifactId>program</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>program</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 数据库
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6d0d8cea1c9fa8a5cd797a055d1b0f3d.jpeg)
- 测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/beb4a7455705e4880128f79edb5cf8a3.png)
注意事项:我用的lombok,可能不能用,自行百度,我的是能用的,热部署Devtools