添加需要的依赖包(里面有些依赖项目其实并不需要,我是因为省事直接复制其他的项目过来的):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc
</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mybaties-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
添加SpringMVC
点击右上角
然后选中原来的spring删除
点击左键项目添加
然后系统就自动增加了两个配置文件
添加dispa-servlet配置文件信息:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--此文件负责整个mvc中的配置-->
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<!--静态资源映射-->
<!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
<mvc:default-servlet-handler /> <!--这句要加上,要不然可能会访问不到静态资源,具体作用自行百度-->
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<!-- 自动扫描装配 开始包中的注解-->
<context:component-scan base-package="controller"/>
<context:component-scan base-package="dao"/>
<context:component-scan base-package="service"/>
</beans>
然后是applicationContext信息作为连接数据库的一个配置信息:
<?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" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.配置数据源 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource ">
<!-- 1.1.数据库驱动 -->
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"></property>
<!-- 1.2.连接数据库的url -->
<property name="url"
value="jdbc:mysql://localhost:3306/你的数据库名称?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"></property>
<!-- 1.3.连接数据库的用户名 -->
<property name="username" value="你的数据库用户名"></property>
<!-- 1.4.连接数据库的密码 -->
<property name="password" value="你数据库的密码"></property>
<!--连接池的配置-->
<property name="initialSize" value="10"/>
<property name="maxActive" value="20"/>
<property name="minIdle" value="10"/>
<property name="queryTimeout" value="5"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
然后是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_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!--welcome pages-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置中央控制器-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--将dispatcher-servlet.xml作为mvc的配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<!--<servlet>-->
<!--<servlet-name>EmpServlet</servlet-name>-->
<!--<servlet-class>com.gx.filter.EmpServlet</servlet-class>-->
<!--</servlet>-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--自定义配置 将数据库连接的配置文件加入-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/applicationContext.xml</param-value>
</context-param>
<!-- 处理乱码-->
<!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*
上代码了 代码其实挺简单的:
实体层 对应你数据库中的类型属性
package entity;
/**
* @author
*/
public class Book {
int id;
String name;
Double price;
public int getId() {
return id;
}
public void setId(int 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 getCreate_time() {
//数据库中我的类型是date_time类型,所以这里接收到后要强制转换成String显示
return (String)create_time;
}
public String getUpdate_time() {
return (String) update_time;//
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", create_time='" + create_time + '\'' +
", update_time='" + update_time + '\'' +
", info='" + info + '\'' +
'}';
}
String create_time;
String update_time;
String info;
public void setUpdate_time(String update_time) {
this.update_time = update_time;
}
public void setCreate_time(String create_time) {
this.create_time = create_time;
}
}
创建dao层接口:
package dao;
import entity.Book;
import java.util.List;
/**
* @author
*/
public interface BookDao {
/*
* 1.添加数据
* 2.查看数据,返回信息
* 3.修改
* 4.删除
*
* */
public boolean add(Book book);
public List<Book> selectAll();
public boolean update(String name,String price);
public boolean delete(int id);
public List<Book> selectByID(int id);
}
实现接口:
package dao;
import entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
@Repository
public class BookDaoImpl implements BookDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public boolean add(Book book) {
String sql = "insert into bookinfo(id,name,age) value("+book.getId()+",'"+book.getName()+"',"+book.getPrice()+")";
this.jdbcTemplate.update(sql);
return true;
}
@Override
public List<Book> selectAll() {
String sql = "select * from bookinfo";
RowMapper<Book> rowMapper = new BeanPropertyRowMapper<Book>(Book.class);
List<Book> books = this.jdbcTemplate.query(sql,rowMapper);
return books;
}
@Override
public boolean update(String name, String price) {
return false;
}
@Override
public boolean delete(int id) {
return false;
}
@Override
public List<Book> selectByID(int id) {
return null;
}
}
这里其实就是封装了一个类似数据库操作的框架,本次只要显示数据库信息就没补全增删改查功能了 各位可以自己去补全!
接下来的service服务层其实就是类似于适配器的功能一样,BookServce接口与dao层的接口完全一样 实现类如下:
package service;
import entity.Book;
import dao.BookDao;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookDao bookDao;
@Override
public boolean add(Book book) {
return false;
}
@Override
public List<Book> selectAll() {
return bookDao.selectAll();
}
@Override
public boolean update(String name, String price) {
return false;
}
@Override
public boolean delete(int id) {
return false;
}
@Override
public List<Book> selectByID(int id) {
return null;
}
}
其实就是实现层的方法里又调用dao层的数据库操作方法,在具体的企业项目里这里提供了对源码的保护.我这里就简单意思一下
控制层:
package controller;
import entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.BookServiceImpl;
import java.util.List;
@Controller
public class BookControl {
@Autowired
BookServiceImpl bookService;
@RequestMapping(value = {
"index"})
public String selectAll(Model model) {
//将后端从数据库接收到的信息用list打包一波带走发送给前端jsp页面
List<Book> books = bookService.selectAll();
model.addAttribute("books",books);
return "booklist";//booklist为jsp页面名称,读者这里不懂的自行百度
}
jsp页面用el表达式接收后端信息就可 el表达式自行百度挺简单的
上booklist.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
<head>
<title>图书列表</title>
</head>
<body>
<table class="gridtable" align="center">
<caption>图书信息表</caption>
<thead>
<tr>
<th class="center">编号</th>
<th class="center">书名</th>
<th class="center">价格</th>
<th class="center">图书信息</th>
<th class="center">创建时间</th>
<th class="center">修改时间</th>
</tr>
</thead>
<tbody>
<!-- 开始循环 -->
<c:forEach items="${books}" var="booklist" varStatus="vs">
<tr>
<td class='center'>${
booklist.id}</td>
<td class='center'>${
booklist.name}</td>
<td class='center'>${
booklist.price}</td>
<td class="center">${
booklist.info}</td>
<td class='center'>${
booklist.create_time}</td>
<td class="center">${
booklist.update_time}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>