<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.tygroupId>
<artifactId>maven-sshartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<spring.version>5.0.0.RELEASEspring.version>
<hibernate.version>5.4.1.Finalhibernate.version>
<struts.version>2.5.20struts.version>
<mysql.version>8.0.13mysql.version>
<druid.version>1.1.10druid.version>
<jstl.version>1.2jstl.version>
<servlet-api.version>4.0.0servlet-api.version>
<jsp-api.version>2.3.3jsp-api.version>
<jackson.version>2.9.8jackson.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-coreartifactId>
<version>${struts.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-spring-pluginartifactId>
<version>${struts.version}version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-json-pluginartifactId>
<version>2.3.1.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>${jstl.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>${servlet-api.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>${jsp-api.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-jpamodelgenartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-coreartifactId>
<version>${struts.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-spring-pluginartifactId>
<version>${struts.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-convention-pluginartifactId>
<version>${struts.version}version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>80port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
<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">
<context:component-scan base-package="com.ty.*"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/20200217?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialectprop>
props>
property>
<property name="mappingLocations" value="classpath:com/ty/entity/*.hbm.xml">property>
bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ty.service..*.*(..))"/>
aop:config>
beans>
<struts>
<package name="myStruts" extends="struts-default">
<action name="findAll" class="com.ty.action.BookAction" method="findAll">
<result type="redirect">/bookManager/book-list.jspresult>
action>
<action name="findById" class="com.ty.action.BookAction" method="findById">
<result type="redirect">/bookManager/update-book.jspresult>
action>
<action name="findByName" class="com.ty.action.BookAction" method="findByName">
<result type="redirect">/bookManager/name-list.jspresult>
action>
<action name="addBook" class="com.ty.action.BookAction" method="addBook">
<result type="redirectAction">findAllresult>
action>
<action name="updateBook" class="com.ty.action.BookAction" method="updateBook">
<result type="redirectAction">findAllresult>
action>
<action name="deleteBook" class="com.ty.action.BookAction" method="deleteBook">
<result type="redirectAction">findAllresult>
action>
package>
struts>
<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.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<filter>
<filter-name>strutsfilter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>strutsfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>encodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
package com.ty.entity;
public class Book {
private Integer id;
private String name;
private String author;
private String publish;
private Integer pages;
private Double price;
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 String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
映射文件Book.hbm.xml
<hibernate-mapping>
<class name="com.ty.entity.Book">
<id name="id" column="id">
<generator class="native">generator>
id>
<property name="name" column="name"/>
<property name="author" column="author"/>
<property name="publish" column="publish"/>
<property name="pages" column="pages"/>
<property name="price" column="price"/>
class>
hibernate-mapping>
package com.ty.dao;
import com.ty.entity.Book;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.List;
@Repository
public class BookDao {
@Autowired
private HibernateTemplate hibernateTemplate;
public void addBook(Book book){
hibernateTemplate.save(book);
}
public void deleteBook(int id){
Book book = hibernateTemplate.get(Book.class, id);
hibernateTemplate.delete(book);
}
public void updateBook(Book book){
hibernateTemplate.update(book);
}
public Book findByName(String name){
List<Book> books = (List<Book>) hibernateTemplate.find("from Book b where b.name like ?0 ",name);
if(books.size()>0) {
return (books.get(0));
}
return null;
}
public Book findById(int id){
return (Book) hibernateTemplate.get(Book.class,id);
}
/**
* 分页查询
* @param page 第几页
* @param size 一页有几条数据
* @return
*/
public List<Book> findAll(String page,int size){
return hibernateTemplate.execute(new HibernateCallback<List<Book>>() {
public List<Book> doInHibernate(Session session) throws HibernateException{
String q = "from Book";
Query query = session.createQuery(q);
query.setFirstResult((Integer.valueOf(page)-1)*size);
query.setMaxResults(size);
List list = query.list();
return list;
}
});
}
/**
* 获取总页数
* @param size
* @return
*/
public int getCount(int size){
List<Book> books = (List<Book>) hibernateTemplate.find(" from Book ");
//将总记录数除以每页的记录数向上取整
return (int) Math.ceil(1.0*books.size()/size);
}
}
package com.ty.service;
import com.ty.dao.BookDao;
import com.ty.entity.Book;
import com.ty.pageUtil.PageSize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BookService {
@Autowired
private BookDao bookDao;
public void deleteBook(int id){
bookDao.deleteBook(id);
}
public void updateBook(Book book){
bookDao.updateBook(book);
}
public void addBook(Book book){
bookDao.addBook(book);
}
public Book findById(int id){
return bookDao.findById(id);
}
public Book findByName(String name){
return bookDao.findByName(name);
}
public Map<String ,Object> findAll(String page){
//当没有页码传入时,我们默认它请求第一页
if(page==null||page.equals("")) {
page="1";
}
Map<String,Object> returnMap = new HashMap<>();
List<Book> books = bookDao.findAll(page, PageSize.SIZE);
int count = bookDao.getCount(PageSize.SIZE);//总页数
returnMap.put("count",count);
returnMap.put("book",books);
returnMap.put("page",page);
return returnMap;
}
}
package com.ty.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ty.entity.Book;
import com.ty.service.BookService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class BookAction extends ActionSupport implements ModelDriven {
private Book book = new Book();
//属性驱动获取前台页码
private String page;
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
@Autowired
private BookService bookService;
@Override
public Object getModel() {
return book;
}
public String addBook(){
bookService.addBook(book);
return SUCCESS;
}
public String deleteBook(){
bookService.deleteBook( book.getId());
return SUCCESS;
}
public String updateBook(){
bookService.updateBook(book);
return SUCCESS;
}
public String findById(){
HttpServletRequest request = ServletActionContext.getRequest();
Book book1 = bookService.findById(this.book.getId());
request.getSession().setAttribute("book",book1);
return SUCCESS;
}
public String findByName(){
Book book1 = bookService.findByName(this.book.getName());
ServletActionContext.getRequest().getSession().setAttribute("book",book1);
return SUCCESS;
}
public String findAll(){
Map<String, Object> map = bookService.findAll(this.page);
List<Book> o = (List<Book>) map.get(book);
List list = new ArrayList();
list.add(map.get(book));
ServletActionContext.getRequest().getSession().setAttribute("books",map);
return SUCCESS;
}
}
package com.ty.pageUtil;
public interface PageSize {
int SIZE=3;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
图书展示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加图书