졸업디자인(5)---spring학습노트(3)의 -dataSource,sessionFactory,hibernateTemplate,사무의 간단한 설정.

7114 단어
1: DataSource 데이터 소스 구성
애플리케이션 Context.xml에서 데이터 Source 구성
<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<!--      dbcp  ,  c3p0   proxool ,      ,    。 -->		
<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		
		<property name="url" value="jdbc:mysql://localhost:3306/    "></property>
		<property name="username" value="root"></property>
		<property name="password" value="111111"></property>
		<property name="maxIdle" value="30"></property> 
		<property name="maxWait" value="10"></property> 
		<property name="defaultAutoCommit" value="false"></property>
	</bean>

dao 또는 서비스에 데이터 Source 주입
public class UserDAOImpl implements UserDAO {
private DataSource dataSource ;

public DataSource getDataSource(){
	return dataSource;
}

@Resource  // bean   
public void setDataSource(DataSource dataSource){
	this.dataSource = dataSource;
}

public void save(User user){
try{
	Connection conn = datasource.getConnection();   //dataSource    ,     。
	conn.createStatement().executeUpdate("insert into user values (null,'wangyi')");
	conn.close();
   }catch(SQLException e){
	e,printStackTrace();
	}
   }
 }


}

2: sessionFactory 구성:
방법1:spring의 프로필 응용 프로그램 Context.xml에서hibernate를 인용합니다.cfg.xml (즉hibernate.cfg.xml 보존)
<!--   sessionFactory -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property> 
 </bean>

방법2:hibernate의hibernate를 사용하지 않습니다.cfg.xml 프로필.
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		
		<!-- 
		<property name="configLocation">
		<value>classpath:hibernate.cfg.xml</value>
		</property>
		 -->
		 
		<property name="dataSource" ref="dataSource"></property>
		<property name="mappingResources">
			<list>
				<value>com/bbs/model/Administrator.hbm.xml</value>
				<value>com/bbs/model/Attention.hbm.xml</value>
				<value>com/bbs/model/Collection.hbm.xml</value>
			</list>
		</property>
		
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

3:hibernateTemplate:
xml 구성:
<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

사용:
	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	public List<Posts> searchAllPosts() {
		List<Posts> list = hibernateTemplate.find("from Posts");
		return list;
	}

4: 트랜잭션:
<!-- dbcp data base connection pool -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/myblog"></property>
		<property name="username" value="root"></property>
		<property name="password" value="111111"></property>
	</bean>
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	
	
	<!--     ,             。。HibernateTransactionManager    aspect     -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- Annotation                -->
	<!--   <tx:annotation-driven transaction-manager="txManager" /> --> 
	<!--           @Transactional                  。
	tx.beginTransaction     tx.commit             callback()
	 -->
	
	
	<!--               -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" />
			<!-- propagation="REQUIRED"   ,      transaction   ,      -->
		</tx:attributes>
	</tx:advice>
	<!--  advice                    。
	pointcut       advice   ,            public  。
	advisor pointcut advice    ,  advice              。
	 -->
	 
	
	<aop:config>
		<aop:pointcut id="managerService"
			expression="execution(public * com.myblog.manager..*.*(..))" />
		<aop:advisor pointcut-ref="managerService" advice-ref="txAdvice" />
	</aop:config>
	
    
        
 

 

 
  
  
  
  

: , 。

좋은 웹페이지 즐겨찾기