SpringBoot集成MyBatis的三種方式
引言
Spring Boot與MyBatis的集成為Java開發(fā)者提供了一種簡便而強大的方式來訪問和操作數(shù)據(jù)庫。在本文中,我們將深入解析Spring Boot集成MyBatis的多種方式,包括XML配置、注解配置以及使用MyBatis Generator生成代碼的方法。
1. XML配置方式
1.1 添加依賴
首先,我們需要在pom.xml文件中添加MyBatis和數(shù)據(jù)庫驅(qū)動的依賴:
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- 數(shù)據(jù)庫驅(qū)動,以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接口和對應(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語句 -->
</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ù)庫驅(qū)動的依賴:
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- 數(shù)據(jù)庫驅(qū)動,以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 運行MyBatis Generator
運行Maven命令執(zhí)行MyBatis Generator:
mvn mybatis-generator:generate
以上是三種常見的Spring Boot集成MyBatis的方式,每種方式都有其適用的場景。開發(fā)者可以根據(jù)項目需求和個人偏好選擇最合適的方式。在實際項目中,可以根據(jù)項目的規(guī)模和復(fù)雜度選擇合適的方式,或者根據(jù)不同的模塊采用不同的集成方式。這樣的靈活性使得Spring Boot與MyBatis的結(jié)合在各種場景下都能夠發(fā)揮強大的作用。
以上就是SpringBoot集成MyBatis的多種方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成MyBatis的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解SpringBoot緩存的實例代碼(EhCache 2.x 篇)
這篇文章主要介紹了詳解SpringBoot緩存的實例代碼(EhCache 2.x 篇),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Spring 處理 HTTP 請求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請求參數(shù)注解的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友參考下吧2024-04-04
Spring Boot 2.7.6整合redis與低版本的區(qū)別
這篇文章主要介紹了Spring Boot 2.7.6整合redis與低版本的區(qū)別,文中補充介紹了SpringBoot各個版本使用Redis之間的區(qū)別實例講解,需要的朋友可以參考下2023-02-02
Mybatis-Plus中分頁插件PaginationInterceptor的使用
我們在開發(fā)的過程中,經(jīng)常會遇到分頁操作,本文主要介紹了Mybatis-Plus中分頁插件PaginationInterceptor的使用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

