当前位置: 首页 > 编程笔记 >

springboot下使用mybatis的方法

伍弘盛
2023-03-14
本文向大家介绍springboot下使用mybatis的方法,包括了springboot下使用mybatis的方法的使用技巧和注意事项,需要的朋友参考一下

使用mybatis-spring-boot-starter即可。 简单来说就是mybatis看见spring boot这么火,于是搞出来mybatis-spring-boot-starter这个解决方案来与springboot更好的集成

详见

http://www.mybatis.org/spring/zh/index.html

引入mybatis-spring-boot-starter的pom文件

<dependency>  
  <groupId>org.mybatis.spring.boot</groupId>  
  <artifactId>mybatis-spring-boot-starter</artifactId>  
  <version>1.1.1</version>  
</dependency>

application.properties 添加相关配置

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = mysql

springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了。

mybatis.type-aliases-package=com.test.demo.model

这个配置用来指定bean在哪个包里,避免存在同名class时找不到bean

在启动类中添加@MapperScan指定dao或者mapper包的位置,可以用 {"",""}的形式指定多个包

@SpringBootApplication
@MapperScan("com.test.demo.dao")
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

或者直接在Mapper类上面添加注解@Mapper也可以指定mapper,建议使用上面这种,给每个mapper加个注解挺麻烦不说,如果是dao的包,还是要用@MapperScan来指定位置

接下来,可以用注解模式开发mapper,或者用xml模式开发

注解模式

@Mapper
public interface CityMapper {
  @Select("select * from city where state = #{state}")
  City findByState(@Param("state") String state);
}

@Select 是查询类的注解,所有的查询均使用这个 @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。 @Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值 @Update 负责修改,也可以直接传入对象 @delete 负责删除 了解更多注解参考这里

http://www.mybatis.org/mybatis-3/zh/java-api.html

xml模式

xml模式保持映射文件的老传统,application.properties需要新增

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

指定mybatis的映射xml文件位置 此外,还可以指定mybatis的配置文件,如果需要增加mybatis的一些基础配置,可以增加下面的配置

mybatis.config-locations=classpath:mybatis/mybatis-config.xml

指定mybatis基础配置文件

mybatis-config.xml可以添加一些mybatis基础的配置,例如

<configuration>
  <typeAliases>
    <typeAlias alias="Integer" type="java.lang.Integer" />
    <typeAlias alias="Long" type="java.lang.Long" />
    <typeAlias alias="HashMap" type="java.util.HashMap" />
    <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
    <typeAlias alias="ArrayList" type="java.util.ArrayList" />
    <typeAlias alias="LinkedList" type="java.util.LinkedList" />
  </typeAliases>
</configuration>

编写Dao层的代码

public interface CityDao {
  public City selectCityByState(String State);
}

对应的xml映射文件

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.demo.dao.CityDao">
  <select id="selectCityByState" parameterType="String" resultType="City">
    select * from city where state = #{state}
  </select></mapper>

总结

以上所述是小编给大家介绍的springboot下使用mybatis的方法,希望对大家有所帮助!

 类似资料:
  • 本文向大家介绍IDEA项目使用SpringBoot+MyBatis-Plus的方法,包括了IDEA项目使用SpringBoot+MyBatis-Plus的方法的使用技巧和注意事项,需要的朋友参考一下 步骤如下: 1.打开IDEA 2.File—>new—> project 3.选择spring initializr—>Next 4.填写Grouphe和Artifact,选择Java version

  • 本文向大家介绍深入理解SpringBoot中关于Mybatis使用方法,包括了深入理解SpringBoot中关于Mybatis使用方法的使用技巧和注意事项,需要的朋友参考一下 这两天启动了一个新项目因为项目组成员一直都使用的是mybatis,虽然个人比较喜欢jpa这种极简的模式,但是为了项目保持统一性技术选型还是定了 mybatis。到网上找了一下关于spring boot和mybatis组合的相

  • 本文向大家介绍SpringBoot下Mybatis的缓存的实现步骤,包括了SpringBoot下Mybatis的缓存的实现步骤的使用技巧和注意事项,需要的朋友参考一下 说起 mybatis,作为 Java 程序员应该是无人不知,它是常用的数据库访问框架。与 Spring 和 Struts 组成了 Java Web 开发的三剑客--- SSM。当然随着 Spring Boot 的发展,现在越来越多的

  • 本文向大家介绍springboot快速整合Mybatis组件的方法(推荐),包括了springboot快速整合Mybatis组件的方法(推荐)的使用技巧和注意事项,需要的朋友参考一下 Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化

  • 本文向大家介绍springboot+mybatis-plus实现内置的CRUD使用详解,包括了springboot+mybatis-plus实现内置的CRUD使用详解的使用技巧和注意事项,需要的朋友参考一下 springboot+mybatis-plus实现内置的CRUD使用详情,具体修改删除操作内容后文也有详细说明 mybatis-plus的特性 无侵入:只做增强不做改变,引入它不会对现有工程产

  • org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: The 'JSON_EXTRACT(all_info, '$' property of null is not a List or Array. 关键是这个sql 用navi