ホームページ  >  記事  >  Java  >  Java 開発における SSM 統合のための Spring+mabatis

Java 開発における SSM 統合のための Spring+mabatis

无忌哥哥
无忌哥哥オリジナル
2018-07-23 10:35:281657ブラウズ

Spring と mybatis の統合

統合のアイデア
Spring はシングルトンを通じて SqlSessionFactory を管理する必要があります。
Spring と myatis の統合はプロキシ オブジェクトを生成し、SqlSessionFactory を使用して SqlSession を作成します。 (springとmybatisの統合は自動で完了します)
永続層のmapperとdaoは管理用にspringが必要です。
環境を統合し、新しい Java プロジェクトを作成します (実際の開発プロジェクト構造に近い)
jar パッケージ:
mybatis3.2.7 jar パッケージ
spring 4.3.9 jar パッケージ
mybatis と spring の統合パッケージ: 初期の ibatis と spring の統合 正式に提供by spring、mybatis および spring 統合は mybatis によって提供されます。

sqlSessionFactory

applicationContext.xmlでsqlSessionFactoryを設定します

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

オリジナルのdao開発(Springとの統合後)

mapper.xml

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

dao

public interface UserDao {

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

daoインターフェース実装クラスはSqlsessionFactoryを注入する必要がありますスプリングインジェクション。

ここで Spring は設定メソッドを宣言し、dao Bean を設定します。

UserDaoImpl実装クラスにSqlSessionDaoSupportクラスを継承させます

	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を設定します

applicationContext.xmlでdaoを設定します。

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

テストを実行してください

rree

以上がJava 開発における SSM 統合のための Spring+mabatisの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。