ssh整合
程序员文章站
2024-02-28 11:45:40
...
前言
可能有误,后续再改!请谨慎食用!
导入所有jar包
antlr-2.7.6.jar
asm.jar
aspectjweaver.jar
cglib-2.1.jar
commons-beanutils.jar
commons-collections-3.2.1.jar
commons-dbcp-1.2.1.jar
commons-digester.jar
commons-logging.jar
commons-pool-1.2.jar
commons-validator.jar
dom4j-1.6.1.jar
hibernate3.jar
jstl.jar
jta.jar
jtds-1.2.2.jar
mysql-connector-java-5.1.19-bin.jar
mysql-connector-java-5.1.7-bin.jar
spring-webmvc-2.5.6.jar
spring-webmvc-struts-2.5.6.jar
spring.jar
standard.jar
struts-legacy.jar
struts.jar
ValidateExtends.jar
以上jar包可能有多余,先不管。
新建struts配置文件,并在web.xml文件中写入struts配置
<servlet>
<servlet-name>myStruts</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myStruts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
新建hibernate配置文件
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 是否启动表结构自动生成 -->
<property name="hbm2ddl.auto">update</property>
<!-- 是否在控制台显示SQL语句 -->
<property name="show_sql">true</property>
</session-factory>
新建model类和hibernate映射文件
private Integer userId;
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
<hibernate-mapping>
<class name="bysj.model.Demo" table="user_tbl">
<id
name="userId"
column="user_id"
length="36">
<generator class="native"/>
</id>
<property
name="username"
type="java.lang.String"
column="user_name"
insert="true"
length="200"
update="true"
not-null="false"/>
</class>
</hibernate-mapping>
新建spring配置文件
允许加载多个spring配置文件
<!-- 配置数据库连接 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bysj" />
<property name="username" value="root"></property>
<property name="password" value="weiyu1996WEIXIAO"></property>
</bean>
<!-- hibernate创建sessionFactory的工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 加载hibernate配置文件 -->
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
<!-- 加载所有映射文件,路径必须是:xxx/yyy/zzz.hbm.xml -->
<property name="mappingLocations" value="classpath:*/*/*.hbm.xml" />
</bean>
<!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 加载多个spring文件 -->
<import resource="config/spring/spring.xml"/>
在web.xml配置文件中配置spring配置文件
<!-- 配置查找spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 添加spring+struts的监听器支持 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>