适合初学者看的 SpringBoot整合Mybatis

  • 注:配置文件以及注解的路径问题要根据自己的项目结构灵活更改哦。

1. 新建支持数据库操作的SpringBoot项目

只要在选择依赖的时候把下面这几个选上就OK了!
在这里插入图片描述

2. 构建基本的项目环境

  • 建库建表
CREATE DATABASE `mybatis`;
USE mybatis;
CREATE TABLE `dept` (
  `id` int,
  `name` varchar(45),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 插入数据
insert into dept values("1", "设计部"),("2","生产部");
  • 在java包下创建
    • bean包(存实体类),建Department实体类。
    • controller包(控制层),建DeptController控制类。
    • mapper包(Dao层),建DepartmentMapper接口。
    • service包(服务层),建DepartmentService接口,并创建对应实现类DepartmentServiceImpl。
    • 在resource包下创建mapper包(Dao层配置文件),并创建Mybatis配置文件DepartmentMapper.xml 。
    • 在templates包下创建结果显示页面 show.html

main包结构图
最后效果

3. 在application.properties中配置数据库的连接信息

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=(ZrF666)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

4. 在配置文件中配置Mybatis

mybatis.mapper-locations=classpath:mapper/*.xml

5. 写测试代码

  1. Department
public class Department {
    
    
    int id;
    String name;

    public Department() {
    
    
    }

    public Department(int id, String name) {
    
    
        this.id = id;
        this.name = name;
    }

    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;
    }

    @Override
    public String toString() {
    
    
        return "[" +
                "id=" + id +
                ", name='" + name + '\'' +
                ']';
    }
}
  1. DepartmentController
@Controller
public class DeptController {
    
    
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/show")
    @ResponseBody
    public List<Department> showAllDept(){
    
    
        return departmentService.getAllDept();
    }
}
  1. DepartmentMapper
public interface DepartmentMapper {
    
    
    List<Department> getAllDept();
}
  1. DepartmentService
public interface DepartmentService {
    
    
    List<Department> getAllDept();
}
  1. DepartmentServiceImpl
@Service()
public class DepartmentServiceImpl implements DepartmentService {
    
    
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDept() {
    
    
        return departmentMapper.getAllDept();
    }
}
  1. DepartmentMapper.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.itzhang.mapper.DepartmentMapper">
    <select id="getAllDept" resultType="com.itzhang.bean.Department">
        select id, name from dept;
    </select>
</mapper>
  1. show.html
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>SpringBoot-Mybatis|Department</title>
</head>
<body>
</body>
</html>
  1. 在启动类里加注解 @Mapperscan() 值为mapper的项目路径
@SpringBootApplication
@MapperScan("com.itzhang.mapper")
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class, args);
    }

}

5. 测试效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44653914/article/details/107482990
今日推荐