Spring整合Struts2,Hibernate的xml方式

       做爲一個學習中的碼農,一直學習纔是咱們的常態,因此最近學習了SSH(Spring,Struts2,Hibernate)整合,數據庫用的MySQL。html

寫了一個簡單的例子,用的工具是IntelliJ Idea,寫的Maven項目。java

1.首先建立項目mysql

咱們看一下項目架構web

2.建立好項目咱們開始項目的第一步,一如jar包,咱們首先考慮一個問題須要什麼jar包,會有什麼問題,jar包衝突問題怎麼解決。spring

筆者遇到的問題比較少,可是遇到了一個log4j與hibernate帶的jbossjar包版本衝突。最後註釋掉log4j就能夠了sql

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>Spring</artifactId>
        <groupId>cn.curry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SSH02</artifactId>
    <packaging>war</packaging>
    <name>SSH02 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!--單測-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
        </dependency>
        <!--javaee-->
        <dependency>
            <groupId>javax.javaee</groupId>
            <artifactId>javaee</artifactId>
            <version>6.0-alpha-1</version>
            <classifier>sources</classifier>
        </dependency>
        <!--slf4j日誌-->
        <!-- <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.7.12</version>
         </dependency>-->
        <!--log4j日誌-->
        <!-- <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.9</version>
         </dependency>-->
        <!--spring核心包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!--aspectJ包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <!--spring-jdbcjar 包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <!--dbcp包-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!--c3p0jar 包-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!--mysql數據庫驅動-->
        <dependency>
            <groupId>org.wisdom-framework</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34_1</version>
        </dependency>
        <!--hibernate核心包-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.0.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>
        <!--oracle數據庫-->
        <!--<dependency>
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc6</artifactId>
         <version>11.2.0.1.0</version>
       </dependency>-->
        <!--jta的jar包-->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <!--支持緩存的核心包-->
        <!-- <dependency>
             <groupId>net.sf.ehcache</groupId>
             <artifactId>ehcache</artifactId>
             <version>2.4.3 </version>
         </dependency>-->
        <!--支持緩存的依賴包-->
        <!--<dependency>
            <groupId>backport-util-concurrent</groupId>
            <artifactId>backport-util-concurrent</artifactId>
            <version>3.1</version>
        </dependency>-->
       <!-- jstl jar包-->
         <dependency>
             <groupId>org.apache.taglibs</groupId>
             <artifactId>taglibs-standard-spec</artifactId>
             <version>1.2.1</version>
         </dependency>

         <dependency>
             <groupId>org.apache.taglibs</groupId>
             <artifactId>taglibs-standard-impl</artifactId>
             <version>1.2.1</version>
         </dependency>
       <!-- mybatis jar包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.5.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.8</version>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.alexkasko.springjdbc</groupId>
            <artifactId>springjdbc-iterable</artifactId>
            <version>1.0.3</version>
        </dependency>-->


    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

 

3.開始代碼書寫,建立實體類,小配置數據庫

package cn.curry.entity;

/**
 * Created by Curry on 2017/4/2.
 */

public class Stock {
    private Integer sid;
    private String sname;
    private Integer scount;

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Integer getScount() {
        return scount;
    }

    public void setScount(Integer scount) {
        this.scount = scount;
    }
}


<?
xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.curry.entity"> <class name="Stock" table="stock"> <id name="sid"> <generator class="native"/> </id> <property name="sname"/> <property name="scount"/> </class> </hibernate-mapping>

實體類小配置很少說。express

3.而後咱們開始寫dao及其實現apache

dao層api

package cn.curry.dao;

import cn.curry.entity.Stock;

/**
 * Created by Curry on 2017/4/2.
 */
public interface IStockDao {
    public int addStock(Stock stock);
}

實現層

package cn.curry.dao.impl;

import cn.curry.dao.IStockDao;
import cn.curry.entity.Stock;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.io.Serializable;


/**
 * Created by Curry on 2017/4/2.
 */
