一、说明:
目前工作中的开发框架主要是 spring ,使用的是 springMVC+ spring + spring JdbcTemplate ,最近学习了一下Mybatis、Maven这些内容,所以尝试使用 springMVC + Spring + Mybatis + Maven来搭建一个简单的demo ,功能很简单,主要是对于一个表的CRUD。
项目源码下载地址:github下载地址
项目结构图:
二、环境搭建流程
① 、新建一个Maven工程,并改项目为WEB项目,创建出相应的项目结构。
步骤:New---》Project----》Maven Project
之后选择Next:
选中 Create a simple project 选项,然后Next
然后点击Finish ,工程创建完成,结构如下:
然后,右键选中项目,打开Properties选项
将图中的Dynamic Web Module 选项去掉,点击OK保存
然后再次选中项目打开上述的菜单,并且选中Dynamic Web Module 选项,对话框出现下图的提示:
此时,点击下面的:Further configuration avaaliable选项 ,出现如下对话框
将web目录改为src/main/webapp ,之后ok保存,发现在项目的src/main.webapp下面多出了WEB-INF与META-INF文件,同时还生成了web.xml 文件,如下图:
②、配置相关的配置文件
1)、配置Maven的pom.xml文件,引入整合所需要的jar包:
<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.elgin</groupId> <artifactId>ssm_first</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- spring 版本号 --> <spring.version>4.1.4.RELEASE</spring.version> <!-- junit 版本号 --> <junit.version>4.10</junit.version> <!-- mybatis 版本号 --> <mybatis.version>3.3.0</mybatis.version> </properties> <dependencies> <!-- 添加spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- 添加mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!-- mysql 驱动 --> <dependency> <groupId>Mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- 需要的log4j2日志包依赖 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.5</version> </dependency> <!-- 单元测试依赖的jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit4</groupId> <artifactId>org.junit4</artifactId> <version>4.3.1</version> </dependency> <!-- JSTL 标签库 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>com.springsource.org.apache.commons.fileupload</artifactId> <version>1.2.0</version> </dependency> </dependencies> </project> |
注:我使用的是oschina的Maven仓库,库不匹配的时候可能部分jar包加不进来。
2)、配置 web.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 声明项目的初始化参数 ,这里声明了spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 加入spring支持 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 加入springMVC支持 --> <!-- springmvc 配置文件默认加载路径为:WEB-INF/[servlet-name]-servlet.xml [servlet-name]为下面配置中的servlet-name元素的值,此处为springmvc --> <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:springmvc*.xml</param-value> </init-param> <!-- 1 代表容器初始化时加载servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 项目编码过滤器 --> <filter> <filter-name>encodingFileter</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>encodingFileter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- log4j2 支持 --> <context-param> <param-name>log4jConfiguration</param-name> <param-value>/WEB-INF/classes/log4j2.xml</param-value> </context-param> <listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener> <filter> <filter-name>log4jFilter</filter-name> <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class> </filter> <filter-mapping> <filter-name>log4jFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
3 )、配置spring的配置文件applicationContext.xml
在源码包src/main/resources下新建xml配置文件: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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 导入properties属性文件 --> <context:property-placeholder location="classpath:properties/datasource.properties" /> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driver}"></property> </bean> <!-- 配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 自动扫描mapper文件对应的接口类,将其中的接口自动创建成 MapperFactoryBean --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.elgin.dao"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务的传播属性 --> <tx:advice id="taansactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 参与事务的service --> <aop:config> <aop:pointcut expression="execution(* com.elgin.service.*.*(..))" id="servicePointCut" /> <aop:advisor advice-ref="taansactionAdvice" pointcut-ref="servicePointCut" /> </aop:config> </beans> |
4)、配置springMVC的配置文件:
在源码包src/main/resources下新建xml配置文件:springmvc.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 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-4.1.xsd"> <!-- 配置spring注解扫描的包 --> <context:component-scan base-package="com.elgin"></context:component-scan> <!-- 开启注解驱动 --> <!-- 会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter 这两个bean, 所以就没有机会再给DefaultAnnotationHandlerMapping注入interceptors属性,无法指定拦截器。 可以通过人工配置上面的两个Bean,不使用 <mvc:annotation-driven />,就可以 给interceptors属性 注入拦截器了 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 静态资源访问,解决页面无法访问到js、img和css文件夹中的文件的问题 --> <mvc:resources location="/imgages/" mapping="/imgages/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <!-- 视图解析器:InternalResourceViewResolver UrlBasedViewResolver 的子类,通常用于查找 JSP(类 InternalResourceView)和 JSTL(类 JstlView,InternalResourceView 的子类)等视图 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 文件上传限制 --> <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="5242880"></property> <property name="maxInMemorySize" value="20480"></property> </bean> </beans> |
5)、配置mybatis的配置文件:
在源码包src/main/resources下新建package:mybatis ,然后在mybatis包下新建mybatis的配置文件: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> <typeAlias type="com.elgin.entities.User" alias="User"/> </typeAliases> </configuration> |
6)、数据库相关配置:
在源码包src/main/resourecs下新建package:properties ,然后在properties包下新建:datasource.properties ,内容如下:
jdbc.username=root
jdbc.password=root jdbc.url=jdbc:mysql://localhost:3306/test jdbc.driver=com.mysql.jdbc.Driver |
7)、mybatis的dao接口映射文件:
在源码包src/main/resourecs下新建package:mapper,然后在mapper包下新建:userMapper.xml,内容如下:
<?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.elgin.dao.UserDao"> <!-- 新增一条记录 --> <insert id="addUser" parameterType="User"> insert into t_user(username,password,email,phone)values(#{username},#{password},#{email},#{phone}) </insert> <!-- 删除一条记录 --> <delete id="deleteUser" parameterType="Integer"> delete from t_user where id = #{id} </delete> <!-- 更新一条记录的数据 --> <update id="updateUser" parameterType="User"> update t_user set username=#{username},password=#{password},email=#{email},phone=#{phone} where id=#{id} </update> <!-- 查询单条记录 --> <select id="getUser" parameterType="Integer" resultType="User"> SELECT * FROM t_user WHERE id = #{id} </select> <!-- 查询多条记录 --> <select id="getUserList" resultType="User"> select * from t_user </select> </mapper> |
8)、配置log4j2配置文件:
在源码包src/main/resourecs下新建xml配置文件:log4j2.xml ,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<Configuration status="OFF"> <Appenders> <!-- 输出到文件 --> <File name="file" fileName="d:/myapp.log" append="true"> <PatternLayout pattern="%d %-5p %c - %m%n" /> </File> <!--输出到控制台配置--> <Console name="Console"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss } %p %l %m%n" /> </Console> <!-- 日志文件定期整理处理 --> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="d:/logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss } %p %l %m%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="20MB" /> </Policies> </RollingFile> </Appenders> <Loggers> <Logger level="debug" name="com.elgin" additivity="false"> <AppenderRef ref="Console" /> <AppenderRef ref="file" /> </Logger> <Root level="warn"> <AppenderRef ref="file" /> <AppenderRef ref="RollingFile" /> </Root> </Loggers> </Configuration> |
9)、开发dao,service,controller,实体entitis相关代码:
在源码包src/main/java中新建如下各个包:
实体类User:
package com.elgin.entities;
public class User { private Integer id; private String username; private String password; private String email; private String phone; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } } |
dao接口类:UserDao
package com.elgin.dao;
import java.util.List; import com.elgin.entities.User; public interface UserDao { public int addUser(User user); public int deleteUser(Integer id); public int updateUser(User user); public User getUser(Integer id); public List<User> getUserList(); } |
service接口类: UserService
package com.elgin.service;
import java.util.List; import com.elgin.entities.User; public interface UserService { public int addUser(User user); public int deleteUser(Integer id); public int updateUser(User user); public User getUser(Integer id); public List<User> getUserList(); } |
service实现类: UserServiceImpl
package com.elgin.service.impl;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.elgin.dao.UserDao; import com.elgin.entities.User; import com.elgin.service.UserService; @Service("userService") public class UserServiceImplimplements UserService { @Autowired private UserDao userDao; public int addUser(User user) { int result=userDao.addUser(user); return result; } public int deleteUser(Integer id) { int result=userDao.deleteUser(id); return result; } public int updateUser(User user) { int result=userDao.updateUser(user); return result; } public User getUser(Integer id) { User user=userDao.getUser(id); return user; } public List<User> getUserList() { List<User> list=userDao.getUserList(); return list; } } |
controller类: UserController
package com.elgin.controller;
import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.elgin.entities.User; import com.elgin.service.UserService; @Controller public class UserController { private Logger logger = Logger.getLogger(this.getClass()); @Autowired private UserService userService; /** * @Title: addPage * @Description: 用户添加页面 * @param mm * @return 参数 */ @RequestMapping("addpage.jhtml") public String addPage(ModelMap mm) { logger.info("add page..."); return "/register"; } /** * @Title: Regiseter * @Description: 用户添加操作 * @param request * @param response * @param user * @throws IOException * @throws ServletException 参数 */ @RequestMapping("register.jhtml") public void Regiseter(HttpServletRequest request, HttpServletResponse response, User user)throws IOException, ServletException { int result =0; if (user != null) { result = userService.addUser(user); } if (result >0) { logger.info("注册成功.."); response.sendRedirect("/allUser.jhtml"); } else { logger.error("注册失败"); request.getRequestDispatcher("/500.jhtml").forward(request, response); } } /** * @Title: listUser * @Description: 列表显示所有用户 * @param mm * @return 参数 */ @RequestMapping("allUser.jhtml") public String listUser(ModelMap mm) { List<User> list = userService.getUserList(); mm.put("list", list); return "/userlist"; } /** * @Title: editUser * @Description: 用户信息编辑页面 * @param id * @param mm * @return 参数 */ @RequestMapping("editUser.jhtml") public String editUser(String id, ModelMap mm) { User user = userService.getUser(Integer.parseInt(id)); mm.put("user", user); return "/register"; } /** * @Title: updateUser * @Description: 更新用户信息 * @param request * @param response * @param user * @throws Exception 参数 */ @RequestMapping("updateUser.jhtml") public void updateUser(HttpServletRequest request, HttpServletResponse response, User user)throws Exception { int result = userService.updateUser(user); if (result >0) { logger.info("更新用户数据成功"); response.sendRedirect("/allUser.jhtml"); } else { logger.error("更新用户数据失败"); request.getRequestDispatcher("/500.jhtml").forward(request, response); } } /** * @Title: deleteUserById * @Description: 删除一条用户记录 * @param request * @param response * @param id * @throws Exception 参数 */ @RequestMapping("deleteUser.jhtml") public void deleteUserById(HttpServletRequest request, HttpServletResponse response,String id) throws Exception { int result = userService.deleteUser(Integer.parseInt(id)); if (result >0) { logger.info("删除用户数据成功.."); response.sendRedirect("/allUser.jhtml"); } else { logger.error("删除用户数据失败.."); request.getRequestDispatcher("/500.jhtml").forward(request, response); } } } |
10)、测试页面:
根据视图解析器的配置,在webapp/WEB-INF下新建jsp文件夹,用来保存相关页面:
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Register</title> </head> <body> <c:if test="${empty user}"> <form action="register.jhtml" method="post"> </c:if> <c:if test="${not empty user}"> <form action="updateUser.jhtml" method="post"> <input type="hidden" value="${user.id}" name="id"> </c:if> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" value="${user.username}"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" value="${user.password}"></td> </tr> <tr> <td>邮件:</td> <td><input type="text" name="email" value="${user.email}"></td> </tr> <tr> <td>电话:</td> <td><input type="text" name="phone" value="${user.phone}"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交"></td> </tr> </table> </form> </body> </html> |
userlist.jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>User List</title> </head> <body> <a href="addpage.jhtml"/>用户注册</a> <br/> <br/> <span >所有用户</span> <br/> <br/> <table border="2px" width="800" > <tbody> <tr> <th>id</th> <th>用户名</th> <th>密码</th> <th>邮箱</th> <th>电话</th> <th>操作</th> </tr> <c:forEach items="${list}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.email}</td> <td>${user.phone}</td> <td> <a href="deleteUser.jhtml?id=${user.id}">删除</a> <a href="editUser.jhtml?id=${user.id}">编辑</a> </td> </tr> </c:forEach> </tbody> </table> </body> </html> |
三、测试结果:
将项目加载到tomcat,并启动。
访问:http://localhost:8080/addpage.jhtml ,进入编辑页面
输入数据点提交,数据保存成功并出现用户列表页面(编辑删除同理):
控制台日志: