Spring Boot 实用MyBatis做数据库操作

前言:

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

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

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

1.1、引入mybatis依赖

[html]  view plain  copy
 print ?
  1.   
  2.         <dependency>  
  3.             <groupId>org.mybatis.spring.bootgroupId>  
  4.             <artifactId>mybatis-spring-boot-starterartifactId>  
  5.             <version>1.1.1version>  
  6.         dependency>  

1.2、引入mysql 驱动

[html]  view plain  copy
 print ?
  1.   
  2.         <dependency>  
  3.             <groupId>mysqlgroupId>  
  4.             <artifactId>mysql-connector-javaartifactId>  
  5.         dependency>  

1.3、项目pom.xml一览

[html]  view plain  copy
 print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   
  4.     <modelVersion>4.0.0modelVersion>  
  5.   
  6.     <groupId>cn.eguid.carDeviceInfoSysgroupId>  
  7.     <artifactId>carSys-webartifactId>  
  8.     <packaging>warpackaging>  
  9.     <version>1.4.0-SNAPSHOTversion>  
  10.     <name>carSys-webname>  
  11.   
  12.     <parent>  
  13.         <groupId>org.springframework.bootgroupId>  
  14.         <artifactId>spring-boot-starter-parentartifactId>  
  15.         <version>1.4.0.RELEASEversion>  
  16.     parent>  
  17.   
  18.     <dependencies>  
  19.           
  20.         <dependency>  
  21.             <groupId>org.springframework.bootgroupId>  
  22.             <artifactId>spring-boot-starter-webartifactId>  
  23.               
  24.         <dependency>  
  25.             <groupId>org.springframework.bootgroupId>  
  26.             <artifactId>spring-boot-starter-aopartifactId>  
  27.         dependency>  
  28.           
  29.         <dependency>  
  30.             <groupId>mysqlgroupId>  
  31.             <artifactId>mysql-connector-javaartifactId>  
  32.         dependency>  
  33.         <dependency>  
  34.             <groupId>org.springframework.bootgroupId>  
  35.             <artifactId>spring-boot-starter-jdbcartifactId>  
  36.         dependency>  
  37.           
  38.         <dependency>  
  39.             <groupId>redis.clientsgroupId>  
  40.             <artifactId>jedisartifactId>  
  41.         dependency>  
  42.         <dependency>  
  43.             <groupId>org.springframework.bootgroupId>  
  44.             <artifactId>spring-boot-starter-testartifactId>  
  45.             <scope>testscope>  
  46.         dependency>  
  47.         <dependency>  
  48.             <groupId>com.jayway.jsonpathgroupId>  
  49.             <artifactId>json-pathartifactId>  
  50.             <scope>testscope>  
  51.         dependency>  
  52.           
  53.         <dependency>  
  54.             <groupId>org.mybatis.spring.bootgroupId>  
  55.             <artifactId>mybatis-spring-boot-starterartifactId>  
  56.             <version>1.1.1version>  
  57.         dependency>  
  58.   
  59.           
  60.         <dependency>  
  61.             <groupId>com.alibabagroupId>  
  62.             <artifactId>fastjsonartifactId>  
  63.             <version>1.2.17version>  
  64.         dependency>  
  65.           
  66.         <dependency>  
  67.             <groupId>dom4jgroupId>  
  68.             <artifactId>dom4jartifactId>  
  69.             <version>1.6.1version>  
  70.         dependency>  
  71.   
  72.     dependencies>  
  73.   
  74.     <profiles>  
  75.         <profile>  
  76.             <id>productionid>  
  77.             <dependencies>  
  78.                   
  79.     <build>  
  80.         <plugins>  
  81.             <plugin>  
  82.                 <groupId>org.springframework.bootgroupId>  
  83.                 <artifactId>spring-boot-maven-pluginartifactId>  
  84.             plugin>  
  85.         plugins>  
  86.     build>  
  87.     <repositories>  
  88.         <repository>  
  89.             <id>spring-releasesid>  
  90.             <url>https://repo.spring.io/libs-releaseurl>  
  91.         repository>  
  92.     repositories>  
  93.     <pluginRepositories>  
  94.         <pluginRepository>  
  95.             <id>spring-releasesid>  
  96.             <url>https://repo.spring.io/libs-releaseurl>  
  97.         pluginRepository>  
  98.     pluginRepositories>  
  99. 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
 print ?
  1. package cn.eguid.carSysWeb.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.annotations.Mapper;  
  6. import org.apache.ibatis.annotations.Param;  
  7.   
  8. import cn.eguid.carSysWeb.entity.DepInfo;  
  9. //使用Mapper注解声明该接口为mybatis的dao接口  
  10. @Mapper  
  11. public interface GetInfoDao {  
  12.   
  13.     public List getRootInfo();  
  14.   
  15.     public List getDepInfo(@Param(value = "parentCoding") String org_parent_coding);  
  16.       
  17.     public List getDepInfoById(@Param(value="dep_id") String dep_id);  
  18. }  


