SSH组合工程之-struts2&hibernate&spring(工程下载)

来源:互联网 发布:dhcp服务器端口 编辑:程序博客网 时间:2024/05/16 12:41

java学习记录(9)

   ssh工程实践之struts2&hibernate篇,ssh采用的版本分别是(struts-2.3.16.1, spring-framework-4.3.9.RELEASE, hibernate-release-5.2.10.Final)。从基础使用的角度来指示stuts2的使用。工程将从三个部分进行集成struts2, struts2和hibernate,struts2,hibernate和spring最终集成。
   本篇是ssh组合工程的第三篇,在前面两篇中论述了struts2和hibernate的集成,在本篇中将新增spring的集成。

行文结构

  1. spring是什么
  2. spring jar包的作用
  3. 组合所需要的jar包
  4. 用spring来管理对象,配置文件
  5. 工程代码
  6. 代码下载

1 spring是什么
   这里只是大致的理解spring的含义而不是要去具体的定义它什么,做工程在helloworld阶段只需要知道它的大概意思即可。
spring用来管理工程中对象的生成和销毁,让工程中不在出现new的字眼。让代码人员更加专注于业务的编写。
2 spring 各个jar包的含义
   java中的jar包的管理是比较麻烦的,但是明白每个jar包的作用之后就不会太麻烦。
   网络中关于spring jar包的作用有很多论述就不在一一列举了。
3 组合工程所需要的jar包
spring的所有jar包,并包括aspectjweaver-1.6.12.jar。
两个javassist.jar,但是并不冲突。分别用于不同的工程中。
4 用spring来管理对象
   在struts2&hibernate组合工程时每次进行数据库的操作都需要获取一个session,而在spring中只需要把相应的对象注入到spring中就会由spring来进行管理。主要是理解spring的配置文件。

<?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:tx="http://www.springframework.org/schema/tx"     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/tx            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- 定义数据源的信息 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"        destroy-method="close">        <property name="driverClass">            <value>com.mysql.jdbc.Driver</value>        </property>        <property name="jdbcUrl">            <value>jdbc:mysql://localhost:3306/test?characterEncoding=utf8&amp;useSSL=false</value>        </property>        <property name="user">            <value>root</value>        </property>        <property name="password">            <value>ziling</value>        </property>        <property name="maxPoolSize">            <value>80</value>        </property>        <property name="minPoolSize">            <value>1</value>        </property>        <property name="initialPoolSize">            <value>1</value>        </property>        <property name="maxIdleTime">            <value>20</value>        </property>    </bean>    <!--定义Hibernate的SessionFactory -->    <!-- SessionFactory使用的数据源为上面的数据源 -->    <!-- 指定了Hibernate的映射文件和配置信息 -->    <bean id="sessionFactory"        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">        <property name="dataSource">            <ref local="dataSource" />        </property>        <property name="mappingResources">            <list>                <value>com/ziling/bean/User.hbm.xml</value>            </list>        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="show_sql">true</prop>                <prop key="hibernate.jdbc.batch_size">20</prop>            </props>        </property>    </bean>    <bean id="transactionManager"        class="org.springframework.orm.hibernate5.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">          <tx:attributes>                         <tx:method name="get*" propagation="REQUIRED" />                         <tx:method name="*" propagation="REQUIRED" />          </tx:attributes>      </tx:advice>    <!-- aop代理设置,默认是注入接口要加上proxy-target-class="true" 通过AOP配置提供事务增强,让dao包下所有Bean的所有方法拥有事务-->    <aop:config proxy-target-class="true">          <aop:pointcut id="daoPointcut" expression="execution(* com.ziling.dao.*.*(..))"/>          <aop:advisor advice-ref="transactionAdvice" pointcut-ref="daoPointcut" />      </aop:config>    <bean id="user" class="com.ziling.bean.User"></bean>    <!--用户注册数据访问类 -->    <bean id="loginDao" class="com.ziling.daoImpl.LoginDaoImpl">        <property name="sessionFactory">            <ref bean="sessionFactory" />        </property>        <property name="user">            <ref bean="user" />        </property>    </bean>    <!--用户注册业务逻辑类 -->    <bean id="loginService" class="com.ziling.serviceImpl.LoginServiceImpl">        <property name="loginDao">            <ref bean="loginDao" />        </property>    </bean>    <!-- 用户注册的Action -->    <bean id="loginAction" class="com.ziling.action.LoginAction">        <property name="loginService">            <ref bean="loginService" />        </property>    </bean>    <!-- more bean definitions go here --></beans>
package com.ziling.action;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.io.UnsupportedEncodingException;import com.opensymphony.xwork2.ActionSupport;import com.ziling.serviceImpl.LoginServiceImpl;public class LoginAction extends ActionSupport{    /**     *      */    private static final long serialVersionUID = 1L;    private String username;    private String password;    LoginServiceImpl loginService;    private InputStream inputStream;    public InputStream getInputStream() {        return inputStream;    }    public void setInputStream(InputStream inputStream) {        this.inputStream = inputStream;    }    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 void setLoginService(LoginServiceImpl loginService) {        this.loginService = loginService;    }    @Override    public String execute() throws Exception {//      @SuppressWarnings("resource")//      ApplicationContext context = new FileSystemXmlApplicationContext("F:/zilingJava/workSpace/SSH_Struts&Hibernate&spring/WebContent/WEB-INF/applicationContext.xml");//      loginService = (LoginServiceImpl) context.getBean("loginService");        return loginService.checkUser(username, password);    }    public String returnString() {//      @SuppressWarnings("resource")//      ApplicationContext context = new FileSystemXmlApplicationContext("F:/zilingJava/workSpace/SSH_Struts&Hibernate&spring/WebContent/WEB-INF/applicationContext.xml");//      loginService = (LoginServiceImpl) context.getBean("loginService");        String userNames = loginService.getUserName();        try {            inputStream = new ByteArrayInputStream(userNames                      .getBytes("UTF-8"));        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }          return "success";    }}

5 工程代码
进行判断的类

package com.ziling.serviceImpl;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.ziling.bean.User;import com.ziling.daoImpl.LoginDaoImpl;import com.ziling.service.LoginService;public class LoginServiceImpl implements LoginService {    LoginDaoImpl loginDao;    User user;    public void setLoginDao(LoginDaoImpl loginDao) {        this.loginDao = loginDao;    }    @Override    public String checkUser(String username, String password) {//      @SuppressWarnings("resource")//      ApplicationContext context = new FileSystemXmlApplicationContext("F:/zilingJava/workSpace/SSH_Struts&Hibernate&spring/WebContent/WEB-INF/applicationContext.xml");//      loginDao = (LoginDaoImpl) context.getBean("loginDao");        user = loginDao.getUser(username);        if(user.getUsername()!=null){            if(password.equals(user.getPassword())){                return "success";            }else{                return "error";            }        }else{            return "register";        }    }    public String getUserName() {//      ApplicationContext context = new FileSystemXmlApplicationContext("F:/zilingJava/workSpace/SSH_Struts&Hibernate&spring/WebContent/WEB-INF/applicationContext.xml");//      loginDao = (LoginDaoImpl) context.getBean("loginDao");        return loginDao.getUserName();    }}

6 代码下载
代码下载

原创粉丝点击