前言:

本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架

spring-boot中使用mybatis持久层框架与原spring项目使用方式和注解都不相同,需要依赖mybatis-spring-boot包

1、引入mybatis和数据库及其他项目依赖

1.1、引入mybatis依赖

[html] view plain copy

 

  1. <!-- mybatis-spring-boot --> <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency>

1.2、引入mysql 驱动

[html] view plain copy

 

  1. <!-- mysql--> <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>

1.3、项目pom.xml一览

[html] view plain copy

 

  1. <project= =
  2. => <modelVersion></modelVersion> <groupId></groupId> <artifactId></artifactId> <packaging></packaging> <version></version> <name></name> <parent> <groupId></groupId> <artifactId></artifactId> <version></version> </parent> <dependencies>
  3. <dependency> <groupId></groupId> <artifactId></artifactId> <exclusions><exclusion><groupId></groupId> <artifactId></artifactId></exclusion></exclusions>> </dependency>
  4. <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>
  5. <dependency> <groupId></groupId> <artifactId></artifactId> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>
  6. <dependency> <groupId></groupId> <artifactId></artifactId> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> <scope></scope> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> <scope></scope> </dependency>
  7. <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency>
  8. <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency> </dependencies> <profiles> <profile> <id></id> <dependencies> > <dependency> <groupId></groupId> <artifactId></artifactId> <type></type> </dependency> </dependencies> </profile> </profiles>
  9. <build> <plugins> <plugin> <groupId></groupId> <artifactId></artifactId> </plugin> </plugins> </build> <repositories> <repository> <id></id> <url></url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id></id> <url></url> </pluginRepository> </pluginRepositories> </project>

2、配置数据库连接参数、设置mybatis的mappers所在包以及spring-boot服务参数配置

在项目根目录下创建一个application.properties,该文件用于定义spring-boot的相关参数及数据库参数,以及配置mybatis的mappers扫描路径

如果是maven项目,application.properties放在src/main/resource/目录下

配置如下:

spring.datasource.url=jdbc:MySQL://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=eguid
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=5
spring.datasource.max-wait=10000
spring.datasource.min-idle=1
spring.datasource.initial-size=3

server.port=8088
server.session.timeout=10
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8

mybatis.mapperLocations=classpath:cn/eguid/carSysWEB/mappers/*.xml

3、mybatis的dao接口及mapper.xml实现

3.1、定义mybatis的dao接口

该接口与mybatis-spring方式不同,需要加上一个@Mapper注解

@Mapper注解用于声明该接口为mybatis的dao接口

[java] view
plain
 copy

 

  1. package

    import

    import
    import

    import
    //使用Mapper注解声明该接口为mybatis的dao接口
    @Mapper
    publicinterface

    public

    public(value = ) String org_parent_coding);

  2. public(value=) String dep_id);
  3. }

3.2、dao接口对应的mapper.xml

mapper.xml与原mybatis写法相同

[html] view
plain
 copy

 

  1. <!DOCTYPE mapper
  2. >

    <mapper=>
    <resultMap= =>
    <id= =/>
    <result= =/>
    <result= =/>
    <result= =/>
    </resultMap>
    <select= =>

    </select>
    <select= = =>
    =#{parentCoding,=}

  3. </select>
    <select= = =>
    =#{dep_id}
  4. </select>
    </mapper>

补充:

做完以上步骤,就可以在service中直接通过spring的IOC注解注入mybatis的dao实现,我这里的dao接口是GetInfoDao,所以是注入‘getInfoDao’就可以正确引用该持久层;

注意:必须在spring-boot的入口类中开启@ComponentScan注解才能扫描到项目中所有注解

[java] view
plain
 copy

 

  1. package

    import
    import
    import

    import

    @SpringBootApplication
    //开启通用注解扫描
    @ComponentScan
    publicclassextends

    * 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行

  2. */
  3. protected
    this
    returnsuper

    publicstaticvoid
    class

    }

5、总结:

1、spring-boot项目中使用mabatis需要依赖mybatis-spring-boot

2、需要在application.xml中定义数据库连接参数以及mybatis的mappers文件扫描路径

3、mybatis的dao接口需要加上@Mapper注解才能被spring-boot正确扫描到

