<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<welcome-file-list>
<welcome-file>/WEB-INF/content/bookForm.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?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:3306/sampledb"
p:user="root"
p:password="qaz"
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">
<!-- annotatedClasses用来列出全部持久化类 -->
<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>
<?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>
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);
}
package org.crazyit.common.dao.impl;
import org.hibernate.*;
import java.util.List;
import java.io.Serializable;
import org.crazyit.common.dao.*;
/***********************************
* 1. Spring推荐使用sessionFactory的getCurrentSession()来获取Session,
* 然后通过Session进行持久化操作。 ==> BaseDaoHibernate4.java
*
* 2. Spring并不推荐使用HibernateTemplate、HibernateDaoSupport来实现Dao组件,
* Spring4为了和以前编码风格兼容,才提供了HibernateTemplate、HibernateDaoSupport;
* ==>==> BaseDaoHibernate3.java
*
***********************************/
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 pageNo, int pageSize
, 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 query.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize)
.list();
}
}
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;
/***********************************
* 1. Spring推荐使用sessionFactory的getCurrentSession()来获取Session,
* 然后通过Session进行持久化操作。 ==> BaseDaoHibernate4.java
*
* 2. Spring并不推荐使用HibernateTemplate、HibernateDaoSupport来实现Dao组件,
* Spring4为了和以前编码风格兼容,才提供了HibernateTemplate、HibernateDaoSupport;
* ==>==> BaseDaoHibernate3.java
*
***********************************/
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 pageNo,
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;
}
}
package org.crazyit.booksys.dao;
import org.crazyit.booksys.domain.Book;
import org.crazyit.common.dao.BaseDao;
public interface BookDao extends BaseDao<Book>
{
}
package org.crazyit.booksys.dao.impl;
import org.crazyit.booksys.dao.BookDao;
import org.crazyit.booksys.domain.Book;
import org.crazyit.common.dao.impl.BaseDaoHibernate4;
/**
* Spring推荐使用继承前面的BaseDaoHibernate4,也可以继承前面的BaseDaoHibernate3
*/
public class BookDaoHibernate4 extends BaseDaoHibernate4<Book>
implements BookDao{
}
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;
}
}
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; //业务层组件
private Book book;
private List<Book> books;
private int id;
// 处理添加图书的add()方法
public String add()
{
if(book == null){
book = new Book();
book.setAuthor("zhangsan");
book.setName("zhangsan");
book.setPrice(123);
}
// 调用业务逻辑组件的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;
}
public void setBookService(BookService bookService)
{
this.bookService = bookService;
}
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;
}
}
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);
}
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);
}
}
<%@ 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>
<%@ 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}">删除</a></td>
</tr>
</s:iterator>
</table>
</body>
</html>
/*
Navicat MySQL Data Transfer
Source Server : default
Source Server Version : 50626
Source Host : localhost:3306
Source Database : sampledb
Target Server Type : MYSQL
Target Server Version : 50626
File Encoding : 65001
Date: 2015-09-08 10:36:58
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for book_inf
-- ----------------------------
DROP TABLE IF EXISTS `book_inf`;
CREATE TABLE `book_inf` (
`book_id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(255) DEFAULT NULL,
`book_name` varchar(255) DEFAULT NULL,
`price` double NOT NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of book_inf
-- ----------------------------
-- ----------------------------
-- Table structure for t_login_log
-- ----------------------------
DROP TABLE IF EXISTS `t_login_log`;
CREATE TABLE `t_login_log` (
`login_log_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`ip` varchar(23) DEFAULT NULL,
`login_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`login_log_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_login_log
-- ----------------------------
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) DEFAULT NULL COMMENT '用户名',
`credits` int(11) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`last_visit` datetime DEFAULT NULL,
`last_ip` varchar(23) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', 'admin', null, '123456', null, null);
INSERT INTO `t_user` VALUES ('2', 'zpp', null, '888888', null, null);