JavaWeb_(Spring妗嗘灦)Spring鏁村悎Hibernate

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb_(Spring妗嗘灦)Spring鏁村悎Hibernate相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/enc' title='enc'>enc   org   director   cut   jdbc   color   杩愯   splay   strong   

 

 

銆€銆€Dao灞傜被瑕佺户鎵縃ibernateDaoSupport.java鐖剁被

 

銆€銆€鍘熷厛浣跨敤Hibernate妗嗘灦hibernate.cfg.xml閰嶇疆鏁版嵁搴?/p>

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///spring</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connecton.password">123456</property>
        
        <!-- 鏁版嵁搴撶殑鏂硅█ -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 鏄惁鏄剧ずsql璇彞 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 鏄惁鏍煎紡鍖杝ql璇彞 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 鏁版嵁搴撴槸鍚﹀府鎴戜滑鍒涘缓琛?-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 闅旂绾у埆 涓嶅彲閲嶅璇? 榛樿4绾у埆-->
        <property name="hibernate.connection.isolation">4</property>
        <!-- 涓庣嚎绋嬬粦瀹?-->
        <property name="hibernate.current_session_context_class">thread</property>
    
    
        <mapping resource="com/Gary/domain/User.hbm.xml"/>
    
    </session-factory>
</hibernate-configuration>

 

鎶€鏈浘鐗? id=
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///spring</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connecton.password">123456</property>
        
        <!-- 鏁版嵁搴撶殑鏂硅█ -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 鏄惁鏄剧ずsql璇彞 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 鏄惁鏍煎紡鍖杝ql璇彞 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 鏁版嵁搴撴槸鍚﹀府鎴戜滑鍒涘缓琛?-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 闅旂绾у埆 涓嶅彲閲嶅璇? 榛樿4绾у埆-->
        <property name="hibernate.connection.isolation">4</property>
        <!-- 涓庣嚎绋嬬粦瀹?-->
        <property name="hibernate.current_session_context_class">thread</property>
    
    
        <mapping resource="com/Gary/domain/User.hbm.xml"/>
    
    </session-factory>
</hibernate-configuration>
hibernate.cfg.xml

 

銆€銆€浣跨敤Spring妗嗘灦鏁村悎Hibernate鍚?/p>

<!-- 閰嶇疆鏁版嵁搴?-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    
    <!-- 閰嶇疆sessionFactory -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref=""></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>

 

鎶€鏈浘鐗? id=
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/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">
    
    <!-- 寮€鍚敞瑙fā寮?寮€鍚寘鐨勬椂鍊欙紝浼氬紑鍚寘涓嬬殑鎵€鏈夊瓙鍖?/span>-->
    <context:component-scan base-package="com.Gary"></context:component-scan>
    
    <!-- 閰嶇疆鏁版嵁搴?-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    
    <!-- 閰嶇疆sessionFactory -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
        
        <!-- 閰嶇疆orm鍏冪礌鎹紝浼氳嚜鍔ㄦ壂鎻忚鍖呬笅鐨? 鎵€鏈? 閰嶇疆鏂囦欢 -->
        
        <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property>
        
    </bean>
    

    
    
    
    
    <!-- 閰嶇疆Action    --> 
    <bean name = "userAction" class="com.Gary.web.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    
    <!-- 閰嶇疆Service  -->
    <bean name="userService" class="com.Gary.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
    <!--閰嶇疆Dao -->
    <bean name="userDao" class="com.Gary.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
     
     
     
     
</beans>
applicationContext.xml

 

銆€銆€鍦―ao灞傜敤鍒癝ession session = HibernateUtils.getCurrentSession()锛涜幏寰梥ession鍙互淇敼鎴?/p>

Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

 

銆€銆€鍦⊿ervice灞傜敤鍒扮殑鍏充簬浜嬪姟鐨勫紑鍚垜浠彲浠ョ粺涓€鍦╝pplicationContext.xml涓繘琛岄厤缃?/p>

銆€銆€寮€鍚簨鍔ransaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction();

銆€銆€浜嬪姟鍥炴粴beginTransaction.rollback();

銆€銆€鎻愪氦浜嬪姟beginTransaction.commit();

 

 銆€銆€鍦╝pplicationContext.xml涓厤缃簨鍔℃牳蹇冪鐞嗗櫒

