SprintBoot整合MyBatis简易教程

2024-04-25 23:48

本文主要是介绍SprintBoot整合MyBatis简易教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SprintBoot整合MyBatis简易教程

MyBatis导入及配置

  • 使用Maven管理
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version>
</dependency>
  • 需要在启动主类添加@MapperScan 使得Mapper文件被扫描到
    即添加下面这行代码到启动主类中,com.imoor.dataobject.mapper为mapper所在包
    @MapperScan(basePackages = "com.imooc.dataobject.mapper")
@SpringBootApplication
@MapperScan(basePackages = "com.imooc.dataobject.mapper")
public class SellApplication {public static void main(String[] args) {SpringApplication.run(SellApplication.class, args);}
}
  • 在控制台打印出MyBatis的sql语句
    application.yml配置文件上配置
logging:level:com.imooc.dataobject.mapper:trace

将mapper包下的所有类的日志等级设置为最低的trace等级

MyBatis基本操作

package com.imooc.dataobject.mapper;import com.imooc.dataobject.ProductCategory;
import org.apache.ibatis.annotations.*;import java.util.List;
import java.util.Map;//采用MyBatis操作数据库
public interface ProductCategoryMapper {//使用Map插入数据//注意!!! #{categoryName,jdbcType=VARCHAR} map中的键值需要与categoryName保持一致//即map.put("categoryName","testName")//jdbcType=VARCHAR是规定写法,jdbcType后面的类型一定要全部大写!!!(只有传入Map对象作为参数,jdbcType才是必写的)@Insert("insert into product_category(category_name,category_type) values (#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")int insertByMap(Map<String,Object> map);//使用对象插入数据//注意!!!  #{categoryName,jdbcType=VARCHAR}    categoryName一定要与ProductCategory类中的属性名称一致//且ProductCategory中属性的get和set方法一定要有@Insert("insert into product_category(category_name,category_type) values (#{categoryName},#{categoryType})")int insertByObject(ProductCategory productCategory);//查询结果只有单条//注意!!! 查询需要返回结果,格式如下,column的值为数据库中的字段名,而property的值为对应类的属性名@Select("select * from product_category where category_type = #{categoryType}")@Results({@Result(column = "category_type",property = "categoryType"),@Result(column = "category_name",property = "categoryName"),@Result(column = "category_id",property = "categoryId"),})ProductCategory findByCategoryType(Integer categoryType);//查询结果有多个//注意!!! 查询需要返回结果,格式如下,column的值为数据库中的字段名,而property的值为对应类的属性名@Select("select * from product_category where category_name = #{categoryName}")@Results({@Result(column = "category_type",property = "categoryType"),@Result(column = "category_name",property = "categoryName"),@Result(column = "category_id",property = "categoryId"),})List<ProductCategory> findByCategoryName(String categoryName);//更新//注意!!! 传多个参数时需要使用@Param映射一下@Update("update product_category set category_name=#{categoryName} where category_type=#{categoryType}")int updateByCategoryType(@Param("categoryName") String categoryName,@Param("categoryType") Integer categoryType);//使用对象更新@Update("update product_category set category_name=#{categoryName} where category_type=#{categoryType}")int updateByObject(ProductCategory productCategory);//删除数据@Delete("delete from product_category where category_type = #{categoryType}")int deleteByCategoryType(Integer categoryType);
}

对应测试代码


package com.imooc.dataobject.mapper;import com.imooc.dataobject.ProductCategory;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.HashMap;
import java.util.List;
import java.util.Map;import static org.junit.Assert.*;@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryMapperTest {@Autowiredprivate ProductCategoryMapper mapper;@Testpublic void insertByMap() {Map<String, Object> map = new HashMap<>();map.put("category_name","fine");map.put("category_type",90);int result = mapper.insertByMap(map);Assert.assertNotEquals(0,result);}@Testpublic void insertByObject(){ProductCategory productCategory = new ProductCategory();productCategory.setCategoryId(30);productCategory.setCategoryName("testName");productCategory.setCategoryType(100);int result = mapper.insertByObject(productCategory);Assert.assertNotEquals(0,result);}@Testpublic void findByCategoryTypeTest(){ProductCategory productCategory = mapper.findByCategoryType(3);Assert.assertNotNull(productCategory);}@Testpublic void findByCategoryNameTest(){List<ProductCategory> resultList = mapper.findByCategoryName("food");Assert.assertNotEquals(1,resultList.size());}@Testpublic void updateByCategoryTypeTest(){int result = mapper.updateByCategoryType("helloTest", 11);Assert.assertEquals(1,result);}@Testpublic void updateByObjectTest(){ProductCategory productCategory = new ProductCategory();productCategory.setCategoryName("worldTest");productCategory.setCategoryType(11);int result = mapper.updateByObject(productCategory);Assert.assertEquals(1,result);}@Testpublic void deleteByCategoryTypeTest(){int result = mapper.deleteByCategoryType(11);Assert.assertNotEquals(0,result);}
}

这篇关于SprintBoot整合MyBatis简易教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/936134

相关文章

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI