struts2,hibernate,spring三大框架整合的注册程序(xml 配置,mysql数据库)

首先呢,新建一个web project,然后导入struts2,hibernate,spring的jar包。

接下来在web.xml下新加listener和filter,分别针对spring和struts2的。web.xml的代码如下:


<?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>register.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.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>
  
</web-app>







。在src下新建下面这些包:

com.guang.action,com.guang.dao,com.guang.dao.impl,com.guang.service,com.guang.service.impl,com.guang.model.

在model下新建实体类User:

package com.guang.model;

public class User {

	private int id;
	
	private String username;
	
	private String password;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
再在model下新建User.hbm.xml,与User POJO相对应,代码如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.guang.model">
	<class name="User" table="user">
		<id name="id" column="id" type="integer">
			<generator class="identity"></generator>
		</id>
		<property name="username" column="username" type="string"></property>
		<property name="password" column="password" type="string"></property>
	</class>



</hibernate-mapping>
下面完成dao层:

dao下新建接口:UserDAO,代码如下:

package com.guang.dao;

import com.guang.model.User;

public interface UserDAO {

	
	public boolean exist(User user);
	
	public void save(User user);
	
	
	
}

再在dao.impl下新建类UserDAOImpl,继承自UserDAO,代码如下:

package com.guang.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.guang.dao.UserDAO;
import com.guang.model.User;
import com.guang.util.HibernateUtil;

public class UserDAOImpl implements UserDAO {
	
	
	private HibernateTemplate hibernateTemplate;
	
	public void save(User user) {

		hibernateTemplate.save(user);
		
	}

	public boolean exist(User user) {

		String hql="from User u where u.username='"+user.getUsername()+"'";
		List<User> users=hibernateTemplate.find(hql);
		
		int count=users.size();
		
		
		if(count>0 && users !=null){
			 
			return true;
		
		}
		return false;
		
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

}

下面完成service层:

在service下新建接口UserService,代码如下:

package com.guang.service;

import com.guang.model.User;

public interface UserService {

	public boolean exist(User user);
	
	public void addUser(User user);
	
	
}

再在service.impl下新建UserServiceImpl继承自UserService,代码如下:

package com.guang.service.impl;

import com.guang.dao.UserDAO;
import com.guang.dao.impl.UserDAOImpl;
import com.guang.model.User;
import com.guang.service.UserService;

public class UserServiceImpl implements UserService {

	UserDAO userdao;
	
	public UserDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDAO userdao) {
		this.userdao = userdao;
	}

	public void addUser(User user) {

		this.userdao.save(user);

	}

	public boolean exist(User user) {

		if(userdao.exist(user)){
			
			return true;
		}
		
		return false;
		
		
	}

}

接下来在action下面新建UserAction类。负责完成注册的,代码如下:

package com.guang.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.guang.model.User;
import com.guang.service.UserService;
import com.guang.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	UserService userservice;
	
	
	public UserService getUserservice() {
		return userservice;
	}







	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}

	private String username;
	
	private String password;
	
	private String repassword;
	
	
	@Override
	public String execute() throws Exception {
		User user=new User();
		
		user.setUsername(username);
		
		user.setPassword(password);
		
		if(userservice.exist(user)){
			
			return "fail";
		}
	
		userservice.addUser(user);
	
		return SUCCESS; 
	}
	
	
	
	
	
	

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	
	
	
	
}

到现在为止,Java的代码基本已经写完了。

接下来配置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.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK"></constant> 
<!-- 允许静态成员访问 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" ></constant>

    <!-- Add packages here -->


<package name="ssh_01" extends="struts-default"  >

	<action name="register" class="com.guang.action.RegisterAction">
		<result name="fail">/register_fail.jsp</result>
		<result name="success">/register_success.jsp</result>
	</action>
</package>








   

</struts>

接下来配置最核心的spring的配置文件,applicationContext.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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>


<context:component-scan base-package="com.guang"></context:component-scan>




<!-- 
<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/spring"/>
<property name="username" value="root"/>
<property name="password" value="guang"/>
</bean>
-->

<!-- 第二种连接方式 -->
<!-- 
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/spring"
p:username="root"
p:password="guang" />
-->
<!-- 第三种连接方式 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"/>
</bean>
<bean id="datasource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!--   annotation 配置 model
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="annotatedClasses">
<list>
<value >com.guang.model.User</value>
</list>
</property>

  -->

<!--xml配置model  -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="mappingResources" >
<list>
<value >com/guang/model/User.hbm.xml</value>
</list>
</property>


<property name="hibernateProperties">
<value>
<!-- 数据库方言 -->
hibernate.dialect=org.hibernate.dialect.MySQLDialect
<!-- 是否显示sql语句 -->
hibernate.show_sql=true
<!-- -->
hibernate.hbm2ddl.auto=update
<!--显示sql语句格式化 -->
hibernate.format_sql=true
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
</value>
</property>
</bean>


<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
	<property name="sessionFactory" ref="SessionFactory"></property>
</bean>

<bean id="userdao" class="com.guang.dao.impl.UserDAOImpl">
	<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="userservice" class="com.guang.service.impl.UserServiceImpl">
	<property name="userdao" ref="userdao"></property>
</bean> 
<bean id="register" class="com.guang.action.RegisterAction" scope="prototype">  
	<property name="userservice" ref="userservice"></property>
</bean>






</beans>


以上的applicationContext.xml中需要注意的是,我列出了三种datasource的实现,最终没被注释这种,也就是实现这种,需要在src下面新建文件:jdbc.properties,将数据库的连接属性写在里面,方便修改,代码如下,可根据自己的实际来修改:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh_01
jdbc.username=root
jdbc.password=guang

注意,除了struts2,hibernate,spring所需的jar包之外,还需要一个jar包,是struts2的用来引进spring来管理自己的action类(也就是dao下面的类)。我这个struts2的版本是:struts2-spring-plugin-2.2.3.1.jar。需要把这个包放到ib下。

还有一点需要注意的是:

在struts.xml里面的action的name属性,必须和applicationContext.xml里面配置的bean同名。

到此,这个小程序就完成了。主要的想法就是整合三大框架到一起。jsp页面在此省略。



你可能感兴趣的:(Hibernate)