SSM项目编程思路
项目准备
1、 分析需求,根据需求创建相应数据库 (基本sql语句)
2、 创建Maven项目,配置pom.xml,导入相应jar包 (mvn仓库),需要再Artifacts手动创建lib目录将jar包部署
3、 根据数据库创建相应pojo,搭建日志环境Log4j (Book lombok插件 log4j.properties)
Dao层
4、 根据业务需求创建接口,编写增删改查的方法 (BookMapper)
5、 编写BookMapper.xml配置文件,编写sql语句,实现BookMapper接口中的功能。 (BookMapper.xml)
6、 编写mybatis-config.xml配置文件,设置别名、绑定mapper等 (mybatis.xml)
7、 编写spring-dao.xml配置文件,创建sqlSession,有多种方法,本案例采用继承SqlSessionDaoSupport,
给BookMapperImpl注入sqlSessionFactory,dataSource也有多种方法,c3p0/druid/spring-jdbc等,
本案例采用spring-jdbc,另外将pojo类交由spring-dao管理 (spring-dao.xml)
8、 编写BookMapper接口实现类,用java实现BookMapper.xml中的sql (BookMapperImpl.xml)
Service层
9、 编写BookService接口,写入需要实现的业务 (BookService)
10、编写BookService实现类,实现方法,此处需要组合BookMapper (BookServiceImpl)
11、编写spring-service.xml,并将service层类交由其管理,此处需要注入BookMapper,
@Resource先根据名字注入,不匹配再按照类型注入,@Autowire那类型注入,可以和@Qualifier搭配使用 (spring-service.xml)
Controller层
12、编写web.xml配置DispatcherServlet和CharacterEncodingFilter,后者主要解决前端与后端的数据乱码问题,
DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:
①、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
②、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
③、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
④、通过ViewResolver解析逻辑视图名到具体视图实现;
⑤、本地化解析;
⑥、渲染具体的视图等;
⑦、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。 (web.xml)
13、编写spring-mvc.xml配置视图层InternalViewResolver,注入前缀后缀,开启扫描注解context和mvn的均要开启以及开启对静态资源的访问。 (spring-mvc.xml)
14、编写Controller通过@Controller和return “页面名称”以及@RequestMapping实现对页面的访问,此外需要注入bookService,
同上,可以通过@Resource、@Autowire和@Qualifier搭配使用实现注入。 (Controller)
15、根据业务内容去编写前端页面,可以参考***Bootstrao可视化系统***
16、配置Tomcat服务器,fix项目的Artifacts即可。
SSM环境搭建
SQL
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
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>org.example</groupId>
<artifactId>booksystem</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/beal.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
Book.java
package com.beal.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private int bookID;
private String bookName;
private Integer bookCounts;
private String detail;
}
BookMapper.java
package com.beal.dao;
import com.beal.pojo.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-07 19:20
*/
public interface BookMapper {
/**
* select all books
*
* @return
*/
public List<Book> queryAllBooks();
/**
* add a Book
*
* @param book
* @return
*/
public int addBook(Book book);
/**
* delete a book
*
* @param id
* @return
*/
public int deleteBook(@Param("bookid") int id);
/**
* update a book
* @param book
* @return
*/
public int updateBook(Book book);
/**
*
* @param id
* @return
*/
public Book queryForOneBook(@Param("id") int id);
}
BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.beal.dao.BookMapper">
<select id="queryAllBooks" resultType="book">
select *
from books
</select>
<insert id="addBook" parameterType="book">
insert into books (bookName, bookCounts, detail)
values (#{bookName}, #{bookCounts}, #{detail});
</insert>
<delete id="deleteBook" parameterType="int">
delete
from books
where bookID = #{bookid};
</delete>
<update id="updateBook" parameterType="book">
update books
set bookName = #{bookName},
bookCounts = #{bookCounts},
detail = #{detail}
where bookID = #{bookID};
</update>
<select id="queryForOneBook" parameterType="int" resultType="book">
select *
from books
where bookID = #{id};
</select>
</mapper>
BookMapperImpl.java
package com.beal.dao;
import com.beal.pojo.Book;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-08 16:11
*/
public class BookMapperImpl extends SqlSessionDaoSupport implements BookMapper{
@Override
public List<Book> queryAllBooks() {
return getSqlSession().getMapper(BookMapper.class).queryAllBooks();
}
@Override
public int addBook(Book book) {
return getSqlSession().getMapper(BookMapper.class).addBook(book);
}
@Override
public int deleteBook(int id) {
return getSqlSession().getMapper(BookMapper.class).deleteBook(id);
}
@Override
public int updateBook(Book book) {
return getSqlSession().getMapper(BookMapper.class).updateBook(book);
}
@Override
public Book queryForOneBook(int id) {
return getSqlSession().getMapper(BookMapper.class).queryForOneBook(id);
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="book" class="com.beal.pojo.Book"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/ssmbuilds?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="055134"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="bookMapper" class="com.beal.dao.BookMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
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>
<typeAliases>
<package name="com.beal.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/beal/dao/BookMapper.xml"/>
</mappers>
</configuration>
DaoTest.java
package com.beal.dao;
import com.beal.pojo.Book;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-08 16:11
*/
public class BookMapperImpl extends SqlSessionDaoSupport implements BookMapper{
@Override
public List<Book> queryAllBooks() {
return getSqlSession().getMapper(BookMapper.class).queryAllBooks();
}
@Override
public int addBook(Book book) {
return getSqlSession().getMapper(BookMapper.class).addBook(book);
}
@Override
public int deleteBook(int id) {
return getSqlSession().getMapper(BookMapper.class).deleteBook(id);
}
@Override
public int updateBook(Book book) {
return getSqlSession().getMapper(BookMapper.class).updateBook(book);
}
@Override
public Book queryForOneBook(int id) {
return getSqlSession().getMapper(BookMapper.class).queryForOneBook(id);
}
}
BookService.java
package com.beal.service;
import com.beal.pojo.Book;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-07 21:46
*/
public interface BookService {
/**
* add a book
*
* @param book
*/
public void addBook(Book book);
/**
* delete a book
*
* @param id
*/
public void deleteBook(int id);
/**
* update a book
*
* @param book
*/
public void updateBook(Book book);
/**
* query a book
*
* @param id
* @return
*/
public Book queryForOneBook(int id);
/**
* query all books
*
* @return
*/
public List<Book> queryAllBooks();
}
BookServiceImpl.java
package com.beal.service;
import com.beal.dao.BookMapper;
import com.beal.pojo.Book;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-07 21:46
*/
public class BookServiceImpl implements BookService {
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public void addBook(Book book) {
bookMapper.addBook(book);
}
@Override
public void deleteBook(int id) {
bookMapper.deleteBook(id);
}
@Override
public void updateBook(Book book) {
bookMapper.updateBook(book);
}
@Override
public Book queryForOneBook(int id) {
return bookMapper.queryForOneBook(id);
}
@Override
public List<Book> queryAllBooks() {
return bookMapper.queryAllBooks();
}
}
spring-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookServiceImpl" class="com.beal.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
</beans>
ServiceTest.java
package com.beal.service;
import com.beal.dao.BookMapper;
import com.beal.pojo.Book;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-08 16:27
*/
public class ServiceTest {
@Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookServiceImpl = (BookService) context.getBean("bookServiceImpl");
List<Book> bookList = bookServiceImpl.queryAllBooks();
for (Book book : bookList) {
System.out.println(book);
}
}
}
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">
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<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>
<!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
spring-mvc.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:mvn="http://www.springframework.org/schema/mvc"
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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<mvn:annotation-driven/>
<mvn:default-servlet-handler/>
<context:component-scan base-package="com.beal.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller.java
package com.beal.controller;
import com.beal.pojo.Book;
import com.beal.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* @author BealQin
* @create 2021-06-08 16:29
*/
@Controller
@RequestMapping("/book")
public class BookController {
// @Resource(name = "bookServiceImpl")
@Autowired
@Qualifier(value = "bookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String test1(Model model) {
List<Book> bookList = bookService.queryAllBooks();
model.addAttribute("list", bookList);
return "allBook";
}
@RequestMapping("/editBook")
public String editBook() {
return "editBook";
}
@RequestMapping("/addBook")
public String addBook(Book book) {
bookService.addBook(book);
return "redirect:/book/allBook";
}
@RequestMapping("/deleteBook")
public String deleteBook(Integer bookID) {
bookService.deleteBook(bookID);
return "redirect:/book/allBook";
}
@RequestMapping("/update")
public String update(){
return "update";
}
@RequestMapping("/updateBook")
public String updateBook(Book book) {
bookService.updateBook(book);
return "redirect:/book/allBook";
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
前端页面
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<blockquote class="blockquote text-center">
<p class="mb-0">A well-known quote, contained in a blockquote element.</p>
<footer class="blockquote-footer">Someone famous in <cite title="Source Title">Source Title</cite></footer>
</blockquote>
<p class="text-center">
<a href="${pageContext.request.contextPath}/book/allBook" class="btn btn-primary btn-lg">进入图书管理系统</a>
</p>
</body>
</html>
allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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>
<small>书籍列表 —— 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/editBook">新增</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="book" items="${list}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/update?bookID=${book.bookID}&bookName=${book.bookName}&bookCounts=${book.bookCounts}&detail=${book.detail}">更改</a>
|
<a href="${pageContext.request.contextPath}/book/deleteBook?bookID=${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
editBook.jsp
<%--
Created by IntelliJ IDEA.
User: Beal Qin
Date: 2021/6/8
Time: 20:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<p class="text-center">
<form action="${pageContext.request.contextPath}/book/addBook" method="get">
<div class="form-group" align="center">
<label>书籍名字</label>
<input type="text" class="form-control" name="bookName" value="${param.bookName}" required>
</div>
<div class="form-group" align="center">
<label>书籍数量</label>
<input type="text" class="form-control" name="bookCounts" value="${param.bookCounts}" required>
</div>
<div class="form-group" align="center">
<label>书籍详情</label>
<input type="text" class="form-control" name="detail" value="${param.detail}" required>
</div>
<div align="center">
<button type="submit" class="btn btn-default">添加</button>
</div>
</form>
</p>
</body>
</html>
updateBook.jsp
<%--
Created by IntelliJ IDEA.
User: Beal Qin
Date: 2021/6/8
Time: 20:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改书籍</title>
</head>
<body>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<p class="text-center">
<form action="${pageContext.request.contextPath}/book/updateBook" method="get">
<input type="hidden" name="bookID" value="${param.bookID}">
<div class="form-group" align="center">
<label>书籍名字</label>
<input type="text" class="form-control" name="bookName" value="${param.bookName}" required>
</div>
<div class="form-group" align="center">
<label>书籍数量</label>
<input type="text" class="form-control" name="bookCounts" value="${param.bookCounts}" required>
</div>
<div class="form-group" align="center">
<label>书籍详情</label>
<input type="text" class="form-control" name="detail" value="${param.detail}" required>
</div>
<div align="center">
<button type="submit" class="btn btn-default">修改</button>
</div>
</form>
</p>
</body>
</html>
注意小细节
① 给工程Artifacts添加lib目录将包导入
② 项目的默认maven版本要进行修改
③ tomcat配置时,无需其他操作,fix模块就行
④ 配置DispatcherServlet的初始参数时,文件应选择applicationContext.xml,不选择spring-mvc.xml
⑤ 前端页面传回对象的属性时,Controller层可以用对象接收,字段名不匹配时则为空。
⑥ BeanCreateException出现在sqlSessionFactory时,尝试把数据库连接工作交给mybatis-config.xml管理,
就避开了sqlSessionFactory问题了。(参考如下xml文件)
待补充。。。
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>
<typeAliases>
<package name="com.beal.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/ssmbuilds?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="055134"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/beal/dao/BookMapper.xml"/>
</mappers>
</configuration>
spring-dao.xml (不管理dataSource)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="book" class="com.beal.pojo.Book"/>
<bean id="bookMapper" class="com.beal.dao.BookMapperImpl"/>
</beans>