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

相关文章

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Linux卸载自带jdk并安装新jdk版本的图文教程

《Linux卸载自带jdk并安装新jdk版本的图文教程》在Linux系统中,有时需要卸载预装的OpenJDK并安装特定版本的JDK,例如JDK1.8,所以本文给大家详细介绍了Linux卸载自带jdk并... 目录Ⅰ、卸载自带jdkⅡ、安装新版jdkⅠ、卸载自带jdk1、输入命令查看旧jdkrpm -qa

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3