资料
官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
自己去Maven找最新的。
整合
1、导入 MyBatis 所需要的依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
2、配置数据库连接信息(可以加入Druid)
spring.datasource.username=root
spring.datasource.password=pwx12345
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.pwx.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
3、测试数据库是否连接成功!
4、创建实体类,导入 Lombok!
package com.pwx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ Auction:pwx
* @ Date:2021/1/25 - 01 - 25 - 18:52
* @ Description:com.pwx.pojo
* @ version:1.0
*/
@AllArgsConstructor
@Data
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
5、创建mapper目录以及对应的 Mapper 接口
package com.pwx.mapper;
import com.pwx.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @ Auction:pwx
* @ Date:2021/1/26 - 01 - 26 - 22:28
* @ Description:com.pwx.mapper
* @ version:1.0
*/
@Mapper
@Repository //@Repository、@Service、@Controller 和 @Component 将类标识为Bean
public interface UserMapper {
List<User> queryList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
6、对应的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="com.pwx.mapper.UserMapper">
<insert id="addUser" parameterType="User">
insert into user(id,name,pwd) values (#{id},#{name},#{pwx})
</insert>
<update id="updateUser" parameterType="User">
update user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
<select id="queryList" resultType="User">
select * from user
</select>
<select id="selectById" resultType="User">
select * from user where id=#{id}
</select>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
7、maven配置资源过滤问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
8、编写Controller 进行测试!
package com.pwx.controller;
import com.pwx.mapper.UserMapper;
import com.pwx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @ Auction:pwx
* @ Date:2021/1/27 - 01 - 27 - 11:00
* @ Description:com.pwx.controller
* @ version:1.0
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryList(){
List<User> userList = userMapper.queryList();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
@GetMapping("/addUser")
public String addUser(User user){
userMapper.addUser(new User(5,"小明","12345"));
return "OK";
}
@GetMapping("/updateUser")
public String updateUser(User user){
userMapper.updateUser(new User(5,"小红","pwx12345"));
return "OK";
}
@GetMapping("/deleteUser")
public String deleteUser(int id){
userMapper.deleteUser(id);
return "OK";
}
}
启动项目访问进行测试,完美!
最后
以上所有是通过在哔哩哔哩中遇见狂神说,狂神老师学习的,在这里表示热衷的感谢,有不懂的大家也可以去看看他的视频。
大家也可以关注一下微信,我个人觉得讲的很好。