SSM(spring,springmvc,mybatis)是目前主流的单体框架,全面替代ssh。
本人尝试往后台转型,这是第一个详细demo,以此为展开。
一、工具准备
1、InterlliJ IDEA,很多都从eclipse转过来,都是优秀的IDE工具;
2、maven安装,不多说;
3、mysql数据库安装,建议使用mysql5的版本,比较容易连接。8的版本,写法会不同。这里主要是安装mysql5.7.28版本。安装成功后,尝试用数据库工具尝试是否连接成功,否则会浪费很多时间的;
4、tomcat安装,本地配置,在IDEA工具也配置,很简单,不多说。
二、编程开始
1、利用IDEA工具,点击“file”,“new”->“project”,选择“Maven”,点击Create from archetype,在列表中,选择红框的XXXX-webapp这一项(看清楚)。
image.png然后,填写自定义的名称,不断下一步就完成了。
初始化后,类似下方的列表内容。
i初始化的文件列表2、maven的pom.xml的文件编写,感觉是整个项目的基础。
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.mvc</groupId>
<artifactId>mvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mvc Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.demoexample666.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>5.0.3.RELEASE</spring.version>
<mybatis.version>3.4.4</mybatis.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--Spring core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring DAO-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 第二部分 Servlet web-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!-- 第三部分 数据库和mybatis-->
<!-- 数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- 数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis-spring整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 第四部分 日志-->
<!--实现slf4接口并整合-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>mvc</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3、创建重要的文件夹,在主目录(这里项目名称叫mvc),右键,新建“target”文件夹;在“src”的“main”下右键,新建“java”和“resource”文件夹,然后把点击“resource”文件夹右键,选择“make direction as”,选为root文件属性,如下图:
image.png
而“java”文件夹,同样的操作,文件夹设置为“Source Root”。
4、从“resource”文件夹,开始写配置文件
1)分别创建jdbc.properties,log4j.properties,mybatis-config.xml文件
jdbc.properties文件内容,这里使用mysql5.7的版本,所以jdbc连接,而mysql8的版本,写法是不通的,切记。同时这里一定匹配自己的本地数据库名称,账号,密码。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=1234567
log4j.properties文件内容,常规写法
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis-config.xml文件内容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局属性-->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名 默认true-->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换Table{create_time} -> Entity{createTime}-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
2)创建“spring”文件夹,在其下面分别创建spring-dao.xml,spring-mvc.xml,spring-service.xml。
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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis过程-->
<!-- 1配置数据库相关参数-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10" />
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false" />
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000"/>
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.pojo"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
spring-mvc.xml文件内容,springmvc核心配置文件,拦截,提供什么显示方式
<?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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<!-- 配置spirngmvc-->
<!--1开启springmvc注解模式-->
<!-- 简化配置:
(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
-->
<mvc:annotation-driven/>
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 3配置jsp显示 viewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 4扫描web相关的bean-->
<context:component-scan base-package="com.controller" />
</beans>
spring-service.xml文件内容,配置service层信息
<?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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
>
<!--扫描service包下所有使用注解的类型-->
<context:component-scan base-package="com.service"/>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池-->
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3)创建“mapper”文件夹,在其下面创建PaperMapper.xml
PaperMapper.xml文件内容,这里是存放mybatis的SQL写法,这里提供文章列表的增删改查的方法,后续根据自己的业务情况变更。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.PaperDao">
<resultMap id="paperResultMap" type="Paper">
<id property="paperId" column="paper_id"/>
<result property="paperName" column="name"/>
<result property="paperNum" column="number"/>
<result property="paperDetail" column="detail"/>
</resultMap>
<insert id="addPaper" parameterType="Paper">
INSERT INTO paper(paper_id,name,number,detail) VALUE (#{paperId},#{paperName},#{paperNum},#{paperDetail})
</insert>
<delete id="deletePaperById" parameterType="long">
DELETE FROM paper WHERE paper_id=#{paperID}
</delete>
<update id="updatePaper" parameterType="Paper">
UPDATE paper
SET NAME = #{paperName},NUMBER = #{paperNum},detail =#{paperDetail}
WHERE paper_id = #{paperId}
</update>
<select id="queryById" resultType="Paper" parameterType="long">
SELECT paper_id,name,number,detail
FROM paper
WHERE paper_id=#{paperId}
</select>
<select id="queryAllPaper" resultMap="paperResultMap">
SELECT paper_id,name,number,detail
FROM paper
</select>
</mapper>
完成后,会出现下面的文件列表
resources.png
5、从“webapp”文件夹,开始写jsp文件和web的配置
1)index.jsp文件内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<!DOCTYPE HTML>
<html>
<head>
<title>文章首页</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
文章管理系统demo
</h1>
</div>
</div>
</div>
</div>
<br><br>
<h3>
<a href="${path }/paper/allPaper">进入文章管理页面</a>
</h3>
</body>
</html>
2)web.xml文件内容编写
<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" metadata-complete="true"
>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc需要加载的配置文件spring-dao.xml,spring-service.xml,spring-mvc.xml
Mybatis - > spring -> springmvc-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 默认匹配所有的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3)在WEB-INF下,创建jsp文件夹,在其下面创建addPaper.jsp,allPaper.jsp,updatePaper.jsp。
addPaper.jsp文件内容
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>新增文章</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
新增文章
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增文章</small>
</h1>
</div>
</div>
</div>
<form action="" name="userForm">
文章名称:<input type="text" name="paperName"><br><br><br>
文章数量:<input type="text" name="paperNum"><br><br><br>
文章详情:<input type="text" name="paperDetail"><br><br><br>
<input type="button" value="添加" onclick="addPaper()">
</form>
<script type="text/javascript">
function addPaper() {
var form = document.forms[0];
form.action = "<%=basePath %>paper/addPaper";
form.method = "post";
form.submit();
}
</script>
</div>
allPaper.jsp文件内容
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String appPath = request.getContextPath(); %>
<html>
<head>
<title>文章列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
所有的文章
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>文章列表 —— 显示所有文章</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${path}/paper/toAddPaper">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>文章编号</th>
<th>文章名字</th>
<th>文章数量</th>
<th>文章详情</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="paper" items="${requestScope.get('list')}" varStatus="status">
<tr>
<td>${paper.paperId}</td>
<td>${paper.paperName}</td>
<td>${paper.paperNum}</td>
<td>${paper.paperDetail}</td>
<td>
<a href="${path}/paper/toUpdatePaper?id=${paper.paperId}">更改</a> |
<a href="<%=appPath%>/paper/del/${paper.paperId}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
updatePaper.jsp文件内容
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>修改文章</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
修改文章
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改文章</small>
</h1>
</div>
</div>
</div>
<form action="" name="userForm">
<input type="hidden" name="paperId" value="${paper.paperId}"/>
文章名称:<input type="text" name="paperName" value="${paper.paperName}"/>
文章数量:<input type="text" name="paperNum" value="${paper.paperNum}"/>
文章详情:<input type="text" name="paperDetail" value="${paper.paperDetail }"/>
<input type="button" value="提交" onclick="updatePaper()"/>
</form>
<script type="text/javascript">
function updatePaper() {
var form = document.forms[0];
form.action = "<%=basePath %>paper/updatePaper";
form.method = "post";
form.submit();
}
</script>
</div>
完成后,会出现以下文件列表
webapp.png
6、从java文件夹入手,创建com包,然后在其下继续创建controller,dao,pojo,service四个包。
1)在controller包下,创建PaperController.java文件
PaperController.java文件内容,这是控制层,前端访问的地址
package com.controller;
import com.pojo.Paper;
import com.service.PaperService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/paper")
public class PaperController {
@Autowired
private PaperService paperService;
@RequestMapping("/allPaper")
public String list(Model model){
List<Paper> list = paperService.queryAllPaper();
model.addAttribute("list", list);
return "allPaper";
}
@RequestMapping("toAddPaper")
public String toAddPaper(){
return "addPaper";
}
@RequestMapping("/addPaper")
public String addPaper(Paper paper){
paperService.addPaper(paper);
return "redirect:/paper/allPaper";
}
@RequestMapping("/del/{paperId}")
public String deletePaper(@PathVariable("paperId") Long id){
paperService.deletePaperById(id);
return "redirect:/paper/allPaper";
}
@RequestMapping("toUpdatePaper")
public String toUpdatePaper(Model model, Long id){
model.addAttribute("paper", paperService.queryById(id));
return "updatePaper";
}
@RequestMapping("/updatePaper")
public String updatePaper(Model model, Paper paper){
paperService.updatePaper(paper);
paper = paperService.queryById(paper.getPaperId());
model.addAttribute("paper", paper);
return "redirect:/paper/allPaper";
}
}
2)在dao包下,创建PaperDao.java文件
PaperDao.java文件内容,访问的方法接口。
package com.dao;
import com.pojo.Paper;
import java.util.List;
public interface PaperDao {
int addPaper(Paper paper);
int deletePaperById(long id);
int updatePaper(Paper paper);
Paper queryById(long id);
List<Paper> queryAllPaper();
}
3)在pojo包下,创建Paper.java
Paper.java文件内容,典型javabean,跟数据库关联。
package com.pojo;
public class Paper {
private long paperId;
private String paperName;
private int paperNum;
private String paperDetail;
public long getPaperId() {
return paperId;
}
public void setPaperId(long paperId) {
this.paperId = paperId;
}
public String getPaperName() {
return paperName;
}
public void setPaperName(String paperName) {
this.paperName = paperName;
}
public int getPaperNum() {
return paperNum;
}
public void setPaperNum(int paperNum) {
this.paperNum = paperNum;
}
public String getPaperDetail() {
return paperDetail;
}
public void setPaperDetail(String paperDetail) {
this.paperDetail = paperDetail;
}
}
4)在service包下,创建接口PaperService.java和实现类PaperServiceImpl.java
PaperService.java文件内容,对数据库的操作方法接口
package com.service;
import com.pojo.Paper;
import java.util.List;
public interface PaperService {
int addPaper(Paper paper);
int deletePaperById(long id);
int updatePaper(Paper paper);
Paper queryById(long id);
List<Paper> queryAllPaper();
}
PaperServiceImpl.java文件内容,实现类
package com.service.impl;
import com.pojo.Paper;
import com.dao.PaperDao;
import com.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PaperServiceImpl implements PaperService {
@Autowired
private PaperDao paperDao;
@Override
public int addPaper(Paper paper){
return paperDao.addPaper(paper);
}
@Override
public int deletePaperById(long id){
return paperDao.deletePaperById(id);
}
@Override
public int updatePaper(Paper paper){
return paperDao.updatePaper(paper);
}
@Override
public Paper queryById(long id){
return paperDao.queryById(id);
}
@Override
public List<Paper> queryAllPaper(){
return paperDao.queryAllPaper();
}
}
完成后,文件列表会出现下方列表
java.png
7、运行环境配置,使用tomcat。在idea工具第一行,找到“Run”,在里面选择“edit configuration”,然后出现弹框,点击“+”,选择tomcat里面的local。
我这里使用的是本地的tomcat,所以要查看是否配置好。
8、选择index.jsp,在代码里右键,选择run。。。。
image.png 首页页面.png
网友评论