事前准备
新建一个项目,如果你是刚换了工作区间的话需要在新建项目之间进行以下配置
进行以上操作没有问题之后再开始创建项目
然后进行建项目之后的配置
具体更详细的步骤可以去参考我之前发的博客
地址
我这里演示用的是一个书籍表
功能实现
1、首先导入我所需要的jar包,由于我是在maven的基础上写的,所以在pom.xml里写上我所需要的jar包的配置语句就好啦
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.solar</groupId>
<artifactId>maven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<servlet.version>4.0.1</servlet.version>
<junit.version>3.8.1</junit.version>
<mysql.version>5.1.44</mysql.version>
<struts.version>2.5.16</struts.version>
<jstl.version>1.2</jstl.version>
<standard.version>1.1.2</standard.version>
<tomcat.version>8.5.38</tomcat.version>
</properties>
<dependencies>
<!-- tomcat-jsp-api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<!-- standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- struts -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>provided</scope>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<build>
<finalName>maven</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、实体类(根据数据库中的表结构)
package com.wangjuanxia.entity;
import java.io.Serializable;
/**
* 数据库t_struts_book书本实体类
* @author solarW
* 2020年7月23日08:58:24
*/
public class BookEntity implements Serializable{
private Integer bookId;
private String bookName;
private Float bookPrice;
private String bookType;
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Float getBookPrice() {
return bookPrice;
}
public void setBookPrice(Float bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookType() {
return bookType;
}
public void setBookType(String bookType) {
this.bookType = bookType;
}
public BookEntity() {
}
public BookEntity(Integer bookId, String bookName, Float bookPrice, String bookType) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookPrice = bookPrice;
this.bookType = bookType;
}
@Override
public String toString() {
return "BookEntity [bookId=" + bookId + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", bookType="
+ bookType + "]";
}
}
3、dao方法
首先写一个BaseDao通用方法类
package com.wangjuanxia.dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.AttributedCharacterIterator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.wangjuanxia.util.DBHelper;
import com.wangjuanxia.util.PageBean;
/**
* dao类的通用方法
* @author solarW
*2020年7月23日11:22:55
* @param <T> 泛型
*/
public class BaseDao<T> {
public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException, ParseException{
// System.out.println("BaseDao");
List<T> list=new ArrayList<T>();
Connection con=DBHelper.getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
if(pageBean!=null&&pageBean.isPagination()) {
String countSql=getCountSql(sql);
ps=con.prepareStatement(countSql);
rs=ps.executeQuery();
if(rs.next()) {
pageBean.setTotal(rs.getObject(1).toString());
}
String pageSql=getPageSql(sql,pageBean);
ps=con.prepareStatement(pageSql);
rs=ps.executeQuery();
}else {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
}
while(rs.next()) {
T t=(T) clz.newInstance();
for (Field f: clz.getDeclaredFields()) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
DBHelper.close(con, ps, rs);
return list;
}
private String getPageSql(String sql, PageBean pageBean) {
// TODO Auto-generated method stub
return sql+" limit "+pageBean.getStartIndex()+","+pageBean.getRows()+"";
}
private String getCountSql(String sql) {
// TODO Auto-generated method stub
return "select count(1) from ("+sql+") t";
}
public int executeUpdate(String sql,T t,String[] attrs) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Connection con = DBHelper.getConnection();
PreparedStatement ps=con.prepareStatement(sql);
int loop=1;
Field f=null;
for (String attr : attrs) {
f = t.getClass().getDeclaredField(attr);
f.setAccessible(true);
ps.setObject(loop++, f.get(t));
}
int code = ps.executeUpdate();
DBHelper.close(con, ps, null);
return code;
}
}
然后是书本的dao方法
package com.wangjuanxia.dao;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import com.wangjuanxia.entity.BookEntity;
import com.wangjuanxia.util.PageBean;
import com.wangjuanxia.util.StringUtils;
/**
* 书本的操作方法
* @author solarW
*2020年7月23日11:22:00
*/
public class BookDao extends BaseDao<BookEntity>{
/**
* 查询数据库中t_struts_book中的所有数据
*
* @param book
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
* @throws ParseException
*/
public List<BookEntity> query(BookEntity book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException, ParseException{
String name=book.getBookName();
String sql="select bookId,bookName,bookPrice,bookType from t_struts_book where true";
if(StringUtils.isNotBlank(name)) {
sql+=" bookName like '%"+name+"%'";
}
sql+=" order by bookId desc";
return super.executeQuery(sql, BookEntity.class, pageBean);
}
/**
* 增加数据
* @param book
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int addBook(BookEntity book) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="insert into t_struts_book(bookName,bookPrice,bookType) values(?,?,?)";
return super.executeUpdate(sql, book, new String[] {"bookName","bookPrice","bookType"});
}
/**
* 修改数据
* @param book
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int editBook(BookEntity book) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="update t_struts_book set bookName=?,bookPrice=?,bookType=? where bookId=?";
return super.executeUpdate(sql, book, new String[] {"bookName","bookPrice","bookType","bookId"});
}
/**
* 删除数据
* @param book
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int delBook(BookEntity book) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="delete from t_struts_book where bookId=?";
return super.executeUpdate(sql, book, new String[] {"bookId"});
}
}
配置book的strutsxml文件,继承struts-base.xml
<?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>
<package name="struts-book" extends="struts-base" >
<action name="bookAction_*" method="{1}" class="com.wangjuanxia.action.BookAction">
<result name="success" type=""></result>
</action>
</package>
</struts>
然后将它添加到总配置文件里
<?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>
<include file="struts-book.xml"></include>
</struts>
处理书本的业务逻辑类
package com.wangjuanxia.action;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ModelDriven;
import com.wangjuanxia.dao.BookDao;
import com.wangjuanxia.entity.BookEntity;
import com.wangjuanxia.util.PageBean;
/**
* 书本类的处理业务逻辑类
* @author solarW
* 2020年7月23日11:30:50
*/
public class BookAction extends BaseAction implements ModelDriven<BookEntity>{
private BookEntity book=new BookEntity();
private BookDao bd=new BookDao();
PageBean pageBean=null;
public String list() {
try {
pageBean=new PageBean();
pageBean.setRequest(request);
List<BookEntity> list= this.bd.query(book, pageBean);
request.setAttribute("list", list);
request.setAttribute("pageBean", pageBean);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String add() {
try {
this.bd.addBook(book);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public BookEntity getModel() {
// TODO Auto-generated method stub
return book;
}
}
然后最后测试一下
package com.wangjuanxia.action;
import static org.junit.Assert.*;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.wangjuanxia.dao.BookDao;
import com.wangjuanxia.entity.BookEntity;
public class BookActionTest {
BookDao bd=new BookDao();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testList() throws InstantiationException, IllegalAccessException, SQLException, ParseException {
BookEntity book=new BookEntity();
List<BookEntity> list = bd.query(book, null);
list.forEach(b->{
System.out.println(b);
});
}
@Test
public void testAdd() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
for (int i = 1; i < 31; i++) {
BookEntity book=new BookEntity(i, i+"季度", 40f, "文艺");
bd.addBook(book);
}
}
}