1. 程式人生 > >Spring+Hibernate整合開發配置,完整例項

Spring+Hibernate整合開發配置,完整例項

目錄結構

這裡寫圖片描述

Jar包

這裡寫圖片描述

配置

一、beans.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
>
<!-- 匯入Src目錄下的jdbc.properties檔案 --> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!-- 開啟Spring的Annotation支援 --> <context:annotation-config/> <!-- 設定Spring去那些包中找Annotation --> <!-- <context:component-scan base-package="com.wen"/> -->
<context:component-scan base-package="com.wen" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!-- 配置資料來源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 配置連線池的初始值 --> </bean> <!-- 建立Spring的SessionFactory工廠 --> <!-- 如果使用的是Annotation的方式,不能使用LocalSessionFactory,而應該使用AnnotationSessionFactoryBean --> <!-- 但好像無法使用了 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 注入資料來源 --> <property name="dataSource" ref="dataSource"/> <!-- 通過掃描包方式設定Spring取哪個包查詢相應的實體類 --> <property name="packagesToScan"> <value>com.wen.entity</value> </property> <!-- 或者通過註解方式 <property name="annotatedClasses"> <list> <value>com.wen.entity.User</value> <value>com.wen.entity.Repairs</value> </list> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 開啟HibernateTemplate,並且為其注入SessionFactory 使用HibernateTemplate不太方便的就是要獲取session得通過getSessionFactory()方法獲取 注意:這裡之所以配置這個是因為我的UserDao繼承了HibernateDaoSupport --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置Spring的事務處理 --> <!-- 建立事務管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置AOP,Spring是通過AOP來進行事務管理的 --> <aop:config> <!-- 設定pointcut,表示哪些方法要加入事務處理 --> <aop:pointcut expression="execution(* com.wen.service.*.*(..))" id="allMethod"/> <!-- 通過advisor來確定具體要加入事務控制的方法 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/> </aop:config> <!-- 配置哪些方法要加入事務控制 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 讓所有的方法都加入事務管理,為了提高效率,可以把一些查詢之類的方法設定為只讀事務 --> <tx:method name="*" propagation="REQUIRED" read-only="true"/> <!-- 以下方法都是可能涉及寫改的方法,就無法設定為只讀 --> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>

二、user-servlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <!-- 註解驅動 -->
    <mvc:annotation-driven/>
    <!-- 掃描包 -->
    <context:component-scan base-package="com.wen"/>
    <!-- 配置檢視解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

2016-9-22 09:41:37 補充 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name></display-name> 

  <!-- 建立Spring的監聽器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- Spring的監聽器可以通過這個上下文引數來獲取bean.xml的位置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/beans.xml</param-value>
  </context-param>

  <servlet>
    <servlet-name>user</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/user-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>user</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- Spring中提供了這個類來實現OpenSessionInViewer的操作 -->
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- SystemContextFilter,我們自己定義的 -->
  <filter>
    <filter-name>SystemContextFilter</filter-name>
    <filter-class>com.wen.web.SystemContextFilter</filter-class>
    <!-- <init-param>
        <param-name>pageSize</param-name>
        <param-value>15</param-value>
    </init-param> -->
  </filter>
  <filter-mapping>
    <filter-name>SystemContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- sitemesh過濾器 -->
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

三、jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_user
jdbc.username=root
jdbc.password=root

com.wen.entity實體類

1、Pager.java

package com.wen.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="t_user")
public class User {
    @Id
    @GeneratedValue
    private int id;
    private String username;
    private String password;
    private String email;
    private String nickname;


    public int getId() {
        return id;
    }
    public void setId(int 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;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }


}

2、Pager.java

package com.wen.entity;

import java.util.List;

public class Pager<T> {
    private List<T> datas;
    //初始頁
    private int offset;
    //每頁記錄數
    private int size;
    private int totalSize;
    private long total;

    public List<T> getDatas() {
        return datas;
    }
    public void setDatas(List<T> datas) {
        this.datas = datas;
    }
    public int getOffset() {
        return offset;
    }
    public void setOffset(int offset) {
        this.offset = offset;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getTotalSize() {
        return totalSize;
    }
    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }
    public long getTotal() {
        return total;
    }
    public void setTotal(long total) {
        this.total = total;
    }
}

