ssh三大框架的整合

今天复习了一下ssh三大框架的整合,记个笔记,忘记的时候可以随时来翻翻,只为自己看,如果能给你带来一些帮助,那就是我最大的快乐
了,我写的是最基本的东西,有不足的地方,请多多指教。我们这里用的是struts2.2.3+sprint3.1+hibernate3.6.3的版本。

1、导入jar包。包括数据库的连接包,一共需要38个jar包。

2、构建基本的类包结构,其结构如图所示。

因为我们这里侧重于三个框架的整合,所以只用了“增删改查”中的“增”加一个用户来进行测试,其他的的方法大家有需要的可以自己去测试一下。

3、首先我们来写实体层的代码(即vo层)的User类。

@Entity
@Table(name="Users")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
	@Column
	private String name;
	@Column
	private String pass;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(Integer id, String name, String pass) {
		super();
		this.id = id;
		this.name = name;
		this.pass = pass;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}
4、配置数据层,代码如下:

@Repository("userDao")//指定此类为数据层类,并指定向业务层注入的对象的名字为“userDao”
public class UserDaoImpl implements UserDao {

	@Resource //向数据层注入HibernateTemplate对象,用来操作数据
	private HibernateTemplate template;
	
	@Override
	public void save(User user) {
		template.save(user);//用HibernateTemplate来添加数据

	}
}
5、配置业务层,代码如下:

@Service("userService") //此注解指定本类为业务层的类,并且向action中注入的对象名称为“userService”
@Scope(value="prototype")//spring默认为单例模式,prototype指定为非单例模式
@Transactional //用注解指定事物
public class UserServiceImpl implements UserService {

	@Resource//从数据层注入userDao对象(和数据层注入的对象名字保持一致,如果没有相同名字的对象,则会选择按照类型注入)
	private UserDao userDao;
	
	@Override
	public void save(User user) {
		// TODO Auto-generated method stub
		userDao.save(user);//添加用户
	       //throw new RuntimeException();//如果这里抛出异常,那么用户添加不成功;注意:这里只有在测试事物的时候才有用,否则就处于注释状态
	}
}

6、配置控制层,其代码如下:

@Controller//指定本类为控制层的类
public class UserAction extends ActionSupport {
	
	@Resource//注入业务层的对象
	private UserService userService;
	
	private User user;
	
	public String execute(){
		userService.save(user);//添加用户
		return SUCCESS;//如果添加成功,返回SUCCESS常量
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}

7、在src包下建立beans.xml,其基本配置如下:

<!--  配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<!-- 注册驱动类 -->
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<!-- 指定数据库的url -->
		<property name="url" value="jdbc:oracle:thin:@192.168.1.1:1521:orcl"></property>
		<!-- 指定数据库的用户名 -->
		<property name="username" value="sshDemo"></property>
		<!-- 指定数据库的用户密码 -->
		<property name="password" value="123"></property>
	</bean>
	 
	
	<!-- 创建sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<!-- 引用数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定注解的类 -->
		<property name="annotatedClasses">
			<list>
				<value>com.ssh.vo.User</value>
			</list>
		</property>
		<!-- 指定hibernate连接数据库的属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="show_sql">true</prop>
				<prop key="format_sql">true</prop>
				<prop key="hibernate.current_session_context_class">thread</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
			</props>
		</property>
	</bean>
	
	<!-- 指定事物 -->  
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	   
	<!-- 向数据层注入HibernateTemplate,数据层用@Resource来引入template -->
	<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- -指定注解扫描的基包 -->
	<context:component-scan base-package="com.ssh"></context:component-scan>
	
	<!-- -事物驱动注入 -->
	<tx:annotation-driven transaction-manager="txManager"/>

8、配置struts.xml,在src包下面进行配置:

<package name="useraction" namespace="/" extends="struts-default">
		<action name="useraction" class="userAction"> 
			<result>/success.jsp</result>
		</action>
	</package>
指定action的名字为“useraction”,如果成功返回“success",则跳转到success.jsp页面。

9、在web.xml中配置struts过滤器和上下文监听器

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath:beans.xml</param-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>

10、写页面index.jsp,进行添加用户测试。

 <body>
    <form action="useraction" method="post">
    	<table>
    		<tr>
    			<td>用户名:</td>
    			<td><input type="text" name="user.name" /></td>
    		</tr>
    		<tr>
    			<td>密码:</td>
    			<td><input type="text" name="user.pass" /></td>
    		</tr>
    		<tr>
    			<td> </td>
    			<td><input type="submit" value="提交" /></td>
    		</tr>
    	</table>
    </form>
  </body>
第一次的时候,要把数据库连接的连接属性<prop key="hibernate.hbm2ddl.auto">create</prop>,hibernate会自动给我们创建数据表;再次添加的时候,要把”create“改为”update“,否则会把第一次添加的数据覆盖。在进行事物测试的时候,手动抛出RuntimeException异常,数据添加就不会成功。

至此,SSH框架整合基本就完成了。




原文地址:https://www.cnblogs.com/Cilimer/p/4075214.html