一、SpringBoot整合MyBatis
在这里声明简单操作一下,跟spring整合mybatis也有相似度。基本试过一次就能了解。
示例
创建springboot项目,在创建的时候记得加上web及mysql驱动
导入依赖
<!-- mybatis-spring-boot-starter 整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
去配置文件连接数据库
老样子数据库在8以上记得加时区
# 连接数据库
spring:
datasource:
username: root
password: 123
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
然后就去写个实体类
public class User {
private Integer id;
private String name;
private String pwd;
public User() {
}
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
在来个mapper接口
这里记得在接口头上加上@Mapper表名这是个Mapper接口,没有mapper,controller就识别不到
@Repository扫描dao
import com.java.ym.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List<User> queryUser();
User queryUserById(int id);
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
}
写完了mapper就去映射写SQL.在resources下建mybatis包,再往下建mapper包,咱就把xml放在这儿。

配完立马去springboot配置里配置路径。就在配置数据库连接的下面编写即可
第一个是配置实体类的别名
第二个是路径.在mybatis前面不要加“/”不然找不到,classpath相于resources下去找mybatis/mapper/*.xml所有的xml
# 连接数据库
spring:
datasource:
username: root
password: 123
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
# 整合mybatis
mybatis:
type-aliases-package: com.java.ym.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
这里SQL我就写一个,简单了解就行
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.java.ym.mapper.UserMapper">
<select id="queryUser" resultType="User">
select * from user
</select>
</mapper>
咱就不建service了,直接去controller
import com.java.ym.mapper.UserMapper;
import com.java.ym.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("/userList")
public List<User> queryUser(){
List<User> userList = userMapper.queryUser();
return userList;
}
}
http://localhost:8080/userList
运行结果:
[{“id”:3,“name”:“漂移崽”,“pwd”:“123”},{“id”:4,“name”:“螺旋崽”,“pwd”:“123”},{“id”:5,“name”:“123”,“pwd”:“123”},{“id”:6,“name”:null,“pwd”:null},{“id”:7,“name”:“小鸟游六花”,“pwd”:“123”}
本文介绍如何在SpringBoot项目中整合MyBatis框架,包括添加依赖、配置数据库连接、编写实体类、创建Mapper接口及XML映射文件,并提供了一个简单的示例。

435

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