3、SystemContext.java

package com.wen.entity;

public class SystemContext {
    private static ThreadLocal<Integer> offset = new ThreadLocal<Integer>();
    private static ThreadLocal<Integer> size = new ThreadLocal<Integer>();

    public static Integer getOffset() {
        return offset.get();
    }
    public static void setOffset(Integer _offset) {
        offset.set(_offset);
    }
    public static Integer getSize() {
        return size.get();
    }
    public static void setSize(Integer _size) {
        size.set(_size);
    }

    public static void removeSize() {
        size.remove();
    }

    public static void removeOffset() {
        offset.remove();
    }
}

4、UserException.java

package com.wen.entity;

public class UserException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public UserException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public UserException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
}

com.wen.dao 資料庫介面

1、IUserDao.java

package com.wen.dao;

import java.util.List;

import com.wen.entity.Pager;
import com.wen.entity.User;

public interface IUserDao {
    public void add(User user);

    public void update(User user);

    public void delete(int id);

    public User load(int id);

    public List<User> list();

    public Pager<User> find();

    public User loadByUsername(String username);
}

2、UserDao.java

package com.wen.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.wen.entity.Pager;
import com.wen.entity.SystemContext;
import com.wen.entity.User;

@Repository("userDao")
public class UserDao extends HibernateDaoSupport implements IUserDao {

    //SessionFactory 在包org.hibernate內
    //private SessionFactory sessionFactory;

    //為SessionFactory新增set方法,Spring才能為其注入
    //public void setSessionFactory(SessionFactory sessionFactory) {
    //    this.sessionFactory = sessionFactory;
    //}