<!-- 浜嬪姟鐨勬牳蹇冪鐞嗗櫒 -->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <!-- 闇€瑕乻essionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 閰嶇疆浜嬪姟鐨勯€氱煡 -->
    <tx:advice id="advice"
        transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 鎵€鏈夌殑鏂规硶閮藉幓澧炲己 -->
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <!-- 缁囧叆 -->
    <aop:config>
        <!-- 鍒囧叆鐐?-->
        <aop:pointcut
            expression="execution(* com.Gary.service.*.*(..))" id="pc" />
        <!-- 閰嶇疆鍒囬潰 -->
        <aop:advisor advice-ref="advice" pointcut-ref="pc" />
    </aop:config>

 

鎶€鏈浘鐗? id=
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/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">
    
    <!-- 寮€鍚敞瑙fā寮?寮€鍚寘鐨勬椂鍊欙紝浼氬紑鍚寘涓嬬殑鎵€鏈夊瓙鍖?/span>-->
    <context:component-scan base-package="com.Gary"></context:component-scan>
    
    <!-- 閰嶇疆鏁版嵁搴?-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    
    <!-- 閰嶇疆sessionFactory -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        <!-- 閰嶇疆orm鍏冪礌鎹紝浼氳嚜鍔ㄦ壂鎻忚鍖呬笅鐨? 鎵€鏈? 閰嶇疆鏂囦欢 -->
        
        <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property>
        
    </bean>
    
    <!-- 浜嬪姟鐨勬牳蹇冪鐞嗗櫒 -->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <!-- 闇€瑕乻essionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 閰嶇疆浜嬪姟鐨勯€氱煡 -->
    <tx:advice id="advice"
        transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 鎵€鏈夌殑鏂规硶閮藉幓澧炲己 -->
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <!-- 缁囧叆 -->
    <aop:config>
        <!-- 鍒囧叆鐐?-->
        <aop:pointcut
            expression="execution(* com.Gary.service.*.*(..))" id="pc" />
        <!-- 閰嶇疆鍒囬潰 -->
        <aop:advisor advice-ref="advice" pointcut-ref="pc" />
    </aop:config>
    
    
    
    
    <!-- 閰嶇疆Action    --> 
    <bean name = "userAction" class="com.Gary.web.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    
    <!-- 閰嶇疆Service  -->
    <bean name="userService" class="com.Gary.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
    <!--閰嶇疆Dao -->
    <bean name="userDao" class="com.Gary.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
     
     
     
     
</beans>
applicationContext.xml

 

 銆€銆€鍦╳eb.xml涓墿澶ession鑼冨洿锛屼娇Session浣滅敤浜庝粠椤甸潰鍙戣捣璇锋眰涓€鐩村埌椤甸潰鐨勫叧闂?/p>

    <!-- 鎵╁ぇ鍒癝ession鑼冨洿锛岄〉闈㈠彂璧疯姹?鍒?椤甸潰鐨勫叧闂?-->
  <filter>
      <filter-name>openSession</filter-name>
      <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>openSession</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

 

鎶€鏈浘鐗? id=
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
     <!-- 璁﹕pring闅忕潃web椤圭洰鐨勫惎鍔ㄨ€屽惎鍔?-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 閰嶇疆spring閰嶇疆鏂囦欢鐨勪綅缃?-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 鎵╁ぇ鍒癝ession鑼冨洿锛岄〉闈㈠彂璧疯姹?鍒?椤甸潰鐨勫叧闂?-->
  <filter>
      <filter-name>openSession</filter-name>
      <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>openSession</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
  <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>
  
  
</web-app>
web.xml

 

銆€銆€

銆€銆€JavaWeb_(Spring妗嗘灦)鍦⊿truts+Hibernate妗嗘灦涓紩鍏pring妗嗘灦銆€銆€浼犻€侀棬

銆€銆€鐜板湪浣跨敤Spring妗嗘灦鏁村悎Struts+Hibernate椤圭洰锛岃繍琛岄」鐩?/p>

銆€銆€鎶€鏈浘鐗? src=

 

 

鎶€鏈浘鐗? src=

 

鎶€鏈浘鐗? id=
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="css/head.css" />
<link rel="stylesheet" type="text/css" href="css/login.css" />
</head>

