您好,登錄后才能下訂單哦!
這篇文章主要講解了“Spring整合MyBatis的實現方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring整合MyBatis的實現方法是什么”吧!
Spring 整合 MyBatis 開發環境除了需要 Spring 的jar包和 MyBatis 的jar包,還需要 Spring 與 MyBatis 整合的中間件 mybatis-spring-xxx.jar,此外還需要數據庫驅動jar包 mysql-connector-java-xxx.jar。
項目案例:Spring + MyBatis 實現對 user 數據庫的查詢。
實現步驟:
【1】創建一個 Maven 項目,然后再 pom.xml 文件中添加相關依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.kgc.sm</groupId> <artifactId>spring06</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring06</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--測試工具--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.9</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <!--IOC 依賴注入--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!--mybatis&spring 整合包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!--aspects 切面--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.9</version> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.9</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!-- 日志工具--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--mybatis分頁插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.9</version> <scope>test</scope> </dependency> </dependencies> </project>
【2】在 resources —> config 目錄下,創建數據庫配置文件 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///java2218?serverTimezone=UTC&useSSL=false&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root
【3】在 resources —> config 目錄下,創建 MyBatis 配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--全局開啟二級緩存--> <setting name="cacheEnabled" value="true"/> </settings> <!--配置mybatis分頁插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 配置mysql方言 --> <property name="helperDialect" value="mysql" /> <!-- 設置為true時,如果pageSize=0就會查詢出全部的結果 --> <property name="pageSizeZero" value="true" /> <!-- 3.3.0版本可用,分頁參數合理化,默認false禁用 --> <!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁 --> <!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空數據 --> <property name="reasonable" value="true" /> </plugin> </plugins> </configuration>
【4】在 resources —> config 目錄下,創建 Spring 配置文件 spring-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--引入外部數據源的配置參數--> <context:property-placeholder location="classpath:config/jdbc.properties"></context:property-placeholder> <!--初始化數據庫連接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--設置數據庫連接池--> <property name="dataSource" ref="dataSource"></property> <!--設置mybatis全局配置文件位置--> <property name="configLocation" value="config/mybatis-config.xml"></property> <!--設置別名的包--> <property name="typeAliasesPackage" value="cn.kgc.sm"></property> <!--設置mapper.xml文件的位置--> <property name="mapperLocations" value="mapper/*.xml"></property> </bean> <!--mapper接口掃描 生成接口代理對象 同時完成對象的托管--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.kgc.sm.mapper"></property> </bean> <!--開啟包掃描 base-package 設置需要掃描的包 --> <context:component-scan base-package="cn.kgc.sm"></context:component-scan> </beans>
【5】創建實體類 User
package cn.kgc.sm.entity; import lombok.Data; @Data public class User { private Integer uId; private String uName; private String uPassword; private Integer uState; }
【6】創建 Mapper 接口 UserMapper
package cn.kgc.sm.mapper; import cn.kgc.sm.entity.User; import java.util.List; public interface UserMapper { List<User> selectAll(); }
【7】創建與 UserMapper 接口映射的 UserMapper.xml 文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.kgc.sm.mapper.UserMapper"> <select id="selectAll" resultType="user"> select * from user </select> </mapper>
【8】創建 Service 業務層接口 UserService
package cn.kgc.sm.service; import cn.kgc.sm.entity.User; import cn.kgc.sm.mapper.UserMapper; import java.util.List; public interface UserService { List<User> selectAll(); }
【9】創建 UserService 接口的實現類 UserServiceImpl
package cn.kgc.sm.service.impl; import cn.kgc.sm.entity.User; import cn.kgc.sm.mapper.UserMapper; import cn.kgc.sm.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public List<User> selectAll() { System.out.println("users = "); List<User> users = userMapper.selectAll(); System.out.println("users = " + users); return users; } }
【10】創建測試類
package cn.kgc.sm.mapper; import cn.kgc.sm.entity.User; import cn.kgc.sm.service.UserService; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config/spring-config.xml") public class UserMapperTest { @Autowired UserService userService; @Test public void Test01(){ List<User> users = userService.selectAll(); users.forEach(user -> System.out.println("user = " + user)); // for (User user : users) { // System.out.println("user = " + user); // } } }
運行結果:
感謝各位的閱讀,以上就是“Spring整合MyBatis的實現方法是什么”的內容了,經過本文的學習后,相信大家對Spring整合MyBatis的實現方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。