Web工程配合Spring从数据库查询信息到前端显示案例(初中级)
改造项目基于:
Web工程配合Spring从数据库查询信息到前端显示案例(初级)
目录结构:
比上一个项目多导入一个jar包:spring-aop-4.3.28.RELEASE
下载地址:
链接:https://pan.baidu.com/s/1tWjDkYL5OWJRH2e6lKIX3g
提取码:nisz
1.Dao层改造
@Repository //变化1
public class BookDaoImpl implements BookDao {
@Autowired //变化2
private QueryRunner queryRunner;
// 变化3 不需要构造方法了
// public BookDaoImpl(QueryRunner queryRunner) {
// this.queryRunner = queryRunner;
// }
@Override
public List<Book> queryAll() {
List<Book> books = null;
try {
books = queryRunner.query(" select id,bookname bookName,author from book ", new BeanListHandler<Book>(Book.class));
return books;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
2.Service层改造
@Service //变化1
public class BookServiceImpl implements BookService {
@Autowired //变化2
private BookDao bookDao;
// 变化3 不需要构造方法了
// public void setBookDao(BookDao bookDao) {
// this.bookDao = bookDao;
// }
@Override
public List<Book> queryAll() {
return bookDao.queryAll();
}
}
3. Spring 配置
- 删除spring-service.xml
- spring-crud.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--打开扫描-->
<context:component-scan base-package="com.example"></context:component-scan>
<!--删除service配置-->
<import resource="spring-dao.xml"></import>
</beans>
- spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:com/example/db-info.properties"></context:property-placeholder>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean name="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!--删除dao配置-->
</beans>
4. servlet改造
public class BookServlet extends BasicServlet {
private BookService bookService;
//重写init()方法,并获取Spring容器中存在的唯一BookService类
@Override
public void init() throws ServletException {
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
this.bookService = context.getBean(BookService.class);
}
protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Book> books = bookService.queryAll();
req.setAttribute("books",books);
req.getRequestDispatcher("/WEB-INF/book-show.jsp").forward(req,resp);
}
}
5.其余文件代码
数据库表格
Book
public class Book {
private Integer id;
private String bookName;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
db-info.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///temporary?useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.password=12345
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>bookservlet</servlet-name>
<servlet-class>com.example.web.servlet.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bookservlet</servlet-name>
<url-pattern>/b</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/example/spring/spring-crud.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
book-show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>书名</th>
<th>作者</th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.bookName}</td>
<td>${book.author}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
依赖jar包
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/b?fun=show">456</a>
</body>
</html>
显示结果: