新建一个springboot项目:



@RestController
public class Hellospringboot {
@RequestMapping("/hello")
public String test() {
return "Hello SpringBoot";
}
}


springboot整合mybatis
导入需要的依赖(这些只是我们另外导入的部分依赖文件)
<!-- 这是自定义的包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--依赖-->
<!--在这里写的依赖都不需要版本号,只要在父依赖中有!-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
编写配置:在下面的两个文件中都可以进行配置编写

application.properties
# 在这里也可以编写一些SpringBoot的配置
# 更改 tomcat 端口号
server.port=9090
# 一会集成 MyBatis 也在这里配置
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
springboot默认的是hikari数据源
在测试类中测试是否连接

@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws SQLException {
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
创建一个pojo/User类
@Data
public class User {
private int id;
private String name;
private String pwd;
}
创建mapper或者叫dao层

UserMapper
@Mapper // @Mapper 标注这是一个Mapper接口
@Repository // 代表持久层
public interface UserMapper {
// @Select("select * from user")
List<User> getUserLists();
}
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.kuang.mapper.UserMapper">
<select id="getUserLists" resultType="User">
select * from user;
</select>
</mapper>
还需要配置application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.gg.pojo
mapper-locations: classpath:com/gg/mapper/*.xml
控制层

@RestController
public class MybatisController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/get")
public String getUserList(){
List<User> userLists = userMapper.getUserLists();
return String.valueOf(userLists);
}
}
运行测试
springboot-web
新建项目

在static资源目录下 可以直接访问(如果没有controller控制会直接访问,若有则会访问跳转页面)
在templates资源目录下 要想访问需要使用controller进行跳转(首先需要spring-boot-starter-thymeleaf依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
IndexController
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/user")
public String index1(){
return "user/index";
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>user首页</h1>
</body>
</html>
传值
先导入头文件:<html lang="en" xmlns:th="http://www.thymeleaf.org">
@Controller
public class IndexController {
@RequestMapping("/")
public String index(Model model){
model.addAttribute("msg","nihao");
model.addAttribute("users", Arrays.asList("guojian","guojian1","guojian2"));
return "index";
}
@RequestMapping("/user")
public String index1(){
return "user/index";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>templates首页</h1>
<p th:text="${msg}"></p>
<!-- 三种遍历的写法-->
<p th:each="user:${users}" th:text="${user}"/>
<h1 th:each="user:${users}">[[${user}]]</h1>
<div th:each="user:${users}">
<p th:text="${user}"/>
</div>
</body>
</html>
本文详细介绍了如何在SpringBoot项目中整合MyBatis,包括配置数据源、编写Mapper接口及XML映射文件,以及实现数据库操作的过程。
1733

被折叠的 条评论
为什么被折叠?



