SpringMvc+Spring+MyBatis+Maven
使用Maven构建项目
1) 创建一个Maven的war工程
2) 在webapp/WEB-INF下创建一个web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 </web-app>
3) Maven引入需要的jar包 地址:http://mvnrepository.com/ 可以在这里搜索需要的jar引入配置 到底是使用远程仓库还是本地仓库随读者的意愿。
引入jar的配置代码如下:
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>JavaWeb</groupId> 4 <artifactId>BMS</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 8 <!--引入项目依赖的jar包 --> 9 <!-- SpringMVC、Spring --> 10 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 11 <dependencies> 12 13 <!--引入pageHelper分页插件 --> 14 <dependency> 15 <groupId>com.github.pagehelper</groupId> 16 <artifactId>pagehelper</artifactId> 17 <version>5.0.0</version> 18 </dependency> 19 20 <!-- MBG --> 21 <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> 22 <dependency> 23 <groupId>org.mybatis.generator</groupId> 24 <artifactId>mybatis-generator-core</artifactId> 25 <version>1.3.5</version> 26 </dependency> 27 28 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-webmvc</artifactId> 32 <version>4.3.7.RELEASE</version> 33 </dependency> 34 35 <!-- 返回json字符串的支持 --> 36 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 37 <dependency> 38 <groupId>com.fasterxml.jackson.core</groupId> 39 <artifactId>jackson-databind</artifactId> 40 <version>2.8.8</version> 41 </dependency> 42 43 <!--JSR303数据校验支持;tomcat7及以上的服务器, 44 tomcat7以下的服务器:el表达式。额外给服务器的lib包中替换新的标准的el 45 --> 46 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> 47 <dependency> 48 <groupId>org.hibernate</groupId> 49 <artifactId>hibernate-validator</artifactId> 50 <version>5.4.1.Final</version> 51 </dependency> 52 53 54 <!-- Spring-Jdbc --> 55 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-jdbc</artifactId> 59 <version>4.3.7.RELEASE</version> 60 </dependency> 61 62 <!--Spring-test --> 63 <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-test</artifactId> 67 <version>4.3.7.RELEASE</version> 68 </dependency> 69 70 71 <!-- Spring面向切面编程 --> 72 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> 73 <dependency> 74 <groupId>org.springframework</groupId> 75 <artifactId>spring-aspects</artifactId> 76 <version>4.3.7.RELEASE</version> 77 </dependency> 78 79 <!--MyBatis --> 80 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 81 <dependency> 82 <groupId>org.mybatis</groupId> 83 <artifactId>mybatis</artifactId> 84 <version>3.4.2</version> 85 </dependency> 86 <!-- MyBatis整合Spring的适配包 --> 87 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 88 <dependency> 89 <groupId>org.mybatis</groupId> 90 <artifactId>mybatis-spring</artifactId> 91 <version>1.3.1</version> 92 </dependency> 93 94 <!-- 数据库连接池、驱动 --> 95 <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> 96 <dependency> 97 <groupId>c3p0</groupId> 98 <artifactId>c3p0</artifactId> 99 <version>0.9.1</version> 100 </dependency> 101 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 102 <dependency> 103 <groupId>mysql</groupId> 104 <artifactId>mysql-connector-java</artifactId> 105 <version>5.1.41</version> 106 </dependency> 107 <!-- (jstl,servlet-api,junit) --> 108 <!-- https://mvnrepository.com/artifact/jstl/jstl --> 109 <dependency> 110 <groupId>jstl</groupId> 111 <artifactId>jstl</artifactId> 112 <version>1.2</version> 113 </dependency> 114 115 <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> 116 <dependency> 117 <groupId>javax.servlet</groupId> 118 <artifactId>javax.servlet-api</artifactId> 119 <version>3.0.1</version> 120 <scope>provided</scope> 121 </dependency> 122 123 124 <!-- junit --> 125 <!-- https://mvnrepository.com/artifact/junit/junit --> 126 <dependency> 127 <groupId>junit</groupId> 128 <artifactId>junit</artifactId> 129 <version>4.12</version> 130 </dependency> 131 </dependencies> 132 133 </project>
4)web.xml的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 <!-- 1.启动Spring的容器,需要配置ContextLoaderListener --> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:applicationContext.xml</param-value> 11 </context-param> 12 13 <!-- 2.配置ContextLoaderListener --> 14 <listener> 15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16 </listener> 17 18 <!-- 3.SpringMvc的前端控制器,拦截所有请求 --> 19 <servlet> 20 <servlet-name>dispatcherServlet</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <load-on-startup>1</load-on-startup> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>dispatcherServlet</servlet-name> 26 <url-pattern>*.do</url-pattern> 27 </servlet-mapping> 28 29 <!-- 4.字符编码的过滤器,放在所有的过滤器之前 --> 30 <filter> 31 <filter-name>CharacterEncodingFilter</filter-name> 32 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 33 <init-param> 34 <param-name>encoding</param-name> 35 <param-value>utf-8</param-value> 36 </init-param> 37 <init-param> 38 <param-name>forceRequestEncoding</param-name> 39 <param-value>true</param-value> 40 </init-param> 41 <init-param> 42 <param-name>forceResponseEncoding</param-name> 43 <param-value>true</param-value> 44 </init-param> 45 </filter> 46 <filter-mapping> 47 <filter-name>CharacterEncodingFilter</filter-name> 48 <url-pattern>/*</url-pattern> 49 </filter-mapping> 50 51 </web-app>
5)在WEB-INF下创建一个dispatcherServlet-servlet.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 9 10 <!-- 增加注解扫描 --> 11 <context:component-scan base-package="com.sky.*"/> 12 13 <!-- 注解驱动 --> 14 <mvc:annotation-driven/> 15 16 <!--配置视图解析器,方便页面返回 --> 17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <property name="prefix" value="/WEB-INF/views/"></property> 19 <property name="suffix" value=".jsp"></property> 20 </bean> 21 </beans>
6)在/BMS/src/main/resources下创建几个配置文件
1.applicationContext.xml
2.dbconfig.properties
3.mybatis-config.xml
4.以及一个空的文件夹mapper,用于存放和dao层的接口相映射的xml文件
配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 11 12 <!-- Spring的配置文件 --> 13 <context:property-placeholder location= "classpath:dbconfig.properties" /> 14 <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 16 <property name="driverClass" value="${jdbc.driverClass}"></property> 17 <property name="user" value="${jdbc.user}"></property> 18 <property name="password" value="${jdbc.password}"></property> 19 </bean> 20 21 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 22 <!-- 指定mybatis全局配置文件的位置 --> 23 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 24 <property name="dataSource" ref="pooledDataSource"></property> 25 <!-- 指定mybatis,mapper文件的位置 --> 26 <property name="mapperLocations" value="classpath:mapper/*.xml"></property> 27 </bean> 28 29 <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --> 30 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 31 <!--扫描所有dao接口的实现,加入到ioc容器中 --> 32 <property name="basePackage" value="com.sky.dao"></property> 33 </bean> 34 </beans>
配置dbconfig.properties
1 jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test 2 jdbc.driverClass=com.mysql.jdbc.Driver 3 jdbc.user=root 4 jdbc.password=123456
配置mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <settings> 8 <setting name="mapUnderscoreToCamelCase" value="true"/> 9 </settings> 10 11 <typeAliases> 12 <package name="com.sky.bean"/> 13 </typeAliases> 14 15 <plugins> 16 <plugin interceptor="com.github.pagehelper.PageInterceptor"> 17 <!--分页参数合理化 --> 18 <property name="reasonable" value="true"/> 19 </plugin> 20 </plugins> 21 22 </configuration>
7)创建一个web测试流程
1 package com.sky.beans; 2 3 public class User { 4 private String userId; 5 private String name; 6 private String address; 7 8 public String getUserId() { 9 return userId; 10 } 11 public void setUserId(String userId) { 12 this.userId = userId; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public String getAddress() { 21 return address; 22 } 23 public void setAddress(String address) { 24 this.address = address; 25 } 26 27 }
1 package com.sky.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.sky.beans.User; 11 import com.sky.service.UserService; 12 13 @Controller 14 public class UserController { 15 @Autowired 16 private UserService userService; 17 18 @RequestMapping("user.do") 19 public String user(HttpServletRequest request,HttpServletResponse response){ 20 String userId = request.getParameter("userId"); 21 String name = request.getParameter("name"); 22 String address = request.getParameter("address"); 23 24 User user = new User(); 25 user.setUserId(userId); 26 user.setName(name); 27 user.setAddress(address); 28 29 int i = userService.userService(user); 30 System.out.println(i == 1 ? "添加成功" : "添加失败"); 31 return ""; 32 } 33 34 }
1 package com.sky.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 import com.sky.beans.User; 7 import com.sky.dao.UserMapper; 8 9 @Service 10 public class UserService { 11 @Autowired 12 private UserMapper userMapper; 13 14 public int userService(User user){ 15 return userMapper.doSave(user); 16 } 17 18 }
1 package com.sky.dao; 2 3 import com.sky.beans.User; 4 5 public interface UserMapper { 6 7 public int doSave(User user); 8 9 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.sky.dao.UserMapper"> 4 5 <insert id="doSave" parameterType="com.sky.beans.User" > 6 insert into user values(#{userId},#{name},#{address}) 7 </insert> 8 9 </mapper>
输出结果为:
源代码下载地址:https://pan.baidu.com/s/1jIFUGyq
这只是一个粗糙版的ssm整合,还可以添加许多其他的组件例如bootStrap jQuery等静态资源,以及整合其他实际开发中需要用到的技术和集成个性化技术。