- 新建数据库表
person
:
DROP TABLE IF EXISTS `user`;
CREATE TABLE user (
`id` int(15) AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`pwd` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY(id)
)
- 导入Mybatis和mysql的依赖:
也可以在新建项目的时候,导入相关的模块
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 注意Maven静态资源配置问题
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!--此处的配置是识别到mapper.xml文件,也可以在application.properties中配置-->
<!--即静态资源导出问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
- 修改yml配置文件:
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-mybatis?useUnicode=true&characterEncoding=utf-8
username: admin
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
- 先测试一下,看看有没有配置成功:
@SpringBootTest
class SpringbootStudy3ApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
- 编写Mapper接口:
注意:根据官方文档,mapper接口,一定要加上@Mapper
注解和@Repository
注解
@Repository
注解的功能和@Component
一样,只是@Repository
是用在Dao层的
@Mapper
@Repository //和@Component功能一样,只是用在Dao层
public interface UserMapper {
List<User> queryUserList();
User queryUserById(@Param("id") int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
- 编写xml实现
这里的xml实现,是放在 resources/mybatis/mapper 下的
想要和接口绑定,还需要在配置文件中配置
<?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="top.faroz.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" parameterType="_int" resultType="User">
select * from user where id=#{id}
</select>
<insert id="addUser" parameterType="User">
insert into user values(#{id},#{name},#{pwd})
</insert>
<update id="updateUser">
update user
set name=#{name},pwd=#{pwd}
where id=#{id}
</update>
<delete id="deleteUser" parameterType="_int">
delete from user where id=#{id}
</delete>
</mapper>
- 配置Mybatis
上面的yml配置其实没有配置完,只配置了一个数据源,其实,还要再配置一下Mybatis,
比如上面的xml和接口的绑定问题
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-mybatis?useUnicode=true&characterEncoding=utf-8
username: admin
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
# 整合Mybatis
mybatis:
type-aliases-package: top.faroz.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
- 编写Controller进行测试
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper mapper;
@GetMapping("/list")
public ResponseEntity queryUserList() {
List<User> list = mapper.queryUserList();
return ResponseEntity.ok(list);
}
@GetMapping("/list/{id}")
public ResponseEntity queryUserById(@PathVariable("id")int id) {
User user = mapper.queryUserById(id);
return ResponseEntity.ok(user);
}
@PostMapping("/add")
public String addUser(@RequestBody User user) {
mapper.addUser(user);
return "添加成功!";
}
@PostMapping("/update")
public String updateUser(@RequestBody User user) {
mapper.updateUser(user);
return "修改成功!";
}
@GetMapping("/delete/{id}")
public String deleteUser(@PathVariable("id") int id) {
mapper.deleteUser(id);
return "删除成功!";
}
}
测试成功