SpringBoot集成MyBatis的三種方式
引言
Spring Boot與MyBatis的集成為Java開發(fā)者提供了一種簡(jiǎn)便而強(qiáng)大的方式來(lái)訪問(wèn)和操作數(shù)據(jù)庫(kù)。在本文中,我們將深入解析Spring Boot集成MyBatis的多種方式,包括XML配置、注解配置以及使用MyBatis Generator生成代碼的方法。
1. XML配置方式
1.1 添加依賴
首先,我們需要在pom.xml
文件中添加MyBatis和數(shù)據(jù)庫(kù)驅(qū)動(dòng)的依賴:
<dependencies> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng),以MySQL為例 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies>
1.2 配置數(shù)據(jù)源
在application.properties
中配置數(shù)據(jù)源:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1.3 創(chuàng)建MyBatis配置文件
在src/main/resources
目錄下創(chuàng)建mybatis-config.xml
文件:
<!-- mybatis-config.xml --> <configuration> <!-- 其他配置 --> <mappers> <mapper resource="mapper/UserMapper.xml"/> <!-- 添加其他Mapper --> </mappers> </configuration>
1.4 編寫Mapper接口和XML文件
創(chuàng)建Mapper接口和對(duì)應(yīng)的XML文件:
// UserMapper.java public interface UserMapper { List<User> getAllUsers(); // 其他方法 }
<!-- UserMapper.xml --> <mapper namespace="com.example.mapper.UserMapper"> <select id="getAllUsers" resultType="com.example.model.User"> SELECT * FROM users </select> <!-- 其他SQL語(yǔ)句 --> </mapper>
1.5 使用Mapper接口
在Service或Controller中注入Mapper并使用:
@Service public class UserService { private final UserMapper userMapper; @Autowired public UserService(UserMapper userMapper) { this.userMapper = userMapper; } public List<User> getAllUsers() { return userMapper.getAllUsers(); } // 其他方法 }
2. 注解配置方式
2.1 添加依賴
同樣,添加MyBatis和數(shù)據(jù)庫(kù)驅(qū)動(dòng)的依賴:
<dependencies> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng),以MySQL為例 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies>
2.2 配置數(shù)據(jù)源
同XML配置方式。
2.3 使用注解配置Mapper
在Mapper接口上使用@Mapper
注解:
@Mapper public interface UserMapper { @Select("SELECT * FROM users") List<User> getAllUsers(); // 其他方法 }
2.4 使用Mapper接口
同XML配置方式。
3. 使用MyBatis Generator
3.1 添加依賴
添加MyBatis Generator插件的依賴:
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <!-- 配置文件路徑 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
3.2 編寫Generator配置文件
在src/main/resources
目錄下創(chuàng)建generatorConfig.xml
文件:
<!-- generatorConfig.xml --> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3 "> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/your_database" userId="your_username" password="your_password"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="false"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <!-- 其他表配置 --> </context> </generatorConfiguration>
3.3 運(yùn)行MyBatis Generator
運(yùn)行Maven命令執(zhí)行MyBatis Generator:
mvn mybatis-generator:generate
以上是三種常見的Spring Boot集成MyBatis的方式,每種方式都有其適用的場(chǎng)景。開發(fā)者可以根據(jù)項(xiàng)目需求和個(gè)人偏好選擇最合適的方式。在實(shí)際項(xiàng)目中,可以根據(jù)項(xiàng)目的規(guī)模和復(fù)雜度選擇合適的方式,或者根據(jù)不同的模塊采用不同的集成方式。這樣的靈活性使得Spring Boot與MyBatis的結(jié)合在各種場(chǎng)景下都能夠發(fā)揮強(qiáng)大的作用。
以上就是SpringBoot集成MyBatis的多種方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成MyBatis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解SpringBoot緩存的實(shí)例代碼(EhCache 2.x 篇)
這篇文章主要介紹了詳解SpringBoot緩存的實(shí)例代碼(EhCache 2.x 篇),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友參考下吧2024-04-04Spring Boot 2.7.6整合redis與低版本的區(qū)別
這篇文章主要介紹了Spring Boot 2.7.6整合redis與低版本的區(qū)別,文中補(bǔ)充介紹了SpringBoot各個(gè)版本使用Redis之間的區(qū)別實(shí)例講解,需要的朋友可以參考下2023-02-02Java核心庫(kù)實(shí)現(xiàn)簡(jiǎn)單的AOP
這篇文章主要介紹了如何用Java核心庫(kù)實(shí)現(xiàn)簡(jiǎn)單的AOP,幫助大家為了更好的理解和學(xué)習(xí)AOP的思想,感興趣的朋友可以了解下2020-08-08Mybatis-Plus中分頁(yè)插件PaginationInterceptor的使用
我們?cè)陂_發(fā)的過(guò)程中,經(jīng)常會(huì)遇到分頁(yè)操作,本文主要介紹了Mybatis-Plus中分頁(yè)插件PaginationInterceptor的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java如何使用poi生成簡(jiǎn)單word文檔并導(dǎo)出
這篇文章主要介紹了Java如何使用poi生成簡(jiǎn)單word文檔并導(dǎo)出問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06SpringBoot自定義注解解決公共字段填充問(wèn)題解決
修改時(shí)間,修改人等字段時(shí),這些字段屬于公共字段,本文主要介紹了SpringBoot自定義注解解決公共字段填充問(wèn)題解決,使用它的好處就是可以統(tǒng)一對(duì)這些字段進(jìn)行處理,感興趣的可以了解一下2024-07-07