springboot整合mybatis

本文详细介绍了如何在SpringBoot项目中整合MyBatis,包括引入相关依赖、使用@Options注解设置查询选项,以及在Mapper接口和XML配置文件中使用resultMap。同时提到了在全局配置中设置mapUnderscoreToCamelCase属性,以便自动转换字段命名规则。最后,还展示了配置文件的编写和测试接口的创建。
摘要由CSDN通过智能技术生成

导入mybatis依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-Java</artifactId>
            <version>5.1.3</version>
        </dependency>

 mybatis-spring-boot-starter引入了

mybatis

mybatis-spring

mybatis-spring-boot-autoconfiguration

spring-boot-starter-jdbc

步骤:
1 )、配置数据源相关属性
2 )、给数据库建表
3 )、创建 JavaBean
注解版
//指定这是一个操作数据库的mapper
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")//使
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}

MyBatis @Options使用方法

配合查询语句使用,主要是开关一些查询的选项。比如useCache = true表示将会缓存本次查询结果,以提高下次查询速度;flushCache = Options.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值