Spring Boot整合Mybatis配置详解

摘要:
首先,你得有个SpringBoot项目。平时开发常用的repository包在mybatis里被替换成了mapper。配置:1.引入依赖:org.mybatis.spring.bootmybatis-spring-boot-starter1.1.12.编辑application.propertiesspring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMTspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.servlet.multipart.max-request-size=2050MBspring.servlet.multipart.max-file-size=2048MBspring.jpa.hibernate.use-new-id-generator-mappings=falseserver.port=8080server.servlet.context-path=/mybatisDemospring.jmx.enabled=falsemybatis.type-aliases-package=tgc.edu.wx.entitymybatis.mapperLocations=classpath:mapping/*.xml这里要注意mybatis的两个相关配置,一是扫描的包,二是映射文件的地址。PeopleService.javapackagetgc.edu.wx.service;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importtgc.edu.wx.entity.People;importtgc.edu.wx.mapper.PeopleMapper;@ServicepublicclassPeopleService{@AutowiredprivatePeopleMapperpeopleDAO;publicListfindAll(){returnpeopleDAO.findAll();}}PeopleController.javapackagetgc.edu.wx.web.controller;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.ModelMap;importorg.springframework.web.bind.annotation.RequestMapping;importtgc.edu.wx.entity.People;importtgc.edu.wx.service.PeopleService;@Controller@RequestMappingpublicclassPeopleController{@AutowiredprivatePeopleServicepeopleService;@RequestMappingpublicStringlist{Listpeoples=peopleService.findAll();map.put;return"peopleList";}}数据库:4.依照application.properties中编写的地址在对应目录下创建xml格式的mybatis映射文件。

首先,你得有个Spring Boot项目。

Spring Boot整合Mybatis配置详解第1张

平时开发常用的repository包在mybatis里被替换成了mapper。

配置:

1.引入依赖:

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

2.编辑 application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.servlet.multipart.max-request-size=2050MB
spring.servlet.multipart.max-file-size=2048MB

spring.jpa.hibernate.use-new-id-generator-mappings=falseserver.port=8080server.servlet.context-path=/mybatisDemo
spring.jmx.enabled=false
mybatis.type-aliases-package=tgc.edu.wx.entity
mybatis.mapperLocations=classpath:mapping/*.xml

这里要注意mybatis的两个相关配置,一是扫描的包,二是映射文件的地址。

3.建立构建web项目所需的类,以及在数据库建立实体类对应的表,完成后如下图:

Spring Boot整合Mybatis配置详解第2张

PeopleMapper.java

packagetgc.edu.wx.mapper;

importjava.util.List;

importorg.apache.ibatis.annotations.Mapper;

importtgc.edu.wx.entity.People;

@Mapper
public interfacePeopleMapper {

    public List<People>findAll();
}

此文件注解为@Mapper而不再是@Repository。

PeopleService.java

packagetgc.edu.wx.service;

importjava.util.List;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;

importtgc.edu.wx.entity.People;
importtgc.edu.wx.mapper.PeopleMapper;

@Service
public classPeopleService {

    @Autowired
    privatePeopleMapper peopleDAO;
    
    public List<People>findAll() {
     returnpeopleDAO.findAll();
    }
}

PeopleController.java

packagetgc.edu.wx.web.controller;

importjava.util.List;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.ModelMap;
importorg.springframework.web.bind.annotation.RequestMapping;

importtgc.edu.wx.entity.People;
importtgc.edu.wx.service.PeopleService;

@Controller
@RequestMapping("/people")
public classPeopleController {
    
    @Autowired
    privatePeopleService peopleService;
    
    
    @RequestMapping("/list")
    publicString list(ModelMap map) {
         List<People> peoples =peopleService.findAll();
        map.put("peoples", peoples);
        return "peopleList";
    }
}

数据库:

Spring Boot整合Mybatis配置详解第3张

4.依照 application.properties 中编写的地址在对应目录下创建xml格式的mybatis映射文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="tgc.edu.wx.mapper.PeopleMapper">

<select id="findAll"resultType="tgc.edu.wx.entity.People">SELECT * FROM people
</select>
</mapper>

其中<select>标签内的 id 对应的是dao层内的方法名,resultType对应的是查询返回结果集的类型,select内填写对应方法的语句即可。

5.最后,在启动类里加上注解用于给出需要扫描的mapper文件路径

packagetgc.edu.wx;

importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("tgc.edu.wx.mapper")
public classMybatisDemoApplication {

    public static voidmain(String[] args) {
        SpringApplication.run(MybatisDemoApplication.class, args);
    }

}

启动项目测试一下:

Spring Boot整合Mybatis配置详解第4张

以上。

Spring Boot整合Mybatis配置详解第5张

免责声明:文章转载自《Spring Boot整合Mybatis配置详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇获取Spring容器中Bean实例的工具类(Java泛型方法实现)namp命令详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信小程序创建自定义select组件(内含组件与父页面的交互流程)

 闲来无事将制作的select自定义组件制作贴出来,供大家借鉴,也可以为了以后的使用做个备注。 select是我自定义的组件,这个一定要记住,它是自定义组件! 以下书写select组件代码 其中的wxml如下 <view class="select_wrap"> <!-- 左边文字 --> <view class=...

SQL查询一个表的总记录数的方法

一、简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指定列 SQL>SELECT empmo, ename, mgr FROM emp; SQL>SELECT DISTINCT mgr FROM emp; 只显示结果不同的项 4. 查询指定行 S...

持久层框架:MyBatis 3.2(1)

MyBatis 的前身就是 iBatis 。是一个数据持久层(ORM)框架。 iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO),同时还提供一个利用这个框架开发的 JPetStore实例。  看到Mybati...

MyBatis中的mapper中的&amp;lt;sql/&amp;gt;标签介绍

mapper中的标签的详细定义如图: insert、update、delete、select这四个标签在mybatis操作与配置文件中以说到,这里就不说了。 1、sql标签: sql标签是mapper节点的标签。 sql标签的属性:  id属性  databaseId属性  lang属性sql标签的子标签:   include、trim、where、se...

ORACLE DBA命令

1 运行SQLPLUS工具sqlplus 2 以OS的默认身份连接/ as sysdba 3 显示当前用户名show user 4 直接进入SQLPLUS命令提示符sqlplus /nolog 5 在命令提示符以OS身份连接connect / as sysdba 6 以SYSTEM的身份连接connect system/xxxxxxx@服务名 7 显示当然...

mysql修改definer方法

-- 函数、存储过程 select definer from mysql.proc; update mysql.proc set definer='billing@%'; -- 定时事件 select DEFINER from mysql.EVENTS; update mysql.EVENTS set definer='billing@%'; --视图 s...