public class StockDao implements IStockDao {
    private SessionFactory sessionFactory;
    public int addStock(Stock stock) {
        Session session = sessionFactory.getCurrentSession();
        Serializable save = session.save(stock);
        Integer count=(Integer)save;

        return count.intValue();
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

4.而後咱們開始寫service

package cn.curry.service;

import cn.curry.entity.Stock;

/**
 * Created by Curry on 2017/4/3.
 */
public interface IStockService {
    public int addStock(Stock stock);
}

接下來寫實現

package cn.curry.service.impl;

import cn.curry.dao.IStockDao;
import cn.curry.entity.Stock;
import cn.curry.service.IStockService;

/**
 * Created by Curry on 2017/4/3.
 */
public class StockService implements IStockService{
    private IStockDao stockDao;
    public int addStock(Stock stock) {
        return stockDao.addStock(stock);
    }

    public IStockDao getStockDao() {
        return stockDao;
    }

    public void setStockDao(IStockDao stockDao) {
        this.stockDao = stockDao;
    }
}

簡單說兩句,以上都是一些最基礎的東西,能夠看出咱們是對數據庫stock表添加一條數據。

5.繼續咱們的重點了開始書寫Action層

package cn.curry.action;

import cn.curry.entity.Stock;
import cn.curry.service.IStockService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * Created by Curry on 2017/4/4.
 */
//繼承ActionSupport使其成爲action類,實現ModelDriven實現屬性名自動裝配
public class StockAction extends ActionSupport implements ModelDriven<Stock>{
    //植入stock實體類
    private Stock stock;
    //植入iStockService業務層
    private IStockService iStockService;
    
    public StockAction(){
        stock=new Stock();
    }
    @Override//重寫execute
    public String execute() throws Exception {
        int count = iStockService.addStock(stock);
        if (count>0){
            return SUCCESS;
        }else{
            return INPUT;
        }

    }

    public Stock getModel() {
        return stock;
    }

    public Stock getStock() {
        return stock;
    }

    public void setStock(Stock stock) {
        this.stock = stock;
    }

    public IStockService getiStockService() {
        return iStockService;
    }

    public void setiStockService(IStockService iStockService) {
        this.iStockService = iStockService;
    }
}

6.咱們先寫頁面最後在寫最重點的xml配置

咱們準備了兩個頁面一個index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>添加</title>
</head>
<body>
<h2>添加股票</h2>
<form action="addStock" method="post">
    <input name="sname"/>
    <input name="scount"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

第二個成功頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功</title>
</head>
<body>
<h1>添加成功!!!</h1>
</body>
</html>

7.最後的配置文件部分了,咱們先寫一個數據庫鏈接的配置文件jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///y2163
jdbc.username=root
jdbc.password=1234

而後咱們看strust2的配置strus.xml

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

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="messages" />
    <package name="default" namespace="/"  extends="struts-default">
        <!--控制權交給Spring容器因此class配置的是Spring配置的注入id-->
        <action name="addStock" class="stockAction">
            <result name="success">success.jsp</result>
            <result name="input">index.jsp</result>
        </action>
     </package>  

</struts>

接下來是咱們相當重要的的applicationContext.xml的配置,裏面配置鏈接數據庫,關聯hibernate,關聯Struts2

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--<context:component-scan base-package="cn.curry"></context:component-scan>-->
    <!--關聯jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--配置數據源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--配置注入sessionFactory-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!--數據源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--hibernate屬性配置,代替hibernate的大配置-->
        <property name="hibernateProperties">
            <props>
                <!--sql方言-->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL57InnoDBDialect</prop>
                <!--是否輸出sql語句-->
                <prop key="hibernate.show_sql">true</prop>
                <!--是否格式化輸出sql語句-->
                <prop key="hibernate.format_sql">true</prop>
                <!--是否與當前線程綁定-->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
        <!--關聯小配置-->
        <property name="mappingDirectoryLocations" value="classpath:cn/curry"></property>
    </bean>
    <!--注入dao-->
    <bean id="stockDao" class="cn.curry.dao.impl.StockDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--注入service-->
    <bean id="stockService" class="cn.curry.service.impl.StockService">
        <property name="stockDao" ref="stockDao"></property>
    </bean>
    <bean id="stockAction" class="cn.curry.action.StockAction">
        <property name="iStockService" ref="stockService"></property>
    </bean>
    <!--事物管理器-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--aop配置事物管理-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="piontCut" expression="execution(* *..service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="piontCut"/>
    </aop:config>
</beans>

最後配置一下web.xml,就能夠發佈項目了

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--讀取配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<!--配置Struts2-->
  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--配置監聽器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--歡迎頁-->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

而後咱們完成了咱們項目的所有配置,最後咱們啓動程序看一下結果

首先看一下數據庫的數據

而後啓動服務,看頁面

而後咱們再看一下數據庫

咱們看到成功的在數據庫添加了一條數據。

到此爲止,咱們的xml整合已經所有完成。

  總結:咱們有大量的配置,因此咱們要當心不要寫錯名字,引用錯名字,最後在編寫類的時候不要引用錯包,主要仍是多寫多練,熟悉每一步的配置。

相關文章
相關標籤/搜索