SpringBoot整合Mybatis
一、基础工作
1、创建项目springboot项目,勾选web+JDBC+MySQL驱动
2、导入mybatis-springboot-starter
依赖
<!-- mybatis-springboot-starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
这里应该注意版本对应
3、编写application.properties
配置文件
#数据库配置 根据自己数据库username和password修改
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合mybatis
#给pojo实体类起别名
mybatis.type-aliases-package=com.kuang.pojo
#接口的配置文件的位置 我这里接口配置文件是UserMapper.xml 如下图所示
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
二、springboot整合mybatis案例
上面基础工作做好,其实基础框架就已经搭建好了,直接开始进行案例的操作就可以了。
项目结构
1、编写pojo包下的实体类User
package com.kuang.pojo;
public class User {
private int id;
private String name;
public String pwd;
public int getId() {
return id;
}
public void setId(int 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;
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
2、编写接口UserMapper
import com.kuang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
/*加了这个注解 就表示了 这是一个Mybatis的mapper类
就相当于之前使用的spring整合mybatis接口 也可以使用@MapperScan("com.kuang.mapper")*/
@Mapper
/**@Component 也可以用这个 万能的*/
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
3、编写接口实现配置文件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="queryUserList" resultType="User">
select * from mybatis.user
</select>
<select id="queryUserById" resultType="User">
select *
from mybatis.user
where id=#{id};
</select>
<insert id="addUser" parameterType="User">
insert into mybatis.user(id,name,pwd)
values(#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="User">
update mybatis.user
set name=#{name},pwd=#{pwd}
where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete
from mybatis.user
where id=#{id};
</delete>
</mapper>
配置文件UserMapper.xml
的位置
4、编写contoller中UserController
import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.net.util.IPAddressUtil;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
for(User user:userList){
System.out.println(user);
}
return userList;
}
@GetMapping("/queryUserById")
public User queryUserById(){
User user = userMapper.queryUserById(1);
return user;
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(21,"阿毛","123456"));
return "添加成功";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(1,"秃驴","123465"));
return "更改成功";
}
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(1);
return "删除成功";
}
}
至此,springboot整合mybatis 并且进行增删改查操作就全部结束了。