<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--SpringBoot版本-->
<version>2.4.3</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<!--springboot web 环境-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<!-- mybatis数据库 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- thymeleaf模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.4</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties
#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=admin
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
前端thymeleaf页面编写
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
<form>
<table border="1" cellspacing="0" cellpadding="0" width="60%">
<tr>
<th>编号</th>
<th>名称</th>
</tr>
<tr th:each="school:${schools}">
<td th:text="${school.id}"></td>
<td th:text="${school.name}"></td>
</tr>
</table>
</form>
</body>
</html>
后端编写
controller层
@Controller
public class HelloController {
@Autowired
private ISchoolService schoolService;
@RequestMapping("/hello")
public String hello2(Model model) {
model.addAttribute("schools",schoolService.query());
return "/hello";
}
}
service层
@Service
public class SchoolServiceImpl implements ISchoolService {
@Autowired
private SchoolMapper schoolMapper;
@Override
public List<School> query() {
return schoolMapper.selectAll();
}
}
public interface ISchoolService {
List<School> query();
}
mapper层
@Repository
public interface SchoolMapper {
List<School> selectAll();
}
mapper.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="org.mybatis.mapper.SchoolMapper">
<select id="selectAll" resultType="org.mybatis.domain.School">
select * from school
</select>
</mapper>
实体类
@Getter
@Setter
public class School {
private Long id;
private String name;
}
启动类
@SpringBootApplication
@MapperScan(basePackages = "mapper包所在包名")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
按照以上编写,可以将MySQL中test库里的school表数据全部打印出来