<body>
    <div class="dvhead">
        <div class="dvlogo">
            <a href="index.html">浣犻棶鎴戠瓟</a>
        </div>
        <div class="dvsearch">10绉掗挓娉ㄥ唽璐﹀彿锛屾壘鍒颁綘鐨勫悓瀛?span style="color: #0000ff;"></div>
        <div class="dvreg">
            宸叉湁璐﹀彿锛岀珛鍗?/span>&nbsp;<a href="login.html">鐧诲綍</a>
        </div>
    </div>
    <section class="sec">
        <form action="${pageContext.request.contextPath }/UserAction_login" method="post">
            <div class="register-box">
                <label for="username" class="username_label"> 鐢?鎴?鍚?<input
                    maxlength="20" name="username" type="text" placeholder="鎮ㄧ殑鐢ㄦ埛鍚嶅拰鐧诲綍鍚? />
                </label>
                <div class="tips"></div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label"> 瀵?鐮?<input
                    maxlength="20" type="password" name="password"
                    placeholder="寤鸿鑷冲皯浣跨敤涓ょ瀛楃缁勫悎" />
                </label>
                <div class="tips"></div>
            </div>
            <div class="arguement">
                <input type="checkbox" id="xieyi" /> 闃呰骞跺悓鎰?<a
                    href="javascript:void(0)">銆婁綘闂垜绛旂敤鎴锋敞鍐屽崗璁€?span style="color: #0000ff;"></a> <a href="register.html">娌℃湁璐﹀彿,绔嬪嵆娉ㄥ唽</a>
                <div class="tips" style="color: red" > <s:property value="#error"/> </div>
            </div>
            <div class="submit_btn">
                <button type="submit" id="submit_btn">绔?鍗?鐧诲綍</button>
            </div>
        </form>
    </section>
    <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</body>
login.jsp

 

鎶€鏈浘鐗? id=
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
     <!-- 璁﹕pring闅忕潃web椤圭洰鐨勫惎鍔ㄨ€屽惎鍔?-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 閰嶇疆spring閰嶇疆鏂囦欢鐨勪綅缃?-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 鎵╁ぇ鍒癝ession鑼冨洿锛岄〉闈㈠彂璧疯姹?鍒?椤甸潰鐨勫叧闂?-->
  <filter>
      <filter-name>openSession</filter-name>
      <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>openSession</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
  <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>
  
  
</web-app>
web.xml

 

銆€銆€com.Gary.dao

鎶€鏈浘鐗? id=
package com.Gary.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils;


public class UserDao extends HibernateDaoSupport{

    public User findUser(User user) {
        
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        //Dao灞傛帶鍒朵簨鍔?       鍋氭硶鏄潪甯镐笉濂界殑

        //Session session = HibernateUtils.getCurrentSession();
        //sql鎿嶄綔鏁版嵁搴?/span>
        String sql = "select * from user where username = ? and password = ?";
        NativeQuery sqlQuery = session.createSQLQuery(sql);
        sqlQuery.addEntity(User.class);
        sqlQuery.setParameter(1, user.getUsername());
        sqlQuery.setParameter(2, user.getPassword());
        
        User result = (User) sqlQuery.uniqueResult();

        
        return result;
    }

}
UserDao.java

 

銆€銆€com.Gary.domain

鎶€鏈浘鐗? id=
package com.Gary.domain;

public class User {

    private String id;
    private String username;
    private String password;
    public String getId() {
        return id;
    }
    public void setId(String 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;
    }
    
    
    
}
User.java

 

鎶€鏈浘鐗? id=
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping package="com.Gary.domain">

    <class name="User" table="user">
        <id name="id">
            <generator class="uuid"></generator>
        </id>
        
        <property name="username" column="username"></property>
        <property name="password" column="password"></property>
        
    </class>

</hibernate-mapping>
User.hbm.xml

 

銆€銆€com.Gary.service

鎶€鏈浘鐗? id=
package com.Gary.service;

import javax.annotation.Resource;

import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.Gary.dao.UserDao;
import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils;


public class UserService {

    private UserDao userDao;
    
    public boolean findUser(User user) {
        
        
        //Transaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction();
        User temp = null;
        try {
            
            temp = userDao.findUser(user);
        }catch(Exception e){
            //beginTransaction.rollback();
        }
        
        //beginTransaction.commit();
        
        //System.out.println("UserService"+temp);
        
        return temp==null?false:true;    
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    
    
}
UserService.java

 

銆€銆€com.Gary.utils

鎶€鏈浘鐗? id=
package com.Gary.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

    private static SessionFactory sessionFactory = null;
    
    static {
        Configuration config = new Configuration().configure();
        sessionFactory = config.buildSessionFactory();
    }
    
    public static Session getSession()
    {
        return sessionFactory.openSession();
    }
    
    public static Session getCurrentSession()
    {
        return sessionFactory.getCurrentSession();
    }
    
}
HibernateUtils.java

 

銆€銆€com.Gary.web

