您的位置:首页 > 其它

S2SH整合

2010-09-02 22:56 162 查看
参考自:http://ajava.org/course/open/12486.html
1:web.xml配置
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ztest</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!--struts2 配置 -->
<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>
<!--spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!--struts2 整合spring 配置 -->
<servlet>
<servlet-name>contextConfigLocation</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>

2:struts.xml
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="loginAction" method="login">
<result name="success" type="redirect">index.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
</package>
</struts>

3:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="TUser" class="test.spring.TUser">
<property name="username" value="小张"></property>
<property name="allname" value="张三"></property>
<property name="address" value="青岛市"></property>
</bean>
<bean id="loginAction" class="test.LoginAction"></bean>

<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>

<property name="driverUrl"
value="jdbc:sqlserver://localhost:1433;databaseName=cctccati">
</property>
<property name="user" value="sa"></property>
<property name="password" value="123"></property>
<property name="alias">
<value>DB</value>
</property>
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<!-- Hibernate配置文件 -->
<list>
<!-- 文件映射 -->
<!-- User配置文件 -->
<value>test/spring/TUser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.jdbc.use_scrollable_resultset">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- 注入配置 -->
<bean id="productDao" class="hibernate.test.ProductDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="productTarget"
class="hibernate.test.ProductServiceImpl">
<property name="productDao">
<ref bean="productDao" />
</property>
</bean>

<!-- 以下就是声明式事务配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<!-- 目标bean -->
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>

4:hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=cctccati
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>

5:TUser.hbm.xml
<hibernate-mapping package="test.spring">
<class name="TUser" table="t_test">
<id column="user_pk" name="userPk" type="string">
<generator class="guid" />
</id>
<property column="username"       name="username"   type="string" />
<property column="allname"        name="allname"       type="string" />
<property column="address"        name="address"       type="string" />
</class>
</hibernate-mapping>

6:ProductService.java ProductServiceImpl.java ProductDao.java ProductDaoImpl.java
package hibernate.test;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import test.spring.TUser;
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {
/**
*保存用户信息到数据库.<br>
*工程名:ztest<br>
*包名:hibernate.test<br>
*方法名:save方法.<br>
*
*{@inheritDoc}
*author :zhupeng.jz@163.com
*@see hibernate.test.ProductDao#save(test.spring.TUser)
*@param user
*/
@Override
public void save(TUser user) {
getHibernateTemplate().save(user);
}
}

7:测试连接数据库
public static void main(String[] args){
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:1433;DatabaseName=cctccati";
String user="sa";
String password="123";
try{
Class.forName(Driver).newInstance();
Connection conn=DriverManager.getConnection(url,user,password);
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement=conn.createStatement();
String sql="select * from t_item";
ResultSet rs=statement.executeQuery(sql);
System.out.println("---------------");
System.out.println("执行结果如下");
System.out.println("---------------");
System.out.println("题目名称"+"/t"+"题干信息");
System.out.println("---------------");
String name=null;
while(rs.next()){
name=rs.getString("item_name");
//name=new String(name.getBytes("ISO-8859-1"),"GB2312");
System.out.println(name+"/t"+rs.getString("item_theme"));

}
rs.close();

}catch(ClassNotFoundException e){
System.out.println("找不到驱动!");
e.printStackTrace();

}
catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: