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


[html] view plain copy
  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 }  

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM