SSH整合 xml版 和注解版

pom.xml

    <dependency>
        <groupId>org.wisdom-frameworkgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.34_1version>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.2.0.RELEASEversion>
    dependency>
    
    <dependency>
        <groupId> org.aspectjgroupId >
        <artifactId> aspectjweaverartifactId >
        <version> 1.8.7version >
    dependency>

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>4.1.8.RELEASEversion>
    dependency>

    
    <dependency>
        <groupId>javaeegroupId>
        <artifactId>javaee-apiartifactId>
        <version>5version>
    dependency>

    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
        <scope>runtimescope>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-txartifactId>
        <version>4.2.5.RELEASEversion>
    dependency>

    
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.2version>
    dependency>

    
    
    <dependency>
        <groupId>javax.transactiongroupId>
        <artifactId>jtaartifactId>
        <version>1.1version>
    dependency>
    
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-ormartifactId>
        <version> 4.2.2.RELEASEversion>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>3.2.13.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.hibernategroupId>
        <artifactId>hibernate-coreartifactId>
        <version>5.2.10.Finalversion>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>RELEASEversion>
    dependency>
    
    
    <dependency>
        <groupId>org.apache.strutsgroupId>
        <artifactId>struts2-coreartifactId>
        <version>2.3.4.1version>
    dependency>
    <dependency>
        <groupId>org.apache.struts.xworkgroupId>
        <artifactId>xwork-coreartifactId>
        <version>2.3.4.1version>
    dependency>
    
    <dependency>
        <groupId>org.apache.strutsgroupId>
        <artifactId>struts2-spring-pluginartifactId>
        <version>2.3.4.1version>
    dependency>

    
    <dependency>
        <groupId>org.apache.strutsgroupId>
        <artifactId>struts2-convention-pluginartifactId>
        <version>2.3.4.1version>
    dependency>

实体类:
public class Dept {
    private Integer deptno;
    private String deptname;
    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }
}
Dao:
   接口:
   
public int addDept(Dept dept);
接口实现:
 
   
private SessionFactory sessionFactory;
public int addDept(Dept dept) {
    Session currentSession = sessionFactory.getCurrentSession();
    Serializable save = currentSession.save(dept);
    return (Integer)save;
}

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}
小配置:
 
   
xml version="1.0"?>
 hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="SSHXML.entity">
    <class name="Dept" table="Dept" >
        <id name="deptno" column="deptno">
            <generator class="native">generator>
        id>
        <property name="deptname" />
    class>
hibernate-mapping>
Service:
  接口:
public interface IDeptService {
    public int addDept(Dept dept);
}
  接口实现:

public class IDeptServiceImp implements IDeptService {
    private IDeptDao dao;
    public int addDept(Dept dept) {
        return dao.addDept(dept);
    }

    public IDeptDao getDao() {
        return dao;
    }

    public void setDao(IDeptDao dao) {
        this.dao = dao;
    }
}
Action:
 
   
public class DeptAction  extends ActionSupport
{
    private Dept dept;
    private IDeptService service;
    public String add(){
        service.addDept(dept);
        return SUCCESS;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public IDeptService getService() {
        return service;
    }

    public void setService(IDeptService service) {
        this.service = service;
    }
}
大配置:
 
   
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost/shpping?characterEncoding=gb2312">property>
        <property name="user" value="root">property>
        <property name="password" value="1234">property>
    bean>

    
    
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="mappingDirectoryLocations" value="classpath:SSHXML/entity">property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.format_sql">trueprop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectprop>
                
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContextprop>
            props>

        property>

    bean>

    
    <bean id="IDeptDao" class="SSHXML.Dao.Imp.IDeptImp">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>

    
    <bean id="service" class="SSHXML.service.Imp.IDeptServiceImp">
        <property name="dao" ref="IDeptDao">property>
    bean>
    
    <bean id="deptAction" class="SSHXML.action.DeptAction" scope="prototype">
        <property name="service" ref="service">property>
    bean>

    
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    

    
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        tx:attributes>
    tx:advice>
    <aop:config>
        
        <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint">aop:advisor>
    aop:config>


beans>
 
    
   
页面:
add.jsp
 
   
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加部门title>
head>
<body>
   <form method="post" action="/add">
      部门名称: <input name="dept.deptname"/>
       <input type="submit" value="添加"/>
   form>
body>
html>
success.jsp
<%@page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>Add ok!h2>
body>
html>

你可能感兴趣的:(SSH整合 xml版 和注解版)