    @Resource
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        this.setSessionFactory(sessionFactory);
    }

    @Override
    public void add(User user) {
        this.getHibernateTemplate().save(user);
    }

    @Override
    public void update(User user) {
        this.getHibernateTemplate().update(user);
    }

    @Override
    public void delete(int id) {
        User user = this.load(id);
        this.getHibernateTemplate().delete(user);
    }

    @Override
    public User load(int id) {
        return this.getHibernateTemplate().load(User.class, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> list() {

        return this.getSessionFactory().getCurrentSession().createQuery("from User").list();
    }

    @Override
    public Pager<User> find() {
        int size = SystemContext.getSize();
        int offset = SystemContext.getOffset();

        Query query = this.getSessionFactory().getCurrentSession().createQuery("from User");
        query.setFirstResult(offset).setMaxResults(size);
        @SuppressWarnings("unchecked")
        List<User> datas = query.list();
        Pager<User> us = new Pager<User>();
        us.setDatas(datas);
        us.setOffset(offset);
        us.setSize(size);
        long total = (Long) this.getSessionFactory().getCurrentSession().createQuery("select count(*) from User").uniqueResult();
        us.setTotal(total);
        return us;
    }

    @Override
    public User loadByUsername(String username) {

        return (User) this.getSessionFactory().getCurrentSession()
                .createQuery("from User where username=?")
                .setParameter(0, username).uniqueResult();
    }

}

com.wen.service 業務邏輯層

1、IUserService.java

package com.wen.service;

import java.util.List;

import com.wen.entity.Pager;
import com.wen.entity.User;

public interface IUserService {
public void add(User user);

    public void update(User user);

    public void delete(int id);

    public User load(int id);

    public List<User> list();

    public Pager<User> find();

    public User login(String username, String password);
}

2、UserService.java

package com.wen.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.wen.dao.IUserDao;
import com.wen.entity.Pager;
import com.wen.entity.User;
import com.wen.entity.UserException;
@Service
public class UserService implements IUserService {


    private IUserDao userDao;

    public IUserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void add(User user) {
        User u = userDao.loadByUsername(user.getUsername());
        if(u!=null) throw new UserException("要新增的使用者已經存在");
        userDao.add(user);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

    @Override
    public User load(int id) {
        return userDao.load(id);
    }

    @Override
    public List<User> list() {
        return userDao.list();
    }

    @Override
    public Pager<User> find() {
        return userDao.find();
    }

    @Override
    public User login(String username, String password) {
        User u = userDao.loadByUsername(username);
        if(u==null) throw new UserException("登入使用者不存在");
        if(!u.getPassword().equals(password)) throw new UserException("使用者密碼不正確");
        return u;
    }

}

com.wen.web 控制層

1、SystemContextFilter.java

package com.wen.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.wen.entity.SystemContext;

public class SystemContextFilter implements Filter {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        int offset = 0;
        try{
            offset = Integer.parseInt(req.getParameter("pager.offset"));
        } catch(NumberFormatException e) {
            //e.printStackTrace();
        }

        try{
            SystemContext.setOffset(offset);
            SystemContext.setSize(15);
            chain.doFilter(req, resp);
        } finally {
            SystemContext.removeOffset();
            SystemContext.removeSize();
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }

}

2、UserController.java

package com.wen.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping ("/user")
public class UserController {
    @RequestMapping(value="/users", method=RequestMethod.GET)
    public String list(){
        return "user/list";
    }
}

關於spring配置的SessionFactory解釋及其使用方法

1、新增類BaseSessionFactoryImpl用於獲取Session

類BaseSessionFactoryImpl的內容如下:

public class BaseSessionFactoryImpl {
    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory=null;
    public Session getSession(){
        return sessionFactory.getCurrentSession();
    }
}

2、在Dao層獲取Session,

public class UserDaoImplextends BaseSessionFactoryImpl
{
    public User queryByUsername(String userName) {    
        User user=null;
        String sql="select user from User user where user.userName="+userName;
        try {
            user=(User) getSession().get(User.class, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }
}

3、重點注意事項

SessionFactory的getCurrentSession並不能保證在沒有當前Session的情況下會自動建立一個新的,這取決於CurrentSessionContext的實現,SessionFactory將呼叫CurrentSessionContext的currentSession()方法來獲得Session。

在Spring中,如果我們在沒有配置TransactionManager並且沒有事先呼叫SessionFactory.openSession()的情況直接呼叫getCurrentSession(),那麼程式將丟擲“No
Session found for current thread”異常。

如果配置了TranactionManager並且通過@Transactional或者宣告的方式配置的事務邊界,那麼Spring會在開始事務之前通過AOP的方式為當前執行緒建立Session,此時呼叫getCurrentSession()將得到正確結果。

然而,產生以上異常的原因在於Spring提供了自己的CurrentSessionContext實現,如果我們不打算使用Spring,而是自己直接從hibernate.cfg.xml建立SessionFactory,並且為在hibernate.cfg.xml
中設定current_session_context_class為thread,也即使用了ThreadLocalSessionContext,那麼我們在呼叫getCurrentSession()時,如果當前執行緒沒有Session存在,則會建立一個繫結到當前執行緒。

Hibernate在預設情況下會使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我們不用配置current_session_context_class,當Hibernate與Spring整合時,將使用該SessionContext,故此時呼叫getCurrentSession()的效果完全依賴於SpringSessionContext的實現。

在沒有Spring的情況下使用Hibernate,如果沒有在hibernate.cfg.xml中配置current_session_context_class,有沒有JTA的話,那麼程式將丟擲”No
CurrentSessionContext
configured!”異常。此時的解決辦法是在hibernate.cfg.xml中將current_session_context_class配置成thread。

在Spring中使用Hibernate,如果我們配置了TransactionManager,那麼我們就不應該呼叫SessionFactory的openSession()來獲得Sessioin,因為這樣獲得的Session並沒有被事務管理。

至於解決的辦法,可以採用如下方式:

1. 在spring 配置檔案中加入

< tx:annotation-driven transaction-manager=”transactionManager”/>
並且在處理業務邏輯的類上採用註解

@Service
public class CustomerServiceImpl implements CustomerService { 
    @Transactional
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }
...
}

另外在 hibernate 的配置檔案中,也可以增加這樣的配置來避免這個錯誤:

< property name=”current_session_context_class”>thread< /property>

關於註解的一些疑問

1、在引用實現類的使用時為什麼使用的是實現類的介面而不是實現的類

參考這裡