Classes.hbm.xml:
xml
version= "1.0" encoding ="utf-8"?>
DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<
hibernate-mapping
>
<
class
name="com.oterman.domain.Classes" >
<id name= "cid" type ="long" length="5">
<column name= "cid">column >
<generator class= "increment">generator >
id>
<property name= "cname" length ="20"> property>
<property name= "description" length="200" >property>
class
>
hibernate-mapping
>
================================hibernate.cfg.xml:
xml
version= '1.0' encoding ='utf-8'?>
DOCTYPE
hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<
hibernate-configuration
>
<
session-factory
>
<
property
name="connection.username"> root property>
<
property
name="connection.password"> root property>
<
property
name="connection.url">
jdbc:mysql:// localhost:3306/hibernate_itheima03
property
>
<
property
name="dialect">
org.hibernate.dialect.MySQLDialect
property
>
<
property
name="connection.driver_class">
com.mysql.jdbc.Driver
property
>
<
property
name="hbm2ddl.auto"> update property>
<
property
name="show_sql"> true property>
<
mapping
resource="com/oterman/domain/Classes.hbm.xml" />
session-factory
>
hibernate-configuration
>
|
applicationContext.xml:
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.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"
>
<
import
resource="applicationContext-db.xml"/>
<
import
resource="applicationContext-classes.xml"/>
beans
>
=======================================================
applicationContext-db.xml:
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.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"
>
<bean id= "sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "configLocation" >
<value> classpath:hibernate/hibernate.cfg.xmlvalue >
property>
bean>
<bean id= "transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionFactory">
<ref bean= "sessionFactory"/>
property>
bean>
<tx:advice id= "tx" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name= "save*" read-only="false" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.oterman.service.*.*(..))" id= "trans"/>
<aop:advisor advice-ref="tx" pointcut-ref="trans" />
aop:config>
beans
>
|
@Test
public
void testSessionFactory(){
ApplicationContext context=
new ClassPathXmlApplicationContext("spring/applicationContext.xml" );
SessionFactory sessionFactory=(SessionFactory) context.getBean(
"sessionFactory"
);
}
|
@Test
public
void testSaveClasses(){
ApplicationContext context=
new ClassPathXmlApplicationContext("spring/applicationContext.xml" );
ClassesService classesService=(ClassesService) context.getBean(
"classesService"
);
Classes classes=
new Classes();
classes.setCname(
"hah");
classesService.saveClasses(classes);
}
|
struts.xml:
xml
version= "1.0" encoding ="UTF-8"?>
DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd" >
<
struts
>
<constant name="struts.devMode" value= "true"/>
<package name="struts-global" namespace="/" extends="struts-default" >
<global-results>
<result name= "errHandler" type ="chain">
<param name="actionName" >errorProcessor param>
result>
global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result= "errHandler" />
global-exception-mappings>
<action name= "errorProcessor" class="com.oterman.exception.ErrorProcessor" >
<result> error.jspresult >
action>
package
>
<include file="struts2/struts-classes.xml">include >
struts
>
================================ struts-classes.xml:
xml
version= "1.0" encoding ="UTF-8"?>
DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd" >
<
struts
>
<
package
name="classes" namespace="/" extends="struts-global" >
<action name= "classesAction_*" method ="{1}" class="classesAction" >
<result> index.jspresult >
action>
package
>
struts
>
|
xml
version= "1.0" encoding ="UTF-8"?>
<
web-app
version= "2.5"
xmlns= "http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<welcome-file-list >
<welcome-file >index.jsp welcome-file>
welcome-file-list >
<
listener
>
<listener-class> org.springframework.web.context.ContextLoaderListenerlistener-class >
listener
>
<
context-param
>
<param-name> contextConfigLocationparam-name >
<param-value> classpath:spring/applicationContext.xmlparam-value >
context-param
>
<
filter
>
<filter-name> struts2 filter-name>
<filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter-class>
filter
>
<
filter-mapping
>
<filter-name> struts2 filter-name>
<url-pattern> /* url-pattern>
filter-mapping
>
web-app
>
|