Heim  >  Artikel  >  Java  >  Spring+mabatis für die SSM-Integration in der Java-Entwicklung

Spring+mabatis für die SSM-Integration in der Java-Entwicklung

无忌哥哥
无忌哥哥Original
2018-07-23 10:35:281657Durchsuche

Spring- und Mybatis-Integration

Integrationsideen
Spring muss SqlSessionFactory über einen Singleton verwalten.
Spring- und Myatis-Integration generiert Proxy-Objekte und verwendet SqlSessionFactory, um SqlSession zu erstellen. (Die Integration von Spring und Mybatis wird automatisch abgeschlossen)
Der Mapper und das Dao der Persistenzschicht benötigen Spring für die Verwaltung.
Integrieren Sie die Umgebung und erstellen Sie ein neues Java-Projekt (nahe der tatsächlichen Entwicklungsprojektstruktur)
JAR-Paket:
JAR-Paket von Mybatis3.2.7
JAR-Paket von Spring 4.3.9
Mybatis und Spring-Integrationspaket: Die frühe Integration von Ibatis und Spring wird offiziell von Spring bereitgestellt, und die Integration von Mybatis und Spring wird von Mybatis bereitgestellt.

sqlSessionFactory

SQLSessionFactory in applicationContext.xml konfigurieren

<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>

Ursprüngliche Dao-Entwicklung (nach Integration mit Spring)

mapper.xml

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

dao

public interface UserDao {

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

Die Dao-Schnittstellenimplementierungsklasse muss SqlsessionFactory bis zum Frühjahr injizieren.

Hier deklariert Spring die Konfigurationsmethode und konfiguriert die Dao-Beans.

Lassen Sie die UserDaoImpl-Implementierungsklasse die SqlSessionDaoSupport-Klasse erben

	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;
	}
        }

Dao konfigurieren

Dao in applicationContext.xml konfigurieren.

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

Führen Sie den Test durch

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);
	}

Das obige ist der detaillierte Inhalt vonSpring+mabatis für die SSM-Integration in der Java-Entwicklung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn