前言:

本项目基于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. Shell记录-Shell脚本基础(六)

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行. 1.命令格式 watch[参数][命令] ...

  2. 在升级Windows 8.1后,桌面的右下角显示"SecureBoot未正确配置"

    原地址为:http://ask.zol.com.cn/q/201881.html 第一种模式BIOS: 在将Secure Boot设置为Enabled后,Secure Boot Status依然为关闭 ...

  3. bzoj千题计划139:bzoj2229: [Zjoi2011]最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=2229 最小割树介绍:http://blog.csdn.net/jyxjyx27/article/de ...

  4. 男女通用的减肥计划 10分钟家庭hiit训练

    在大城市的年轻人,一般都会比较忙,晚上下班吃完饭,到家就要8-9点了,再让他们去,有时候真的不太方便. 其实你如果想要,也不一定要,在家里做hiit运动,就可以了. hiit(高强度间歇运动),是目前 ...

  5. 视差滚动(Parallax Scrolling)的一点小心得

    下面内容来源于我知乎的这个答案:http://www.zhihu.com/question/20990029/answer/21436067 假期有空,整理到博客园这边,并做了一些语境的调整. ——— ...

  6. fifo 上使用 select -- 转

    http://www.outflux.net/blog/archives/2008/03/09/using-select-on-a-fifo/ The right way to handle on-g ...

  7. Tomcat与Spring中的事件机制详解

    最近在看tomcat源码,源码中出现了大量事件消息,可以说整个tomcat的启动流程都可以通过事件派发机制串起来,研究透了tomcat的各种事件消息,基本上对tomcat的启动流程也就有了一个整体的认 ...

  8. phpcm nginx 伪静态文件

    rewrite ^/show-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid ...

  9. CF264B Good Sequences

    传送门 Description: 松鼠丽丝特别喜欢n个她称之为“好整数”的整数:a1,a2,……,an.(会输入) 现在,她对“好序列”很感兴趣.如果一个序列x1,x2,...,xk能够满足一下三个条 ...

  10. ECMAScript 6中的var,let,const

    var的变量提升 console.log(a); //输出undefined ; 他的实际执行顺序是: var a: console.log(a); a= 这就是var的变量提升 const命令的用法 ...