zoukankan      html  css  js  c++  java
  • 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
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- mybatis-spring-boot --> <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> </dependency>  

     

    1.2、引入mysql 驱动

     

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- mysql--> <dependency> <groupId></groupId> <artifactId></artifactId> </dependency>  

     

    1.3、项目pom.xml一览

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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
     
     在CODE上查看代码片派生到我的代码片
    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
     
     在CODE上查看代码片派生到我的代码片
    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
     
     在CODE上查看代码片派生到我的代码片
    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
     
     在CODE上查看代码片派生到我的代码片
    1. package import import import import import import import import @SpringBootApplication // 开启通用注解扫描 @ComponentScan publicclassextends      * 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行 
    2.      */   
    3. protected this returnsuper publicstaticvoid class }  

     



  • 相关阅读:
    hdu 1106 排序(排序)
    hdu 1040 As Easy As A+B(排序)
    hdu 1029 Ignatius and the Princess IV(排序)
    mysql-9索引
    mysql-8 alter命令
    mysql-7事务管理
    mysql-6正则表达式
    http协议
    9-2交互体验
    9-2专项测试下午
  • 原文地址:https://www.cnblogs.com/xuyatao/p/8110259.html
Copyright © 2011-2022 走看看