Examples of spring and hibernate integration operations
This article describes the integration of spring and Hibernate with examples. Share with you for your reference, as follows:
A web configuration
<?xml version="1.0" encoding="GBK"?> <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" version="3.1"> <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>
II. ApplicationContext xml
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.MysqL.jdbc.Driver" p:jdbcUrl="jdbc:MysqL://localhost/spring" p:user="root" p:password="32147" p:maxPoolSize="40" p:minPoolSize="2" p:initialPoolSize="2" p:maxIdleTime="30"/> <!-- 定义Hibernate的SessionFactory,SessionFactory需要依赖数据源,注入dataSource --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionfactorybean" p:dataSource-ref="dataSource"> <!-- mappingResources用来列出全部映射文件 --> <property name="annotatedClasses"> <list> <!-- 以下用来列出所有的PO类--> <value>org.crazyit.booksys.domain.Book</value> </list> </property> <!-- 定义Hibernate SessionFactory的属性 --> <property name="hibernateProperties"> <props> <!-- 指定Hibernate的连接方言 --> <prop key="hibernate.dialect"> org.hibernate.dialect.MysqL5InnoDBDialect</prop> <!--是否根据Hiberante映射创建数据表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 定义Service组件,并将DAO组件注入Service组件 --> <bean id="bookService" class="org.crazyit.booksys.service.impl.BookServiceImpl" p:bookDao-ref="bookDao"/> <!-- 定义DAO组件,并将SessionFactory注入DAO组件 --> <bean id="bookDao" class="org.crazyit.booksys.dao.impl.BookDaoHibernate4" p:sessionFactory-ref="sessionFactory"/> <!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 --> <!-- 该类是PlatformTransactionManager接口针对采用Hibernate的特定实现类 --> <!-- 配置HibernateTransactionManager需依赖注入SessionFactory --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/> <!-- 配置事务增强处理Bean,指定事务管理器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 用于配置详细的事务定义 --> <tx:attributes> <!-- 所有以'get'开头的方法是read-only的 --> <tx:method name="get*" read-only="true"/> <!-- 其他方法使用默认的事务设置,指定超时时长为5秒 --> <tx:method name="*" isolation="DEFAULT" propagation="required" timeout="5"/> </tx:attributes> </tx:advice> <!-- AOP配置的元素 --> <aop:config> <!-- 配置一个切入点 --> <aop:pointcut id="mypointcut" expression="bean(bookService)"/> <!-- 指定在mypointcut切入点应用txAdvice事务增强处理 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/> </aop:config> </beans>
III. struts configuration
<?xml version="1.0" encoding="GBK"?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置了系列常量 --> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true"/> <package name="lee" extends="struts-default"> <action name="addBook" class="org.crazyit.booksys.action.BookAction" method="add"> <!-- 添加图书成功,列出所有图书 --> <result type="chain">listBooks</result> <!-- 添加图书失败,跳转到添加图书的表单页 --> <result name="error">/WEB-INF/content/bookForm.jsp</result> </action> <action name="listBooks" class="org.crazyit.booksys.action.BookAction" method="list"> <result>/WEB-INF/content/listBooks.jsp</result> </action> <action name="deleteBook" class="org.crazyit.booksys.action.BookAction" method="delete"> <result type="chain">listBooks</result> </action> <!-- 让用户直接访问该应用时列出所有视图页面 --> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
Four views
1 bookForm. jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>添加图书</title> </head> <body> <h3>添加图书</h3> <s:form action="addBook"> <s:textfield name="book.name" label="书名"/> <s:textfield name="book.price" label="价格"/> <s:textfield name="book.author" label="作者"/> <tr align="center"> <td colspan="2"> <s:submit value="添加" theme="simple"/> <s:reset value="重设" theme="simple"/> </td> </tr> </s:form> </body> </html>
2 listBooks. jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>全部图书</title> </head> <body> <h3>全部图书</h3> <table width="640" border="1"> <s:iterator value="books" var="b"> <tr> <td><s:property value="name"/></td> <td><s:property value="price"/></td> <td><s:property value="author"/></td> <td><a href="${pageContext.request.contextPath}/deleteBook?id=${b.id}" rel="external nofollow" >删除</a></td> </tr> </s:iterator> </table> </body> </html>
V. domain
package org.crazyit.booksys.domain; import javax.persistence.*; @Entity @Table(name="book_inf") public class Book { @Id @Column(name="book_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(name="book_name") private String name; private double price; private String author; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
Six actions
package org.crazyit.booksys.action; import java.util.List; import org.crazyit.booksys.domain.Book; import org.crazyit.booksys.service.BookService; import com.opensymphony.xwork2.ActionSupport; public class BookAction extends ActionSupport { private BookService bookService; // 依赖注入BookService组件必须的setter方法。 // 该方法的方法名要与BookService的配置id对应 public void setBookService(BookService bookService) { this.bookService = bookService; } private Book book; private List<Book> books; private int id; public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public int getId() { return id; } public void setId(int id) { this.id = id; } // 处理添加图书的add()方法 public String add() { // 调用业务逻辑组件的addBook()方法来处理用户请求 int result = bookService.addBook(book); if(result > 0) { addActionMessage("恭喜您,图书添加成功!"); return SUCCESS; } addActionError("图书添加失败,请重新输入!"); return ERROR; } public String list() { setBooks(bookService.getAllBooks()); return SUCCESS; } public String delete() { bookService.deleteBook(id); return SUCCESS; } }
VII. Service
1 BookService. java
package org.crazyit.booksys.service; import java.util.List; import org.crazyit.booksys.domain.Book; public interface BookService { // 添加图书 int addBook(Book book); List<Book> getAllBooks(); void deleteBook(int id); }
2 BookServiceImpl. java
package org.crazyit.booksys.service.impl; import java.util.List; import org.crazyit.booksys.dao.BookDao; import org.crazyit.booksys.domain.Book; import org.crazyit.booksys.service.BookService; public class BookServiceImpl implements BookService { private BookDao bookDao; public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } @Override public int addBook(Book book) { return (Integer) bookDao.save(book); } @Override public List<Book> getAllBooks() { return bookDao.findAll(Book.class); } @Override public void deleteBook(int id) { bookDao.delete(Book.class,id); } }
Eight Dao
BaseDao. java
package org.crazyit.common.dao; import java.util.List; import java.io.Serializable; public interface BaseDao<T> { // 根据ID加载实体 T get(Class<T> entityClazz,Serializable id); // 保存实体 Serializable save(T entity); // 更新实体 void update(T entity); // 删除实体 void delete(T entity); // 根据ID删除实体 void delete(Class<T> entityClazz,Serializable id); // 获取所有实体 List<T> findAll(Class<T> entityClazz); // 获取实体总数 long findCount(Class<T> entityClazz); }
BaseDaoHibernate3. java
package org.crazyit.common.dao.impl; import java.io.Serializable; import java.util.List; import org.crazyit.common.dao.BaseDao; import org.hibernate.*; import org.springframework.orm.hibernate4.support.HibernateDaoSupport; import org.springframework.orm.hibernate4.HibernateCallback; public class BaseDaoHibernate3<T> extends HibernateDaoSupport implements BaseDao<T> { // 根据ID加载实体 public T get(Class<T> entityClazz,Serializable id) { return getHibernateTemplate().get(entityClazz,id); } // 保存实体 public Serializable save(T entity) { return getHibernateTemplate().save(entity); } // 更新实体 public void update(T entity) { getHibernateTemplate().saveOrUpdate(entity); } // 删除实体 public void delete(T entity) { getHibernateTemplate().delete(entity); } // 根据ID删除实体 public void delete(Class<T> entityClazz,Serializable id) { delete(get(entityClazz,id)); } @Override @SuppressWarnings("unchecked") public List<T> findAll(Class<T> entityClazz) { return (List<T>)getHibernateTemplate().find("select en from " + entityClazz.getSimpleName() + " en"); } @Override @SuppressWarnings("unchecked") public long findCount(Class<T> entityClazz) { List<Long> list = (List<Long>)getHibernateTemplate().find( "select count(*) from " + entityClazz.getSimpleName() + " en"); return list.get(0); } /** * 使用hql 语句进行分页查询操作 * @param hql 需要查询的hql语句 * @param pageNo 查询第pageNo页的记录 * @param pageSize 每页需要显示的记录数 * @return 当前页的所有记录 */ @SuppressWarnings("unchecked") protected List<T> findByPage(final String hql,final int pageNo,final int pageSize) { // 通过一个HibernateCallback对象来执行查询 List<T> list = getHibernateTemplate() .execute(new HibernateCallback<List<T>>() { // 实现HibernateCallback接口必须实现的方法 public List<T> doInHibernate(Session session) { // 执行Hibernate分页查询 List<T> result = session.createQuery(hql) .setFirstResult((pageNo - 1) * pageSize) .setMaxResults(pageSize) .list(); return result; } }); return list; } /** * 使用hql 语句进行分页查询操作 * @param hql 需要查询的hql语句 * @param pageNo 查询第pageNo页的记录 * @param pageSize 每页需要显示的记录数 * @param params 如果hql带占位符参数,params用于传入占位符参数 * @return 当前页的所有记录 */ @SuppressWarnings("unchecked") protected List<T> findByPage(final String hql,final int pageSize,final Object... params) { // 通过一个HibernateCallback对象来执行查询 List<T> list = getHibernateTemplate() .execute(new HibernateCallback<List<T>>() { // 实现HibernateCallback接口必须实现的方法 public List<T> doInHibernate(Session session) { // 执行Hibernate分页查询 Query query = session.createQuery(hql); // 为包含占位符的HQL语句设置参数 for(int i = 0,len = params.length ; i < len ; i++) { query.setParameter(i + "",params[i]); } List<T> result = query.setFirstResult((pageNo - 1) * pageSize) .setMaxResults(pageSize) .list(); return result; } }); return list; } }
BaseDaoHibernate4. java
package org.crazyit.common.dao.impl; import org.hibernate.*; import java.util.List; import java.io.Serializable; import org.crazyit.common.dao.*; public class BaseDaoHibernate4<T> implements BaseDao<T> { // DAO组件进行持久化操作底层依赖的SessionFactory组件 private SessionFactory sessionFactory; // 依赖注入SessionFactory所需的setter方法 public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return this.sessionFactory; } // 根据ID加载实体 @SuppressWarnings("unchecked") public T get(Class<T> entityClazz,Serializable id) { return (T)getSessionFactory().getCurrentSession() .get(entityClazz,id); } // 保存实体 public Serializable save(T entity) { return getSessionFactory().getCurrentSession() .save(entity); } // 更新实体 public void update(T entity) { getSessionFactory().getCurrentSession().saveOrUpdate(entity); } // 删除实体 public void delete(T entity) { getSessionFactory().getCurrentSession().delete(entity); } // 根据ID删除实体 public void delete(Class<T> entityClazz,Serializable id) { getSessionFactory().getCurrentSession() .createQuery("delete " + entityClazz.getSimpleName() + " en where en.id = ?0") .setParameter("0",id) .executeUpdate(); } // 获取所有实体 public List<T> findAll(Class<T> entityClazz) { return find("select en from " + entityClazz.getSimpleName() + " en"); } // 获取实体总数 public long findCount(Class<T> entityClazz) { List<?> l = find("select count(*) from " + entityClazz.getSimpleName()); // 返回查询得到的实体总数 if (l != null && l.size() == 1 ) { return (Long)l.get(0); } return 0; } // 根据HQL语句查询实体 @SuppressWarnings("unchecked") protected List<T> find(String hql) { return (List<T>)getSessionFactory().getCurrentSession() .createQuery(hql) .list(); } // 根据带占位符参数HQL语句查询实体 @SuppressWarnings("unchecked") protected List<T> find(String hql,Object... params) { // 创建查询 Query query = getSessionFactory().getCurrentSession() .createQuery(hql); // 为包含占位符的HQL语句设置参数 for(int i = 0,len = params.length ; i < len ; i++) { query.setParameter(i + "",params[i]); } return (List<T>)query.list(); } /** * 使用hql 语句进行分页查询操作 * @param hql 需要查询的hql语句 * @param pageNo 查询第pageNo页的记录 * @param pageSize 每页需要显示的记录数 * @return 当前页的所有记录 */ @SuppressWarnings("unchecked") protected List<T> findByPage(String hql,int pageNo,int pageSize) { // 创建查询 return getSessionFactory().getCurrentSession() .createQuery(hql) // 执行分页 .setFirstResult((pageNo - 1) * pageSize) .setMaxResults(pageSize) .list(); } /** * 使用hql 语句进行分页查询操作 * @param hql 需要查询的hql语句 * @param params 如果hql带占位符参数,params用于传入占位符参数 * @param pageNo 查询第pageNo页的记录 * @param pageSize 每页需要显示的记录数 * @return 当前页的所有记录 */ @SuppressWarnings("unchecked") protected List<T> findByPage(String hql,int pageSize,params[i]); } // 执行分页,并返回查询结果 return query.setFirstResult((pageNo - 1) * pageSize) .setMaxResults(pageSize) .list(); } }
BookDao. java
package org.crazyit.booksys.dao; import java.util.List; import org.crazyit.booksys.domain.Book; import org.crazyit.common.dao.BaseDao; public interface BookDao extends BaseDao<Book> { }
BookDaoHibernate4. java
package org.crazyit.booksys.dao.impl; import java.util.List; import org.crazyit.booksys.dao.BookDao; import org.crazyit.booksys.domain.Book; import org.crazyit.common.dao.impl.BaseDaoHibernate3; import org.crazyit.common.dao.impl.BaseDaoHibernate4; public class BookDaoHibernate4 extends BaseDaoHibernate4<Book> implements BookDao { }
Nine tests
Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills
I hope this article will be helpful to you in Java programming.