初使用SpringBoot整合mybatis笔记(一)

本文档详细介绍了如何在SpringBoot项目中整合Mybatis,包括在pom.xml中添加Mybatis依赖,创建Department实体类,编写DepartmentMapper接口及XML映射文件,配置application.yml,以及创建DepartmentController进行数据操作。通过这些步骤,读者可以快速掌握SpringBoot集成Mybatis的基本流程。
摘要由CSDN通过智能技术生成

初使用SpringBoot整合mybatis笔记(一)

需要编写的内容有:

1.在pom.xml中导入mybatis的依赖包
- springboot官方的依赖以spring-boot-starter开头,mybatis的依赖包是mybatis根据springboot写的,所以是mybatis开头
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
2.编写实体对象类Department.class,注意匹配数据库表字段
- Lombok注解
/*部门表*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {

    private Integer id;
    private String departmentName;
    private Integer floor;

}
3.编写持久层接口Mapper文件departmentMapper.Interface
- 需要添加@Mapper和@Repository注解
@Mapper
@Repository
public interface DepartmentMapper {

    /*返回部门列表*/
    public List<Department> getDep();

    /*根据id查询部门*/
    public Department getDepById(Integer id);

    /*增加部门*/
    public void insertDep(Department department);

    /*更新部门*/
    public void updateDep(Department department);

    /*删除部门*/
    public void deleteDep(Integer id);
}
4.编写mapper的映射文件departmentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--注意此处的mapper设置,下列标签才是mapper开始-->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itguigu.webtest.mapper.DepartmentMapper">

    <!--此处的id需要和Mapper里的方法名一致 区分大小写;返回类型只需要写实体类名,不分大小写(相当于已经起了别名)-->
    <select id="getDep" resultType="department">
        select  * from department
    </select>

    <select id="getDepById" parameterType="int" resultType="department">
        select * from department where id=#{id}
    </select>

    <insert id="insertDep" parameterType="department">
        insert into department(departmentName,floor) values(#{departmentName},#{floor})
    </insert>

    <update id="updateDep" parameterType="department">
        update department set departmentName=#{departmentName},floor=#{floor} where id=#{id}
    </update>

    <delete id="deleteDep" parameterType="int">
        delete from department where id=#{id}
    </delete>

</mapper>
5.编写配置文件application.yml(文件名固定,后缀可以用.yml或者.properties)
#配置建立数据库连接参数
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/eesy_mybatis?serverTimezone=UTC&useUnicode=true&characterEncodeing=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver

#整合mybatis :
#  type-aliases-package:实体类所在的包路径
#  mapper-locations: mapper配置文件所在的路径,注意classpath:后面不加/表示resources路径下
mybatis:
  type-aliases-package: com.itguigu.webtest.pojo
  mapper-locations:  classpath:mybatis/mapper/*.xml

6.编写控制器文件DepartmentController.class
- @RestController:直接返回到浏览器(包含了@Controller和@ResponseBody)
- @Autowried 自动注入
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentMapper departmentMapper;

    @GetMapping("/getDep")
    public List<Department> getDep(){
        List<Department> depList = departmentMapper.getDep();
        for (Department dep : depList){
            System.out.println(dep);
        }
        return depList;

    }
 }
7.运行主程序类
- @SpringBootApplication注解表示这是一个主程序类

主程序类
运行成功

8.在浏览器输入http://localhost:8080/getDep

得到结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值