SpringMVC Hibernate 주석 기반 설정

15633 단어
최근 에 SpringMVC 를 다시 배 웠 는데 연합 하 이 버 네 이 트 는 손 쓸 길이 없 는 것 을 발 견 했 습 니 다. 프로필 에 대해 잘 모 르 기 때문에 이 편 을 쓰 면 나중에 학습 을 돌 이 켜 볼 수 있 습 니 다.
SpringMVC 에 대해 서 는 잠시 후에 필 기 를 하 겠 습 니 다.
 
Web.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<!-- POST         -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>chapter2</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>chapter2</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

 
주석 기반 설정 chapter 2 - servlet. 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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-2.5.xsd
         http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	
	<context:component-scan base-package="chapter2" ></context:component-scan>
	 <!--        -->
	<mvc:resources location="/resources/" mapping="/resources/**"/>
	<!--                       ,          
	-->
	<mvc:default-servlet-handler />
	

	<!-- Spring    Ioc -->
	<context:annotation-config />

	<!--                    HandlerMapping HandlerAdapter  bean -->
	<mvc:annotation-driven />
	<!-- SpringMVC    
		<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
		<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
	-->

	<!--        “/” ,    “/helloworld/index” -->
	<!--  
	<mvc:view-controller path="/" view-name="forward:/hello" />
	-->
	<!--    @Transactional      
		<tx:annotation-driven proxy-target-class="true" transaction-manager="txManager" /> 
	-->
	<!--         -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="merge*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="put*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="count*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="list*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="interceptorPointCuts"
			expression="execution(* com.ycj..*.*(..))" /><!-- ycj                        -->
		<aop:advisor advice-ref="txAdvice"
			pointcut-ref="interceptorPointCuts" />
	</aop:config>


	<!--   resources   bean.xml -->
	<import resource="classpath: **/spring-*.xml" />

	<bean id="jdbcProperties"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:my.properties"></property>
	</bean>

	<!--              URL        -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" /><!--       -->
		<!--   hibernate     -->
		<!-- ①mappingResources   classpath           xxx.hbm.xml -->
		<!-- ②mappingLocations         and      classpath: /com/xxx/*.hbm.xml -->
		<!-- ③mappingDirectoryLocations           WEB-INF/HibernateMapping or classpath:/XXX/package -->
		<!-- ④mappingJarLocations           jar    -->
		<property name="mappingDirectoryLocations">
			<list>
				<value></value><!--   hibernate     *.hbm.xml -->
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<!--     hibernate           -->
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop><!--      sql   -->
				<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
			</props>
					</property>
	</bean>
	<!-- ViewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

 
 
 
 
 
 
주석 기반 설정 chapter 2 - servlet. 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="jdbcProperties"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:my.properties"></property>
	</bean>
	<!--              URL        -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<!--  
			<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
			</property>
			<property name="url" value="jdbc:mysql://localhost:3306/demo" />
			<property name="username" value="root" />
			<property name="password" value="root" />
		-->
		<property name="driverClassName"
			value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" /><!--       -->
		<!--   hibernate     -->
		<!-- ①mappingResources   classpath           xxx.hbm.xml -->
		<!-- ②mappingLocations         and      classpath: /com/xxx/*.hbm.xml -->
		<!-- ③mappingDirectoryLocations           WEB-INF/HibernateMapping or classpath:/XXX/package -->
		<!-- ④mappingJarLocations           jar    -->
		<property name="mappingDirectoryLocations">
			<list>
				<value></value><!--   hibernate     *.hbm.xml -->
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<!--     hibernate           -->
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
			</props>
			<!-- 
				<props>    			
				//  oracle      ,             SQL      
				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>    
				//  HQL            
				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>    
				//        sql        
				<prop key="hibernate.show_sql">true </prop>    
				// Hibernate     hibernate.use_outer_join       ,         outer join         。      
				<prop key="hibernate.use_outer_join">true </prop>    
				//    ,  cglib    。cglib    Hibernate     PO    ,                      
				<prop key="hibernate.cglib.use_reflection_optimizer">true </prop>    
				//       sql,           
				<prop key="hibernate.format_sql">true </prop>    
				//“useUnicode” “characterEncoding”                      Encode,      Encode      
				<prop key="hibernate.connection.useUnicode">true </prop>    
				//      ,                 .      
				<prop key="hibernate.cache.use_query_cache">false </prop>    
				<prop key="hibernate.default_batch_fetch_size">16 </prop>    
				//                
				<prop key="hibernate.dbcp.maxActive">100 </prop>    
				//                ,DBCP     (0 =   ,1 =   ,2  =    )      
				<prop key="hibernate.dbcp.whenExhaustedAction">1 </prop>    
				//            
				<prop key="hibernate.dbcp.maxWait">1200 </prop>    
				//         ,               
				<prop key="hibernate.dbcp.maxIdle">10 </prop>    
				##    prepared statement   ,  。      
				<prop key="hibernate.dbcp.ps.maxActive">100 </prop>    
				<prop key="hibernate.dbcp.ps.whenExhaustedAction">1 </prop>    
				<prop key="hibernate.dbcp.ps.maxWait">1200 </prop>    
				<prop key="hibernate.dbcp.ps.maxIdle">10 </prop>    
				</props>    				
			-->
		</property>
	</bean>
	<!-- HandlerMapping -->
	<!--       URL Bean    
		 URL "   /hello", Spring      "/hello" bean-->
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<!-- HandlerAdapter -->
	<!--       org.springframework.web.servlet.mvc.Controller  
		 Bean   Spring MVC      -->
	<bean
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

	<!-- ViewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!--     -->
	<bean name="/hello"
		class="cn.javass.chapter2.web.controller.HelloWorldController" />

	<bean name="/login"
		class="cn.javass.chapter2.web.controller.LoginController">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

</beans>

  

좋은 웹페이지 즐겨찾기