第一步:创建springboot空项目
第二步:pom.xml添加相应依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
第三步:application.properties添加配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=9091
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
第四部:编写接口
dao层 : 使用mybatis-plus
public interface UserMapperDao extends BaseMapper<User> {
}
DemoApplication 去扫描dao层
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
service层:
1、UserMapperService
public interface UserMapperService {
List<User> findAll();
}
2、UserMapperServiceImpl
@Service
public class UserMapperServiceImpl implements UserMapperService {
@Autowired
private UserMapperDao userMapper;
@Override
public List<User> findAll() {
return userMapper.selectList(null);
}
}
controller层:
@Controller
@RequestMapping("/mp")
public class UserMapperController {
@Autowired
private UserMapperService userMapperService;
/**
* 重定向到首页
*/
@GetMapping("/")
public ModelAndView webIndex(){
return new ModelAndView("redirect:/index.html");
}
@GetMapping("/userall")
public String userall(ModelMap modelMap){
List<User> user = userMapperService.findAll();
modelMap.addAttribute("user",user);
return "index";
}
}
第五步:thymeleaf模版引擎进行数据交互
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>mybatis-plus Demo</title>
</head>
<body style="text-align: center">
<main class="main">
<h1 th:if="1>2">没有数据时候的首页</h1>
<table th:each="c:${user}">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>email</th>
</tr>
<tr>
<td th:text="${c.id}">id</td>
<td th:text="${c.name}">name</td>
<td th:text="${c.age}">age</td>
<td th:text="${c.email}">email</td>
</tr>
</table>
</main>
</body>
</html>