SSH
1. 搭建SSH 框架
spring 包(6)
aspectjrt.jar aspectjweaver.jar c3p0-0.9.1.2.jar cglib-nodep-2.1_3.jar
common-annotations.jar spring.jar
hibernate 包(15)
antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar
ehcache-1.2.3.jar ejb3-persistence.jar
hibernate3.jar hibernate-annotations.jar hibernate-cglib-repack-2.1_3.jar hibernate-commons-annotations.jar
hibernate-entitymanager.jar
javassist-3.4.GA.jar
jta-1.1.jar log4j.jar
slf4j.api-1.5.2.jar slf4j-log4j12.jar
struts2.1 包(10)
commons-fileupload-1.2.1.jar commons-io-1.3.2.jar commons-logging-1.0.4.jar
freemarker-2.3.13.jar jstl.jar ognl-2.6.11.jar
standard.jar struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar xwork-2.1.2.jar
mysql
mysql-connector-java-5.1.5-bin.jar
2.创建数据库userlogin
3.几个 xml 配置文件的写法
struts2.1 中web.xml:
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,
我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
beans.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: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/context
http://www.springframework.org/schema/context/spring-context-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">
<context:component-scan base-package="com.test"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/userlogin?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/test/model/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<!-- 配置一个事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 默认的视图主题 -->
<!--
<constant name="struts.ui.theme" value="simple" />
-->
<constant name="struts.objectFactory" value="spring" />
<package name="person" extends="struts-default" >
<action name="manage_*" class="personManageAction" method="{1}">
<result name="message">/WEB-INF/user/message.jsp</result>
<result name="delete">/WEB-INF/user/person.jsp</result>
<result name="list">/WEB-INF/user/person.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
一个实体Users 映射文件
Users.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 package="com.test.model">
<class name="Person" table="person">
<id name="username" length="20"/>
<property name="password" length="20" not-null="false" />
<property name="image" length="41" not-null="false" />
<property name="gender" length="5" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">com.test.model.Gender</param>
<!-- 12为java.sql.Types.VARCHAR常量值,即保存枚举的字段值到数据库,
如果不指定type参数,保存到数据库的值为枚举的索引值( 从0开始) -->
<param name="type">12</param>
</type>
</property>
</class>
</hibernate-mapping>
4.模块开发
按照以下顺序依次编码
(1)实体bean
(2)业务bean
(3)分页显示列表(Action)
(4) 增/删/改 操作(Action)
这里写一个测试类:
PersonTest.java
package junit.test;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.model.Gender;
import com.test.model.Person;
import com.test.service.PersonService;
public class PersonTest {
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try{
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService)act.getBean("personServiceImpl");
}catch(RuntimeException e){
e.printStackTrace();
}
}
@Test
public void save(){
personService.save(new Person("wsxw" ,"000000"));
}
@Test
public void find(){
Person person = personService.findById("liming");
System.out.println(person.getPassword());
}
@Test
public void update(){
Person person = personService.findById("liming");
person.setPassword("666666");
person.setGender(Gender.WOMEN);
personService.update(person);
}
@Test
public void findAll(){
List<Person> list = personService.findAll();
for(Person person : list)
{
System.out.println(person.getUsername());
}
}
@Test
public void delete(){
Person person = personService.findById("wsxw");
personService.delete(person);
}
}