Spring Boot 实用MyBatis做数据库操作
前言:
本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架
spring-boot中使用mybatis持久层框架与原spring项目使用方式和注解都不相同,需要依赖mybatis-spring-boot包
1、引入mybatis和数据库及其他项目依赖
1.1、引入mybatis依赖
- <!-- mybatis-spring-boot --> <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency>
1.2、引入mysql 驱动
- <!-- mysql--> <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>
1.3、项目pom.xml一览
- <project= =
- => <modelVersion></modelVersion> <groupId></groupId> <artifactId></artifactId> <packaging></packaging> <version></version> <name></name> <parent> <groupId></groupId> <artifactId></artifactId> <version></version> </parent> <dependencies>
- <dependency> <groupId></groupId> <artifactId></artifactId> <exclusions><exclusion><groupId></groupId> <artifactId></artifactId></exclusion></exclusions>> </dependency>
- <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>
- <dependency> <groupId></groupId> <artifactId></artifactId> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>
- <dependency> <groupId></groupId> <artifactId></artifactId> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> <scope></scope> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> <scope></scope> </dependency>
- <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency>
- <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>
- <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接口
- package
import
import
importimport
//使用Mapper注解声明该接口为mybatis的dao接口
@Mapper
publicinterfacepublic
public(value = ) String org_parent_coding);
- public(value=) String dep_id);
- }
3.2、dao接口对应的mapper.xml
mapper.xml与原mybatis写法相同
- <!DOCTYPE mapper
- >
<mapper=>
<resultMap= =>
<id= =/>
<result= =/>
<result= =/>
<result= =/>
</resultMap>
<select= =></select>
<select= = =>
=#{parentCoding,=} - </select>
<select= = =>
=#{dep_id} - </select>
</mapper>
补充:
做完以上步骤,就可以在service中直接通过spring的IOC注解注入mybatis的dao实现,我这里的dao接口是GetInfoDao,所以是注入‘getInfoDao’就可以正确引用该持久层;
注意:必须在spring-boot的入口类中开启@ComponentScan注解才能扫描到项目中所有注解
- package
import
import
importimport
@SpringBootApplication
//开启通用注解扫描
@ComponentScan
publicclassextends* 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行
- */
- protected
this
returnsuperpublicstaticvoid
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容器
- <!-- spring-boot web -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <!-- 排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目-->
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
2、spring-boot入口实现SpringBootServletInitializer接口
补充:SpringBootServletInitializer接口依赖javax.servlet包,需要在pom.xml中引入该包
spring-boot入口类必须实现SpringBootServletInitializer接口的configure方法才能让外部容器运行spring-boot项目
注意:SpringBootServletInitializer接口需要依赖 javax.servlet
- package
import
import
import
import
importimport
import
import@SpringBootApplication
// 开启通用注解扫描
@ComponentScan
publicclassextends* 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行
- */
- protected
this
returnsuperpublicstaticvoid
class}
Spring Boot 实用MyBatis做数据库操作的更多相关文章
- Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...
- spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化
SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的) 1在pom.xml引入mysql, spring-data-jpa依赖 2.在src/ ...
- spring boot web 开发及数据库操作
推荐网站http://springboot.fun/ 1.json 接口开发 2.自定义 filter 3.自定义 property 4.log 配置 5.数据库操作 6.测试
- spring boot整合mybatis查询数据库返回Map字段为空不返回解决
1.出现问题原因原因1:mybatis的配置即mapper返回映射配置. 原因2:jackson的配置即@ResponseBody序列化配置. 2.解决方式步骤1:解决原因1 mybatis: con ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- Spring Boot整合Mybatis完成级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...
- 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 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
随机推荐
- Windows下php,mysql,apache相关安装与配置,完善中…
PHP 的安装 由于php是一个zip文件(非install版),安装较为简单解压就行.把解压的 php5.2.1-Win32重命名为 php5.并复制到安装盘目录下.例如安装路径为 c:\php5 ...
- 戴尔R720xd服务器系统安装前期环境实现
型号:R720xd 开启服务器,Ctrl+R进入raid配置 配置完raid后F2对硬盘进行格式化 保存并重启 F11进入BIOS选项设置U盘启动 选择U盘启动 开始进行系统安装!
- SQL Server 2008 R2 企业版安装教程
1 安装包解压 2 解压后,打开setup.exe文件,选择安装,显示如图: 3 选择全新安装或向现有安装添加功能 4 点确定 5 输入 企业版序列号:R88PF-GMCFT-KM2KR-4R7GB- ...
- shell作业后台执行的方法
来思考几种场景: 1.某个脚本需要执行时间比较长,无人值守,可能执行过程中因ssh会话超时而中断? 2.某次测试一段代码,需要临时放入后台运行? 3.放入后台运行的脚本,需要在一段时间后重新调到前台? ...
- Grunt、gulp、webpack、不要听着高大上你就上,试试Codekit?
下载地址:https://incident57.com/codekit/ 官方网站了解更多 要编译Less.Sass.Stylus, CoffeeScript, Typescript, Jade, H ...
- APScheduler API -- apscheduler.triggers.cron
apscheduler.triggers.cron API Trigger alias for add_job(): cron class apscheduler.triggers.cron.Cron ...
- angular select ng-change实时获取value
<select ng-model="vm.selectVal" ng-options="o.id as o.title for o in vm.optionsDat ...
- shell系统检测->
系统状态检测脚本练习 1-> 查看磁盘状态 思路:查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件 1.获取磁盘当前使用的值 df -h|grep /$ 2.从获取到的值中提取出,对应的 ...
- mac下---charles抓包https
网上找的很多安装包都有问题,终于找到个可用的! 下载地址: http://pan.baidu.com/s/1pLAONbX ———————————————————————————— 教程转载:htt ...
- Maven 基础知识
Maven MavenMaven 简介 Maven MavenMaven 是 Apache Apache Apache 软件基金会组织维护的 软件基金会组织维护的 软件基金会组织维护的 软件基金会组织 ...