4、spring-boot开启注解扫描的注解是@ComponentScan

6.让外部Tomcat运行Spring Boot项目

只需要在原项目上做两件事

1、在pom.xml中排除org.springframework.boot的内置tomcat容器

  1. <!-- spring-boot web -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. <!-- 排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目-->
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-tomcat</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>

2、spring-boot入口实现SpringBootServletInitializer接口

补充:SpringBootServletInitializer接口依赖javax.servlet包,需要在pom.xml中引入该包


spring-boot入口类必须实现SpringBootServletInitializer接口的configure方法才能让外部容器运行spring-boot项目

注意:SpringBootServletInitializer接口需要依赖 javax.servlet

[java] view
plain
 copy

 

  1. package

    import
    import
    import
    import
    import

    import
    import
    import

    @SpringBootApplication
    // 开启通用注解扫描
    @ComponentScan
    publicclassextends

    * 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行

  2. */
  3. protected
    this
    returnsuper

    publicstaticvoid
    class

    }

Spring Boot 实用MyBatis做数据库操作的更多相关文章

  1. Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作

    开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...

  2. spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化

    SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的) 1在pom.xml引入mysql, spring-data-jpa依赖 2.在src/ ...

  3. spring boot web 开发及数据库操作

    推荐网站http://springboot.fun/ 1.json 接口开发 2.自定义 filter 3.自定义 property 4.log 配置 5.数据库操作 6.测试

  4. spring boot整合mybatis查询数据库返回Map字段为空不返回解决

    1.出现问题原因原因1:mybatis的配置即mapper返回映射配置. 原因2:jackson的配置即@ResponseBody序列化配置. 2.解决方式步骤1:解决原因1 mybatis: con ...

  5. Spring Boot整合Mybatis并完成CRUD操作

    MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...

  6. Spring Boot整合Mybatis完成级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  7. Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...

  8. Spring boot教程mybatis访问MySQL的尝试

    Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...

  9. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

随机推荐

  1. [ONTAK2015]Bajtman i Okrągły Robin

    bzoj 4276: [ONTAK2015]Bajtman i Okrągły Robin Time Limit: 40 Sec  Memory Limit: 256 MB Description 有 ...

  2. zookeeper系列之:zookeeper简介浅谈

    一.zookeeper的定义 打开zookeeper官网,赫然一行大字,写着:“Apache ZooKeeper致力于开发和维护实现高度可靠的分布式协调的开源服务器”.什么意思呢?就是Apache Z ...

  3. codeforces9D How many trees?

    传送门:http://codeforces.com/problemset/problem/9/D [题解] 树形dp,f(i,j)表示i个节点,高度为j的方案数,枚举左子树大小和哪一个子树高度为j-1 ...

  4. 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序

    [题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...

  5. 20155321 2016-2017-2 《Java程序设计》第七周学习总结

    20155321 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 Date/DateFormat Date是日期类,可以精确到毫秒. 构造方法 Date() ...

  6. hadoop启动步骤

    一.ssh的启动 ssh localhost二.hadoop的HDFS的格式化 bin/hadoop namenode -format三.hadoop的start-all.sh的启动 bin/tart ...

  7. 训练赛第一场D题

    解题报告:一开始不知道ATA的意思,后来才知道原来是转置矩阵乘以原来的矩阵.这题说起来比较麻烦就不说了,直接上代码: #include<cstdio> #include<cstrin ...

  8. HDU 4690 EBCDIC 2013 Multi-University Training Contest 9

    解题报告:一个模拟题,有两张表格,然后输入一个字符在第一章表格中的位置,让你找出这个字符在第二章表对应的位置. 我欧诺个的是暴力打表,输了两百多个数字,时间复杂度直接降到O(1),这题觉得比较坑的就是 ...

  9. vue组件之间传值方式解析

    vue组件之间传值方式解析一.父组件传到子组件 1.父组件parent代码如下: <template> <div class="parent"> <h ...

  10. Spring4笔记5--基于注解的DI(依赖注入)

    基于注解的DI(依赖注入): 对于 DI 使用注解,将不再需要在 Spring 配置文件中声明 Bean 实例.只需要在 Spring 配置文件中配置组件扫描器,用于在指定的基本包中扫描注解. < ...