图解SSH三大框架整合及事务处理


 

SSH登录案例

sshlogin项目

1创建用户  ssh  ssh

2创建表:  login   id  name password

3新建项目:loginssh

4添加strutsjar

 4.1)移除项目包

      添加struts2.3.4下面的8个包

5添加hibernate

 5.1)注意添加hibernate3.3

图片

next安钮

图片

没有连接,所以不选择等以后创建

图片

next,也不创建sessionFactory

图片

最后点击finash按钮

开始创建连接数据库

图片

选择new创建一个连接

图片

添加show_sql 值:true

图片

6添加Spring支持

“前四后一“并放在lib

图片

点击next创建applicationContext.xml放在WEB-INF/

图片

点击next

图片

最后点击finash按钮

7修改web.xml文件,添加Springlistener

图片

8:测试项目所有包的正确性:

布署项目到tomcat并启动

确保正常显示页面,表表示jar包没冲突,项目无框架问题

8.1)添加struts2-spring-plugin.jar

(此包用来表明由sping来管理struts中的action)

测试完毕,正常显示测试页面,表明三大框架整合完毕

以下是业务逻辑

9:反转生成实体类

9.1)包:com.hzh.entity

9.2)反转生成实体类及配置文件

图片

接着:

图片

接下来:

图片

下一步,next,next

直到:注意添写包名.类名

图片

Finash结束反转回到myEclipse视图

注意:修改id的类型为int

以下是源代码:

package com.hzh.entity;

 

import java.math.BigDecimal;

 

/**

 * Login entity. @author  MyEclipse Persistence Tools

 */

 

public class Login implements java.io.Serializable {

 

    // Fields

 

    private int id;

    private String name;

    private String password;

 

    // Constructors

 

    /** default constructor */

    public Login() {

    }

 

    /** minimal constructor */

    public Login(int id) {

        this.id = id;

    }

 

    /** full constructor */

    public Login(int id, String name, String password) {

        this.id = id;

        this.name = name;

        this.password = password;

    }

 

    // Property accessors

 

    public int getId() {

        return this.id;

    }

 

    public void setId(int id) {

        this.id = id;

    }

 

    public String getName() {

        return this.name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getPassword() {

        return this.password;

    }

 

    public void setPassword(String password) {

        this.password = password;

    }

 

}

以下是Loign.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">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="com.hzh.entity.Login" table="LOGIN" schema="SSH">

        <id name="id" type="int">

            <column name="ID" precision="22" scale="0" />

            <generator class="assigned"></generator>

        </id>

        <property name="name" type="java.lang.String">

            <column name="NAME" length="4000" />

        </property>

        <property name="password" type="java.lang.String">

            <column name="PASSWORD" length="4000" />

        </property>

    </class>

</hibernate-mapping>

 

9.2)修改hibernate.cfg.xml添加Login.hbm.xml

<mapping resource="com/hzh/entity/Login.hbm.xml" />

 

 

 

10完成DAO接口

10.1)建包:

  com.hzh.dao

10.2)写一个LoginDao接口

package com.hzh.dao;

 

import java.util.List;

 

import com.hzh.entity.Login;

 

 

 

public interface LoginDao {

     public List<Login>find(Login condition);

}

 

10.3)创建包

com.hzh.dao.hibimp

并创建一个实现类

LoginDaoHibImpl

package com.hzh.dao.hibimpl;

 

import java.util.List;

 

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

import com.hzh.dao.LoginDao;

import com.hzh.entity.Login;

 

public class LoginDaoHibImpl extends HibernateDaoSupport implements LoginDao {

 

         public List<Login> find(Login condition) {

                   String hql="from Login l where l.name='"+

                   condition.getName()+"' and l.password='"+

                   condition.getPassword()+"'";

                   return this.getHibernateTemplate().find(hql);

                  

         }

 

}

 

11创建biz

com.hzh.biz

并创建一个接口

LoginBiz

package com.hzh.biz;

 

import com.hzh.entity.Login;

 

 

 

public interface LoginBiz {

    public Login login(Login login)throws Exception;

}

 

112)创建bizimpl

并填加LoginBizImpl

package com.hzh.biz.impl;

 

import java.util.List;

 

 

 

import com.hzh.biz.LoginBiz;

import com.hzh.dao.LoginDao;

import com.hzh.entity.Login;

 

public class LoginBizImpl implements LoginBiz {

     //注入dao

    LoginDao loginDao;

   

    public void setLoginDao(LoginDao loginDao) {

        this.loginDao = loginDao;

    }

 

    public Login login(Login login) throws Exception {

        Login ret=null;

        List<Login>list=loginDao.find(login);

        if(list==null){

            throw new Exception("查无此人");

        }else{

            ret=list.get(0);

           

        }

        return ret;

    }

 

}

 

