项目结构
注意
springboot配置文件com.mysql.jdbc.Driver报红
去掉pom.xml里面的runtime
1.导入依赖
<!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot 的,而不是Spring Boot自己的-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2.配置数据库连接信息
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3,创建实体类
package org.westos.springboot.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
4.配置Mapper接口类
package org.westos.springboot.dao;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.westos.springboot.pojo.User;
import java.util.List;
@Mapper //表示本类是一个 MyBatis 的 Mapper,等价于以前 Spring 整合 MyBatis 时的 Mapper 接口
@Repository //spring组件
public interface UserDao {
//选择全部用户
List<User> selectUser();
//根据id选择用户
User selectUserById(int id);
//添加一个用户
int addUser(User user);
//修改一个用户
int updateUser(User user);
//根据id删除用户
int deleteUser(int id);
}
5.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="org.westos.springboot.dao.UserDao">
<!--选择全部用户-->
<select id="selectUser" resultType="User">
select * from mybatis.user
</select>
<!--根据id选择用户-->
<select id="selectUserById" resultType="User">
select * from mybatis.user where id=#{id}
</select>
<!--添加一个用户-->
<update id="addUser" parameterType="User">
insert into user (id,name,pwd)
values (#{id},#{name},#{pwd})
</update>
<!--修改一个用户-->
<update id="updateUser" parameterType="User">
update user set name =#{name},id=#{id},pwd=#{pwd}
where id= #{id}
</update>
<!--根据id删除用户-->
<delete id="deleteUser" parameterType="User">
delete from user where id = #{id}
</delete>
</mapper>
SpringBoot整合
#指定myBatis的核心配置文件与Mapper映射文件
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
# 注意:对应实体类的路径
mybatis.type-aliases-package=org.westos.springboot.pojo
7.编写controller
package org.westos.springboot.controller;
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.RestController;
import org.westos.springboot.dao.UserDao;
import org.westos.springboot.pojo.User;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
//选择全部用户
@GetMapping("/selectUser")
public String selectUser(){
List<User> users = userDao.selectUser();
for (User user : users) {
System.out.println(user);
}
return "查询所有用户了";
}
//根据id选择用户
@GetMapping("/selectUserById")
public String selectUserById(){
User user = userDao.selectUserById(1);
System.out.println(user);
return "用id查询了";
}
//添加一个用户
@GetMapping("/addUser")
public String addUser(){
userDao.addUser(new User(5,"阿三","456789"));
return "增加了";
}
//修改一个用户
@GetMapping("/updateUser")
public String updateUser(){
userDao.updateUser(new User(5,"阿三","421319"));
return "修改了";
}
//根据id删除用户
@GetMapping("/deleteUser")
public String deleteUser(){
userDao.deleteUser(5);
return "删除了";
}
}