當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Mybatis与Spring整合之配置文件方式
生活随笔
收集整理的這篇文章主要介紹了
Mybatis与Spring整合之配置文件方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mybatis與Spring整合之配置文件
案例制作步驟——基礎準備工作
? 環境準備
? 業務類與接口準備
? 基礎配置文件
? 整合前基礎準備工作
整合Spring與Mybatis
1. 導入Spring坐標,MyBatis坐標,MySQL坐標,Druid坐標
<?xml version="1.0" encoding="UTF-8"?> <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>com.fs</groupId><artifactId>day01_spring_ioc_mybatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency><!-- jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.9.RELEASE</version></dependency><!-- spring整合mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.1</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.5</version></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.1.20</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies> </project>2. 創建數據庫表,并制作相應的實體類
mysql數據庫表
實體類
package com.fs.pojo;import lombok.Data;@Data public class Account {private Integer id;private String name;private Double money; }3. 定義業務層接口與數據層接口
數據層接口
package com.fs.dao;import com.fs.pojo.Account; import org.apache.ibatis.annotations.*;import java.util.List;public interface AccountDao {@Select("select * from account")List<Account> findAll();//查詢所有 }業務層接口
package com.fs.service;import com.fs.pojo.Account;import java.util.List;public interface AccountService {List<Account> findAll(); }4. 在業務層調用數據層接口,并實現業務方法的調用
package com.fs.service.impl;import com.fs.dao.AccountDao; import com.fs.pojo.Account; import com.fs.service.AccountService;import java.util.List;public class AccountServiceImpl implements AccountService {//在業務層調用數據層接口private AccountDao accountDao;//提供DI依耐注入的set方法public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}//并實現業務方法的調用@Overridepublic List<Account> findAll() {return accountDao.findAll();} }5. jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.93.132:3306/test jdbc.username=root jdbc.password=root6. MyBatis映射配置文件
我這里使用的MyBatis的注解開發,所以沒有使用這個映射配置文件
7. spring配置文件,整合MyBatis(xml中有詳細解釋)
spring核心配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- 引入properties--><context:property-placeholder location="classpath:jdbc.properties"/><!-- 整合druid,把DruidDataSource交給spring管理--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- 整合MyBatis--><!-- 配置MyBatis的會話工廠類 mybatis.spring 下的SqlSessionFactoryBean配置的sqlSessionFactory得到SqlSession,然后MyBatis從spring中拿到SqlSession.getMapper()去動態代理dao--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 給MyBatis配置鏈接池,依耐注入--><property name="dataSource" ref="dataSource"/> <!-- 配置別名掃描的包,被掃描的包下的類起的別名就是類名首字母小寫,用于mapper.xml文件中使用--><property name="typeAliasesPackage" value="com.fs.pojo"/></bean><!--配置MyBatis掃描dao的包,讓MyBatis動態代理生成這個dao的實現類,并交給ioc管理mybatis-spring這個包下MapperScannerConfigurer提供了spring于MyBatis的整合--><bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 告訴MyBatis我dao在哪里.然后MyBatis將這個dao實現,然后給spring管理--><property name="basePackage" value="com.fs.dao"/></bean><!-- 把業務類 AccountServiceImpl 交給ioc管理 --><bean id="accountServiceImpl" class="com.fs.service.impl.AccountServiceImpl"> <!-- 依耐注入dao,這個dao被MyBatis動態代理實現后被spring存放在ioc容器中--><property name="accountDao" ref="accountDao"/></bean> </beans>測試方法
@Testpublic void findAll() {//創建ioc容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//從容器中獲取accountServiceImplAccountServiceImpl accountServiceImpl = (AccountServiceImpl) applicationContext.getBean("accountServiceImpl");//調用方法,查詢結果List<Account> all = accountServiceImpl.findAll();System.out.println(all);}執行結果
總結
以上是生活随笔為你收集整理的Mybatis与Spring整合之配置文件方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈:数据结构之双链表结构与代码模拟双链
- 下一篇: spring的注解开发@Componen