鎶€鏈浘鐗? id=
package com.Gary.web;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class UserAction extends ActionSupport implements ModelDriven<User>{

    public User user = new User();

    private UserService userService;
    
    public String login() throws Exception {
        
        
        
        boolean success = userService.findUser(user);
        
        
        //System.out.println("UserService"+user.getUsername()+user.getPassword());        
        
        if(success)
        {
            return "toIndex";
        }else {
            ActionContext.getContext().put("error", "鐢ㄦ埛鍚嶆垨鑰呭瘑鐮侀敊璇紒锛?);
            return "login";
        }
        
    }

    @Override
    public User getModel() {
        
        return user;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    
    
    
    
}
UserAction.java

 

鎶€鏈浘鐗? id=
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/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">
    
    <!-- 寮€鍚敞瑙fā寮?寮€鍚寘鐨勬椂鍊欙紝浼氬紑鍚寘涓嬬殑鎵€鏈夊瓙鍖?/span>-->
    <context:component-scan base-package="com.Gary"></context:component-scan>
    
    <!-- 閰嶇疆鏁版嵁搴?-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    
    <!-- 閰嶇疆sessionFactory -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        <!-- 閰嶇疆orm鍏冪礌鎹紝浼氳嚜鍔ㄦ壂鎻忚鍖呬笅鐨? 鎵€鏈? 閰嶇疆鏂囦欢 -->
        
        <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property>
        
    </bean>
    
    <!-- 浜嬪姟鐨勬牳蹇冪鐞嗗櫒 -->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <!-- 闇€瑕乻essionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 閰嶇疆浜嬪姟鐨勯€氱煡 -->
    <tx:advice id="advice"
        transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 鎵€鏈夌殑鏂规硶閮藉幓澧炲己 -->
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <!-- 缁囧叆 -->
    <aop:config>
        <!-- 鍒囧叆鐐?-->
        <aop:pointcut
            expression="execution(* com.Gary.service.*.*(..))" id="pc" />
        <!-- 閰嶇疆鍒囬潰 -->
        <aop:advisor advice-ref="advice" pointcut-ref="pc" />
    </aop:config>
    
    
    
    
    <!-- 閰嶇疆Action    --> 
    <bean name = "userAction" class="com.Gary.web.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    
    <!-- 閰嶇疆Service  -->
    <bean name="userService" class="com.Gary.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
    <!--閰嶇疆Dao -->
    <bean name="userDao" class="com.Gary.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
     
     
     
     
</beans>
applicationContext.xml

 

銆€銆€hibernate.cfg.xml銆愬彲涓嶅啓銆?/strong>

鎶€鏈浘鐗? id=
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///spring</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connecton.password">123456</property>
        
        <!-- 鏁版嵁搴撶殑鏂硅█ -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 鏄惁鏄剧ずsql璇彞 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 鏄惁鏍煎紡鍖杝ql璇彞 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 鏁版嵁搴撴槸鍚﹀府鎴戜滑鍒涘缓琛?-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 闅旂绾у埆 涓嶅彲閲嶅璇? 榛樿4绾у埆-->
        <property name="hibernate.connection.isolation">4</property>
        <!-- 涓庣嚎绋嬬粦瀹?-->
        <property name="hibernate.current_session_context_class">thread</property>
    
    
        <mapping resource="com/Gary/domain/User.hbm.xml"/>
    
    </session-factory>
</hibernate-configuration>
hibernate.cfg.xml

 

鎶€鏈浘鐗? id=
<?xml version="1.0" encoding="UTF-8"?>

<!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>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    
    <!-- 鍛婅瘔struts锛屼綘涓嶇敤鍒涘缓Action锛宻ping鏉ュ府浣犲垱寤篈ction -->
    <constant name="struts.objectFactory" value="spring"></constant>
    
    <package name="spring" namespace="/" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}">
            <result name="login">/login.jsp</result>
            <result name="toIndex" type="redirect">/index.html</result>
        </action>
    </package>
</struts>
struts.xml

 

 

 

以上是关于JavaWeb_(Spring妗嗘灦)Spring鏁村悎Hibernate的主要内容,如果未能解决你的问题,请参考以下文章

鎵嬫捀Spring妗嗘灦锛岃璁′笌瀹炵幇璧勬簮鍔犺浇鍣紝浠嶴pring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄

寮€婧愬伐鍏?| hbase-sdk ORM妗嗘灦

MyBatis妗嗘灦鍏ラ棬

VINS_Fusion妗嗘灦

Mybatis妗嗘灦 鍏ラ棬瀛︿範

鍧楄澶囬┍鍔ㄧ▼搴忕殑妗嗘灦