Home > Java > javaTutorial > Spring+mabatis for SSM integration in Java development

Spring+mabatis for SSM integration in Java development

无忌哥哥
Release: 2018-07-23 10:35:28
Original
1802 people have browsed it

Spring and mybatis integration

Integration ideas
Spring is required to manage SqlSessionFactory through a singleton.
Spring and myatis integration generate proxy objects and use SqlSessionFactory to create SqlSession. (The integration of spring and mybatis is automatically completed)
The mapper and dao of the persistence layer require spring for management.
Integrate the environment and create a new java project (close to the actual development project structure)
jar package:
jar package of mybatis3.2.7
jar package of spring 4.3.9
mybatis and Spring integration package: Early integration of ibatis and spring is officially provided by spring, and integration of mybatis and spring is provided by mybatis.

sqlSessionFactory

Configure sqlSessionFactory in applicationContext.xml

<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 ">
	
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	
	<!-- 数据源,使用dbcp -->        
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"   destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}"/><!-- jdbc:mysql://localhost:3306/test?characterEncoding=utf8 -->
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.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 name="dataSource" ref="dataSource"></property>
		
	</bean>
	<!-- 原始dao接口 -->
 	<bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> 

</beans>
Copy after login

Original dao development (after integration with spring)

mapper.xml

 <!-- 加载映射文件 -->
 <mappers>
 <mapper resource="sqlmap/User.xml"/>
Copy after login

dao

public interface UserDao {

	//根据id查询用户信息
	public User findUserById(int id)throws Exception;	
Copy after login

dao interface implementation class needs to inject SqlsessionFactory through spring.

Here spring declares the configuration method and configures the dao beans.

Let the UserDaoImpl implementation class inherit the SqlSessionDaoSupport class

	public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{

        @Override
	public User findUserById(int id) throws Exception {
		//继承SqlSessionDaoSupport ,通过this.getSqlSession()得到sqlSession
		SqlSession sqlSession = this.getSqlSession();
		User user = sqlSession.selectOne("test.findUserById", id);
		//释放资源
		//sqlSession.close();
		return user;
	}
        }
Copy after login

Configure dao

Configure dao in applicationContext.xml.

	<!-- 原始dao接口 -->
 	<bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
Copy after login

Run test

public class UserDaoImplTest {
	
	private ApplicationContext applicationContext;
	//在setUp这个方法中得到spring容器
	@Before
	public void setUp()throws Exception{
		applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
	}
	
	@Test
	public void testFindUserById() throws Exception {
		UserDao userDao=(UserDao) applicationContext.getBean("userDao");
//		//调用userDao的方法
		User user=userDao.findUserById(1);
		System.out.println(user);
	}
Copy after login

The above is the detailed content of Spring+mabatis for SSM integration in Java development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template