mybatis整合spring
1、整合思路
(1)資料來源資訊交給spring管理
(2)SqlSessionFactory交給spring進行單例管理
(3)由spring來管理原始dao的實現類或者mapper代理的代理類
2、案例需求
(1)使用原始dao方式和mapper代理方式實現以下功能:
根據使用者ID查詢商品資訊
3、工程建立
(1)新增jar:ysql的驅動包;Mybatis的核心包和依賴包;Mybatis和spring的整合包;Spring的包;dbcp資料庫連線池包
(2)圖解:
4、整合配置檔案
(1)mybatis配置檔案:
在config下,建立mybatis目錄,然後建立SqlMapConfig.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>
<!-- 載入對映檔案 -->
<mappers>
<mapper resource="mybatis/sqlmap/User.xml"/>
</mappers>
</configuration>
(2)spring配置檔案:
在config下,建立spring目錄,然後建立applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 載入Java配置檔案 -->
<context:property-placeholder location="db.properties"/>
<!-- 建立資料來源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis的全域性配置路徑 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
<!-- 資料來源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- userDao -->
<bean id="userDao" class="com.san.dao.UserDaoImpl">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 配置UserMapper代理類 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 設定代理類的介面 -->
<property name="mapperInterface" value="com.san.mapper.UserMapper"/>
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
5、整合程式碼—原始dao開發
(1)對映檔案:
在config/mybatis下建立sqlmap,然後建立User.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
<!-- 根據使用者ID查詢使用者資訊 -->
<select id="findUserById" resultType="com.san.po.User" parameterType="int">
select * from user where id=#{id}
</select>
</mapper>
(2)dao程式碼:
public interface UserDao {
/**
* 根據使用者ID查詢使用者資訊
* @param id
* @return
*/
public User findUserById(int id);
}
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User findUserById(int id) {
return this.getSqlSession().selectOne("test.findUserById",id);
}
}
(3)配置UserDao實現類:
在applicationContext.xml中配置UserDao實現類
<!-- userDao -->
<bean id="userDao" class="com.san.dao.UserDaoImpl">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
(4)測試程式碼:
private ApplicationContext cxt;
@Before
public void setUp() throws Exception {
cxt = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindUserById() {
UserDao userDao = (UserDao) cxt.getBean("userDao");
User user= userDao.findUserById(1);
System.out.println(user);
}
6、整合程式碼—mapper代理
(1)對映檔案:
將對映檔案放到UserMapper介面的同包下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.san.mapper.UserMapper">
<!-- 根據使用者ID查詢使用者資訊 -->
<select id="findUserById" parameterType="int" resultType="com.san.po.User">
select * from user where id=#{id}
</select>
</mapper>
(2)mapper介面:
public interface UserMapper {
/**
* 根據使用者資訊查詢使用者資訊
* @param id
* @return
*/
public User findUserById(int id);
}
(3)配置mapper代理類:
單個mapper代理類配置:
<!-- 配置UserMapper代理類 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 設定代理類的介面 -->
<property name="mapperInterface" value="com.san.mapper.UserMapper"/>
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
批量設定mapper代理類:
<!-- 批量配置mapper代理類,預設bean的id為類名首字母小寫 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置掃描的包 -->
<property name="basePackage" value="com.san.mapper"></property>
<!-- 預設不需要配置SqlSessionFactory(只有一個SqlSessionFactory時),單獨配置也可以 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
(4)測試程式碼:
private ApplicationContext cxt;
@Before
public void setUp() throws Exception {
cxt = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindUserById() {
UserMapper mapper = (UserMapper) cxt.getBean("userMapper");
User user = mapper.findUserById(1);
System.out.println(user);
}
相關文章
- Spring 整合 MyBatisSpringMyBatis
- Spring整合MyBatisSpringMyBatis
- MyBatis(九) 整合Spring、整合SpringMVCMyBatisSpringMVC
- Spring整合Mybatis plusSpringMyBatis
- Mybatis與Spring整合MyBatisSpring
- Spring整合JUnit,MybatisSpringMyBatis
- Spring Boot 整合 MyBatisSpring BootMyBatis
- spring:spring與mybatis的整合SpringMyBatis
- SSM整合(Spring、SpringMVC、Mybatis)SSMSpringMVCMyBatis
- SSM——Spring整合SpringMVC,MyBatisSSMSpringMVCMyBatis
- SSM(SpringMVC + Spring + Mybatis)整合SSMSpringMVCMyBatis
- spring整合mybatis(idea版)SpringMyBatisIdea
- Spring框架(五)實戰Spring整合MybatisSpring框架MyBatis
- Mybatis 的使用(整合Spring、SpringBoot)MyBatisSpring Boot
- Spring整合Mybatis方式二 - SqlSessionTemplateSpringMyBatisSQLSession
- Spring Boot —— 整合 MyBatis-PlusSpring BootMyBatis
- Spring+SpringMVC+MyBatis框架整合SpringMVCMyBatis框架
- Spring筆記(四)整合MybatisSpring筆記MyBatis
- Spring+SpringMVC+MyBatis的整合SpringMVCMyBatis
- JAVA學習Spring整合MybatisJavaSpringMyBatis
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring同時整合JPA與MybatisSpringMyBatis
- mybatis與spring整合ssm01MyBatisSpringSSM
- MyBatis和Spring整合的奧祕MyBatisSpring
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- 【Spring Boot】快速整合Mybatis-PlusSpring BootMyBatis
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- Spring Boot整合MyBatis實現通用MapperSpring BootMyBatisAPP
- Spring Boot(六)整合 MyBatis 操作 MySQL 8Spring BootMyBatisMySql
- 小代學Spring Boot之整合MyBatisSpring BootMyBatis
- 深入原始碼理解Spring整合MyBatis原理原始碼SpringMyBatis
- Spring Boot系列(三):Spring Boot整合Mybatis原始碼解析Spring BootMyBatis原始碼
- Spring Boot 整合 MyBatis和 SQL Server實踐Spring BootMyBatisSQLServer
- Mybatis與Spring整合時都做了什麼?MyBatisSpring
- Spring+SpringMVC+MyBatis+easyUI整合最佳化SpringMVCMyBatisUI
- 深入淺出MyBatis:MyBatis與Spring整合及實用場景MyBatisSpring
- Spring boot學習(五)Spring boot整合Mybatis Generator以及PageHelperSpring BootMyBatis
- Spring整合Mybatis方式一 - 常規整合 - 註冊對映器SpringMyBatis