3.2、dao接口对应的mapper.xml

mapper.xml与原mybatis写法相同

[html]  view plain  copy
 print ?
  1.     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  2.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3.   
  4. <mapper namespace="cn.eguid.carSysWeb.dao.GetInfoDao">  
  5.  <resultMap type="cn.eguid.carSysWeb.entity.DepInfo" id="depMap">  
  6.         <id column="org_coding" property="dep_id"/>  
  7.         <result column="org_name" property="dep_name"/>  
  8.         <result column="org_parent_coding" property="dep_parent_coding"/>  
  9.         <result column="last_time" property="last_time"/>  
  10.     resultMap>  
  11. <select id="getRootInfo" resultMap="depMap">  
  12. select * from car_organization where org_parent_coding is null  
  13. select>  
  14. <select id="getDepInfo" parameterType="string" resultMap="depMap">  
  15. SELECT * FROM car_organization where org_parent_coding=#{parentCoding,jdbcType=VARCHAR}  
  16. select>  
  17. <select id="getDepInfoById" parameterType="string" resultMap="depMap">  
  18. SELECT * FROM car_organization where org_coding=#{dep_id}  
  19. select>  
  20. mapper>  


补充:

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

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

[java]  view plain  copy
 print ?
  1. package cn.eguid.carSysWeb;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.builder.SpringApplicationBuilder;  
  6.   
  7. import org.springframework.context.annotation.ComponentScan;  
  8.   
  9. @SpringBootApplication  
  10. //开启通用注解扫描  
  11. @ComponentScan  
  12. public class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {  
  13.     /** 
  14.      * 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行 
  15.      */  
  16.     @Override  
  17.     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
  18.         builder.sources(this.getClass());  
  19.         return super.configure(builder);  
  20.     }  
  21.   
  22.     public static void main(String[] args) {  
  23.         SpringApplication.run(Application.class, args);  
  24.     }  
  25. }  



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容器




org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-starter-tomcat





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

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


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

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

[java]  view plain  copy
 print ?
  1. package cn.eguid.Run;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.builder.SpringApplicationBuilder;  
  6. import org.springframework.boot.web.support.SpringBootServletInitializer;  
  7. import org.springframework.context.annotation.ComponentScan;  
  8.   
  9. import cc.eguid.livepush.PushManager;  
  10. import cc.eguid.livepush.PushManagerImpl;  
  11. import cn.eguid.livePushServer.redisManager.RedisMQHandler;  
  12.   
  13. @SpringBootApplication  
  14. // 开启通用注解扫描  
  15. @ComponentScan  
  16. public class Application extends SpringBootServletInitializer {  
  17.     /** 
  18.      * 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行 
  19.      */  
  20.     @Override  
  21.     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
  22.         builder.sources(this.getClass());  
  23.         return super.configure(builder);  
  24.     }  
  25.       
  26.     public static void main(String[] args) {  
  27.         SpringApplication.run(Application.class, args);  
  28.           
  29.     }  
  30. }  




你可能感兴趣的:(Spring,Boot,Spring,Boot,与,微服务实践)