1、导入整合Mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
2、配置数据库连接池信息
spring.datasource.username=root
spring.datasource.password=19990802
spring.datasource.url=jdbc:mysql//localhost:3306/mybatis??useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3、测试数据库连接池是否连接成功
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
连接成功:
4、IDEA连接数据库,并创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
5、编写mapper接口,UserMapper
@Mapper//表示是mybatis的mapper接口
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(@Param("id") int id);
int addUser(User user);
int deleteUserById(@Param("id") int id);
int updateUser(User user);
}
6、在配置文件中补充整合mybatis的配置
spring.datasource.username=root
spring.datasource.password=19990802
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合mybatis的配置
mybatis.type-aliases-package=com.cheng.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
7、编写mapper对应的映射文件,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.cheng.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(name,pwd,id)
values (#{name},#{pwd},#{id})
</insert>
<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>
<update id="updateUser" parameterType="User">
update user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
</mapper>
8、编写UserCortroller测试(这里偷懒不写service层,直接调用dao层)
package com.cheng.controller;
import com.cheng.mapper.UserMapper;
import com.cheng.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryList")
public List<User> queryUserList(){
return userMapper.queryUserList();
}
@GetMapping("/queryUserById/{id}")
public User queryUserById(@PathVariable("id") int id){
return userMapper.queryUserById(id);
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(1,"万里","123456"));
return "ok";
}
@GetMapping("/deleteUserById/{id}")
public String deleteUserById(@PathVariable("id") int id){
userMapper.deleteUserById(id);
return "ok";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(3,"神明","123456"));
return "ok";
}
}
测试成功!