12创建action

com.hzh.action

并创建LoginAction.java

package com.hzh.action;

 

import com.hzh.biz.LoginBiz;

import com.hzh.entity.Login;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport {

    private Login login;

    // 注入biz

    private LoginBiz loginBiz;

 

    public String login() throws Exception {

        String ret = INPUT;

        Login newLogin = null;

        newLogin = loginBiz.login(login);

        if (newLogin == null) {

            System.out.println("查无此人");

        } else {

 

            ret =SUCCESS;

        }

        return ret;

 

    }

 

    public void setLoginBiz(LoginBiz loginBiz) {

        this.loginBiz = loginBiz;

    }

 

    public void setLogin(Login login) {

        this.login = login;

    }

 

    public Login getLogin() {

        return login;

    }

}

 

13:View

Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

  

   

    <title>登录页面</title>

   

   

 

  </head>

 

  <body>

    <h1>登录</h1>

   

    <s:actionmessage/>

    <s:form action="login">

       <s:textfield name="login.name" label="用户名"/>

       <s:password name="login.password" label="密码"/>

       <s:submit value="登录"></s:submit>

    </s:form>

  </body>

</html>

 

Success.jsp

登录成功!

14配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<constant name="struts.i18n.encoding" value="utf-8" />

 

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

      

        <action name="login" class="loginAction" method="login">

           <result name="input">/login.jsp</result>

           <result name="success">/success.jsp</result>

      </action>

    </package>

 

</struts>   

 

15applicationContext.xml

15.1)先注入loginDao

15.2)然后注入loginBiz

15.3)最后注入loginAction

<?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"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 

 

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="configLocation"

            value="classpath:hibernate.cfg.xml">

        </property>

    </bean>

    <!-- dao -->

    <bean id="loginDao" class="com.hzh.dao.hibimpl.LoginDaoHibImpl">

       <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <!-- biz -->

    <bean id="loginBiz" class="com.hzh.biz.impl.LoginBizImpl">

      <property name="loginDao" ref="loginDao"/>

    </bean>

    <!-- 配置action -->

    <bean id="loginAction" class="com.hzh.action.LoginAction">

       <property name="loginBiz" ref="loginBiz"/>

    </bean>

   

    </beans>

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>index.jsp</welcome-file>

  </welcome-file-list>

 

   <!-- 配置加载spring配置文件 -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>

            /WEB-INF/applicationContext.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>

 

  </web-app>

 

 

升级项目添加事务管理

1增加beans头的命名空间aop,tx

2增加txManager节点(创建事务管理器)

3声明通知<tx:advice>结点

3声明方面<aop>

<?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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="

            http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd

            http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd

            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="configLocation"

            value="classpath:hibernate.cfg.xml">

        </property>

    </bean>

    <!-- 配置事务 -->

    <!-- 配置事务的管理器 -->

    <bean id="txManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <!-- 配置属性 -->

        <property name="sessionFactory" ref="sessionFactory" />

    </bean>

    <!-- 声明规则通知 -->

 

    <tx:advice id="txAdvice" transaction-manager="txManager">

        <!-- 哪些方法应用规则 get/find/query/search/add.... -->

        <tx:attributes>

            <tx:method name="get*" read-only="true" />

            <tx:method name="find*" read-only="true" />

            <tx:method name="query*" read-only="true" />

            <tx:method name="search*" read-only="true" />

            <tx:method name="add*" propagation="REQUIRED" />

            <tx:method name="del*" propagation="REQUIRED" />

            <tx:method name="update*" propagation="REQUIRED" />

            <tx:method name="check*" propagation="REQUIRED" />

            <tx:method name="do*" propagation="REQUIRED" />

            <tx:method name="*" propagation="REQUIRED" read-only="true" />

 

        </tx:attributes>

    </tx:advice>

    <!-- 程序的哪些方法应用这些规则 ,让通知通知谁应用规则  -->

    <aop:config>

       <aop:pointcut expression="execution(* com.hzh.biz.*.*(..))" id="bizMethod"/>

       <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethod"/>

    </aop:config>

    <!-- dao -->

    <bean id="loginDao" class="com.hzh.dao.hibimpl.LoginDaoHibImpl">

       <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <!-- biz -->

    <bean id="loginBiz" class="com.hzh.biz.impl.LoginBizImpl">

      <property name="loginDao" ref="loginDao"/>

    </bean>

    <!-- 配置action -->

    <bean id="loginAction" class="com.hzh.action.LoginAction">

       <property name="loginBiz" ref="loginBiz"/>

    </bean>

   

    </beans>

 


转载于:https://my.oschina.net/superisofty/blog/356633

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值