目录
一、准备工作
本Demo使用Eclipse作为开发IDE,运行容器是Tomcat,数据库是MySQL,后端使用Maven管理jar包,前端展现使用Bootstrap。因此,所需工具和准备有:
- Eclipse
Demo使用的Eclipse版本为Oxygen.1a Release (4.7.1a),下载列表地址为:
https://www.eclipse.org/downloads/packages/release/oxygen/1a
- Tomcat
Demo使用的Tomcat版本为8.5.30,下载列表地址为:
https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.30/bin/
- MySQL
Demo使用的MySQL版本为5.7.3-m13,下载列表地址为:
http://www.mirrorservice.org/sites/ftp.mysql.com/Downloads/MySQLInstaller/
- Maven
Demo使用的Maven版本为3.5.2,下载列表地址为:
http://apache.dattatec.com/maven/maven-3/
- Bootstrap
Demo使用的Bootstrap版本为3.3.7,下载列表地址为:
https://getbootstrap.com/docs/versions/
- jQuery
Demo使用的jQuery版本为3.4.0,下载列表地址为:
二、框架搭建
由于安装IDE和配置Maven、Tomcat不是主要介绍的内容,此处略过。下面直接进行框架的搭建。
1.集成Spring与Spring MVC
1)新建Maven项目
在Eclipse的File菜单项中选择new,在随后的子菜单中选择Maven Project:
随后出现新建窗口:
只勾选使用默认工作空间地址就行,其他可以不用选,然后点击Next:
由于Demo是web工程,所以选择webapp这一项就好,然后点击Next:
填写Group Id、Artifact Id然后点击Finish即可完成建立Maven项目了。
建立好的项目结构:
这里发现引入的JRE版本较低,需要替换掉,本Demo采用的版本是jre8。替换方法是:选中项目名,使用快捷键alt+enter,进入项目的properties设置对话框:
选中Java Build Path,Remove掉JRE库,并点击Add Library…按钮,添加jre8库:
点击Apply按钮保存配置,然后选择左边菜单项中的Java Compiler,把编译器level选为1.8:
点击Apply按钮保存配置,然后选择左边菜单项中的Project Facets,把Java这一项选为1.8
点击Apply按钮保存配置,然后选择左边菜单项中的Server,把Server选上Tomcat:
点击Apply按钮保存配置,然后选择左边菜单项中的Targeted Runtimes,选中Tomcat:
点击Apply and Close按钮保存配置并关闭对话框。
现在项目结构如下:
2)配置文件
现在来配置pom.xml文件,Demo使用的Spring版本是5.1.6.RELEASE:
<properties>
<spring.version>5.1.6.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<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>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
</dependencies>
然后是配置web.xml文件,关于Spring的配置是指明Spring配置文件所在路径和添加监听器:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext-spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
关于Spring MVC的配置是添加servlet,并在其中初始化参数里指明Spring MVC配置文件所在路径:
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext-springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在resources文件夹中新建config文件夹,并建立配置文件。
首先配置applicationContext-spring.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描注解类(service) -->
<context:component-scan base-package="com.ynter.service" />
</beans>
采用Spring的自动注解,配置其去扫描service类并自动装配bean。
接下来配置applicationContext-springmvc.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
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.xsd">
<!-- 开启MVC注解 -->
<mvc:annotation-driven />
<!-- 扫描注解类(controller) -->
<context:component-scan base-package="com.ynter.controller" />
<!-- 配置 Spring MVC 的视图模板 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 支持JSTL -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 模板的路径 -->
<property name="prefix" value="/WEB-INF/view/" />
<!-- 模板文件后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 静态资源访问 -->
<mvc:resources location="/WEB-INF/res/js/" mapping="/js/**" />
<mvc:resources location="/WEB-INF/res/css/" mapping="/css/**" />
<mvc:resources location="/WEB-INF/res/images/" mapping="/images/**" />
</beans>
此时的项目结构为:
到此Spring和SpringMVC初步集成完毕。
3)运行框架
首先运行项目试试能不能访问(已经预先准备了简单内容的index.jsp):
创建controller文件TestController.java并进行编写:
@Controller
@RequestMapping("/test")
public class TestController {
@PostMapping("/showMessage")
public ModelAndView showMessage(HttpServletRequest request) {
ModelAndView mv = new ModelAndView("test/showMessage");
mv.addObject("message", "Hello ssm demo!");
return mv;
}
}
在index.jsp中加入表单:
<form action="test/showMessage" method="post">
<button type="submit">点击跳转</button>
</form>
新增showMessage.jsp:
<%@ page language="java" contentType="text/html; charset=utf8"
pageEncoding="utf8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>SSM Demo Index</title>
</head>
<body style="text-align:center;">
<h2 style="text-align:center;">Welcome to ssm demo!</h2>
<p>The message is : ${message}</p>
</body>
</html>
保存项目后重部署到Tomcat,并进行访问:
此时项目结构为:
2.加载Bootstrap
1)引入Bootstrap
Demo使用的Bootstrap需要用到jQuery,所以引入Bootstrap的同时也把jQuery一起引入。在WEB-INF文件夹下新建util文件夹,用来存放下载并解压出来的插件。
改造applicationContext-springmvc.xml,加入对Bootstrap所在路径的静态映射配置:
<mvc:resources location="/WEB-INF/util/bootstrap/" mapping="/bootstrap/**" />
<mvc:resources location="/WEB-INF/util/jQuery/" mapping="/jQuery/**" />
Bootstrap的引入就完成了。
2)应用Bootstrap
首先在index.jsp中引入Bootstrap所需文件:
<script type="text/javascript"
src="${pageContext.request.contextPath}/jQuery/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css" />
然后改造下页面内容:
<body>
<div class="container">
<h2>Welcome to ssm demo! - index</h2>
</div>
<div class="container">
<form action="test/showMessage" method="post" class="form-index">
<div class="form-group">
<button type="submit" class="btn btn-lg btn-primary btn-block">点击跳转</button>
</div>
</form>
</div>
</body>
对于showMessage.jsp页面做同样的引入并进行改造。
重部署项目并重启Tomcat,访问项目:
3.集成Mybatis
1)配置文件
Demo使用的Mybatis版本是3.5.1,在pom.xml文件中配置:
<properties>
…
<mybatis.version>3.5.1</mybatis.version>
<mybatis.spring.version>2.0.1</mybatis.spring.version>
</properties>
<dependencies>
…
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
</dependencies>
由于需要连接MySQL数据库,所需驱动也要配置进来:
<properties>
…
<mysql.version>5.1.38</mysql.version>
</properties>
<dependencies>
…
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
…
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
使用Spring管理数据库链接要用到数据连接池,这里使用的是c3p0,因此一并配置进来:
<properties>
…
<c3p0.version>0.9.5.4</c3p0.version>
</properties>
<dependencies>
…
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
</dependencies>
接下来就是对Mybatis的整体配置,首先建立数据库连接文件datasource.properties:
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useSSL=false
dataSource.user=root
dataSource.password=root
dataSource.maxPoolSize=20
dataSource.maxIdleTime = 1000
dataSource.minPoolSize=5
dataSource.initialPoolSize=5
然后对applicationContext-spring.xml进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描注解类(service) -->
<context:component-scan base-package="com.ynter.service" />
<!-- 扫描 mapper 接口并自动引入 -->
<mybatis:scan base-package="com.ynter.mapper" />
<!-- 加载数据源参数 -->
<context:property-override location="classpath:config/datasource.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" />
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" />
<!-- JDBC事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<!-- 启用注解方式事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
至此Mybatis的集成完毕。
2)运行框架
首先在数据库建立表:
CREATE TABLE `t_book_info` (
`id`varchar(32) NOT NULL,
`name` varchar(32) DEFAULT NULL,
`author` varchar(32) DEFAULT NULL,
`isbn` varchar(17) DEFAULT NULL,
`price` float DEFAULT NULL,
`in_time` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
在Demo中建立对应的pojo类BookInfoPojo.java,并进行编写(由于引入了lombok,可以省略get/set方法,具体在下一节介绍):
package com.ynter.pojo;
import java.util.Date;
import lombok.Data;
@Data
public class BookInfoPojo {
private String id;
private String name;
private String author;
private String isbn;
private Float price;
private Date inTime;
}
建立mapper接口BookInfoMapper.java,并进行编写:
package com.ynter.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.ynter.pojo.BookInfoPojo;
public interface BookInfoMapper {
@Select(" SELECT * FROM t_book_info")
public List<BookInfoPojo> findAll();
}
建立service接口BookInfoService.java,并进行编写:
package com.ynter.service;
import java.util.List;
import com.ynter.pojo.BookInfoPojo;
public interface BookInfoService {
public List<BookInfoPojo> getBookInfos();
}
建立service实现类BookInfoServiceImpl.java,并进行编写:
package com.ynter.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ynter.mapper.BookInfoMapper;
import com.ynter.pojo.BookInfoPojo;
import com.ynter.service.BookInfoService;
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class BookInfoServiceImpl implements BookInfoService {
@Autowired
private BookInfoMapper bookInfoMapper;
@Override
@Transactional(readOnly = true)
public List<BookInfoPojo> getBookInfos() {
return bookInfoMapper.findAll();
}
}
建立controller类BookInfoController.java,并进行编写:
package com.ynter.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ynter.service.BookInfoService;
@Controller
@RequestMapping("/bookInfo")
public class BookInfoController {
@Autowired
private BookInfoService bookInfoService;
@RequestMapping("/showBookInfos")
public ModelAndView showBookInfos(HttpServletRequest request) {
ModelAndView mv = new ModelAndView("bookInfo/showBookInfos");
mv.addObject("bookInfos", bookInfoService.getBookInfos());
return mv;
}
}
建立页面showBookInfos.jsp,并编写内容:
<%@ page language="java" contentType="text/html; charset=utf8"
pageEncoding="utf8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<script type="text/javascript"
src="${pageContext.request.contextPath}/jQuery/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet"
href="${pageContext.request.contextPath}/css/common.css" />
<link rel="stylesheet"
href="${pageContext.request.contextPath}/css/bookInfo/bookInfo.css" />
<title>showBookInfos</title>
</head>
<body>
<div class="container">
<h1>showBookInfos</h1>
</div>
<div class="div-table">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>书名</th>
<th>作者</th>
<th>ISBN</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<c:forEach items="${bookInfos}" var="bookInfo" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${bookInfo.name}</td>
<td>${bookInfo.author}</td>
<td>${bookInfo.isbn}</td>
<td>${bookInfo.price}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="container div-return">
<a class="btn btn-lg btn-primary btn-block" href="${pageContext.request.contextPath}">返回</a>
</div>
</body>
</html>
改造页面index.jsp,添加跳转按钮:
…
<div class="container">
<form action="bookInfo/showBookInfos" method="post" class="form-index">
<div class="form-group">
<button type="submit" class="btn btn-lg btn-primary btn-block">查看图书信息</button>
</div>
</form>
</div>
…
重部署Demo,重启Tomcat,看看效果:
至此,整个框架就集成完毕。现在项目结构如下:
4.改进框架
1)引入Lombok
Demo中可以引入lombok来让pojo类省去get/set编写,具体引入步骤如下:
在pom.xml中配置:
…
<properties>
<lombok.version>1.18.6</lombok.version>
</properties>
…
<dependencies>
…
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
…
保存后maven将会自动下载lombok的jar包,需要去到下载目录中找到该jar包,双击启动安装程序:
点击Specify location…按钮选择eclipse所在目录,然后点击Install/Update按钮进行安装。安装完毕后会在eclipse目录下看到Lombok程序lombok.jar:
然后就可以在Demo中使用Lombok插件了(上节的pojo类中)。
2)引入Log4j
为了方便调试,可以在Demo中引入Log4j,这里引入的是Log4j2,具体步骤为:
在pom.xml配置:
…
<properties>
<log4j2.version>2.11.2</log4j2.version>
</properties>
…
<dependencies>
…
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j2.version}</version>
</dependency>
</dependencies>
…
接下来,在web.xml中配置:
…
<!-- log4j2 配置 start -->
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:config/applicationContext-log4j2.xml</param-value>
</context-param>
<!-- log4j2 配置 end -->
…
在相应位置建立applicationContext-log4j2.xml文件,并编写内容:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="Warn" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %5p] [%C] [%M : %L]%n====>%m%n%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="info" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.mybatis" level="info" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="com.ynter" level="debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
之后只需在需要使用Log4j的类中引入以下语句即可使用:
…
private static final Logger log = LogManager.getLogger();
…
5.完善Demo
这一节对Demo中关于BookInfo的增删改等功能进行完善。
首先对BookInfoMapper.java进行修改:
…
@SelectKey(statement = " SELECT REPLACE(UUID(),'-','')", keyProperty = "id", resultType = String.class, before = true)
@Insert(" INSERT INTO t_book_info(id, name, author, isbn, price, in_time) VALUES(#{id}, #{name}, #{author}, #{isbn}, #{price}, now())")
public int insert(BookInfoPojo bookInfo);
@Delete(" DELETE FROM t_book_info WHERE id = #{id}")
public int delete(String id);
@Select(" SELECT * FROM t_book_info")
public List<BookInfoPojo> findAll();
@Update(" UPDATE t_book_info SET name = #{name}, author = #{author}, isbn = #{isbn}, price = #{price} WHERE id = #{id}")
public int update(BookInfoPojo bookInfo);
…
接下来对BookInfoService.java进行修改:
…
public int setBookInfo(BookInfoPojo bookInfo);
public int removeBookInfo(String id);
public List<BookInfoPojo> getBookInfos();
public int changeBookInfo(BookInfoPojo bookInfo);
…
对BookInfoServiceImpl.java进行修改:
…
@Override
@Transactional()
public int setBookInfo(BookInfoPojo bookInfo) {
return bookInfoMapper.insert(bookInfo);
}
@Override
@Transactional()
public int removeBookInfo(String id) {
return bookInfoMapper.delete(id);
}
@Override
@Transactional(readOnly = true)
public List<BookInfoPojo> getBookInfos() {
return bookInfoMapper.findAll();
}
@Override
@Transactional()
public int changeBookInfo(BookInfoPojo bookInfo) {
return bookInfoMapper.update(bookInfo);
}
…
对BookInfoController.java进行修改:
…
@RequestMapping("/createBookInfo")
public ModelAndView createBookInfo(HttpServletRequest request) {
BookInfoPojo bookInfo = new BookInfoPojo();
String name = request.getParameter("name");
String author = request.getParameter("author");
String isbn = request.getParameter("isbn");
Float price = Float.valueOf(request.getParameter("price"));
bookInfo.setName(name);
bookInfo.setAuthor(author);
bookInfo.setIsbn(isbn);
bookInfo.setPrice(price);
int result = bookInfoService.setBookInfo(bookInfo);
log.info("The insert result is : " + (result > 0 ? true : false));
return showBookInfos(request);
}
@RequestMapping("/destroyBookInfo")
public ModelAndView destroyBookInfo(HttpServletRequest request) {
String id = request.getParameter("id");
int result = bookInfoService.removeBookInfo(id);
log.info("The delete result is : " + (result > 0 ? true : false));
return showBookInfos(request);
}
@RequestMapping("/showBookInfos")
public ModelAndView showBookInfos(HttpServletRequest request) {
ModelAndView mv = new ModelAndView("bookInfo/showBookInfos");
mv.addObject("bookInfos", bookInfoService.getBookInfos());
return mv;
}
@RequestMapping("/modifyBookInfo")
public ModelAndView modifyBookInfo(HttpServletRequest request) {
BookInfoPojo bookInfo = new BookInfoPojo();
String id = request.getParameter("id");
String name = request.getParameter("name");
String author = request.getParameter("author");
String isbn = request.getParameter("isbn");
Float price = Float.valueOf(request.getParameter("price"));
bookInfo.setId(id);
bookInfo.setName(name);
bookInfo.setAuthor(author);
bookInfo.setIsbn(isbn);
bookInfo.setPrice(price);
int result = bookInfoService.changeBookInfo(bookInfo);
log.info("The update result is : " + (result > 0 ? true : false));
return showBookInfos(request);
}
…
最后是对页面进行修改,代码略。
重部署Demo,重启Tomcat,效果如下:
三、总结
Demo在集成时其实也遇到很多问题,最终通过查资料等方式解决了,下面简单概况下一些可能会碰到的问题。
- Maven在初始建立时有可能需要调节下参数,例如Java Build Path的内容、Java Compiler的内容等,在上面也提到过。
- Web.xml在配置时,要注意使用的版本,本Demo用的是3.0,可以在配置文件的开头查看到。例如:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
…
</web-app>
- dataSource.properties文件中,由于Demo的配置,需要各属性名都严格匹配。
- Mapper接口中要注意SQL语句的拼写与参数的传入。
- 可以引入Junit4与Spring Test结合进行项目的单元测试(Demo中已引入)。
- 关于mybatis注解的资料可以参考:http://www.mybatis.org/mybatis-3/zh/java-api.html
- 关于log4j2配置的资料可以参考:http://logging.apache.org/log4j/2.x/manual/configuration.html
- 关于bootstrap的使用可以参考:https://v3.bootcss.com/css/
*另:项目代码可以在这里下载:https://download.csdn.net/download/ynter/11147066