SSM框架—Spring+SpringMVC+MyBatis
1、环境搭建
1.1概念
Spring是一个Java应用的开源框架,Bean/Context/Core/IOC/AOP/MVC等是其重要组件,IOC控制反转,AOP面向切面编程,各种注入方式,实现方式不再一一列举;
SpringMVC是一个基于请求的Web框架,模型视图控制器,涉及的控制器解析器处理器以及流程,不再一一列举;
Mybatis是一个基于原型ibatis的Java持久层框架,消除jdbc代码,仅通过简单的配置与映射,通过接口实现与数据库的交互;
1.2环境
JDK:从sun公司官网下载jdk,安装,配置环境变量(新建变量名:JAVA_HOME 变量值:(例如)E:\Java\jdk1.6.0_43;编辑变量名:Path 在后面加上:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin),cmd中输入java -version测试;
Tomcat:从apache官网下载Tomcat,安装,不配置环境变量;
Maven:从Maven官网下载Maven,安装或者解压,配置环境变量( 新建变量名:MAVEN_HOME 变量值:(例如)D:\server\apache-maven-3.2.1;编辑变量名:Path 在最前面加上:%MAVEN_HOME%\bin;)
eclipse或者idea编辑器;
2、项目创建
idea创建Maven项目
2.1创建项目
groupId为项目组织id或者编码等
artifactID为项目的Id或者编码等
3、配置整合
3.1 Maven引入需要的JAR包
修改pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.gennetsowa</groupId> 8 <artifactId>gennet</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>gennet Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.7</maven.compiler.source> 19 <maven.compiler.target>1.7</maven.compiler.target> 20 21 <!-- 设置项目编码编码 --> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <!-- spring版本号 --> 25 <spring.version>4.3.5.RELEASE</spring.version> 26 <!-- mybatis版本号 --> 27 <mybatis.version>3.4.1</mybatis.version> 28 </properties> 29 30 <dependencies> 31 <dependency> 32 <groupId>javax.servlet</groupId> 33 <artifactId>servlet-api</artifactId> 34 <version>2.5</version> 35 <scope>provided</scope> 36 </dependency> 37 38 <!-- 单元测试框架 --> 39 <dependency> 40 <groupId>junit</groupId> 41 <artifactId>junit</artifactId> 42 <version>4.11</version> 43 <scope>test</scope> 44 </dependency> 45 46 <!-- spring核心包 --> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-core</artifactId> 50 <version>${spring.version}</version> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-beans</artifactId> 55 <version>${spring.version}</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-context</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-jdbc</artifactId> 65 <version>${spring.version}</version> 66 </dependency> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-tx</artifactId> 70 <version>${spring.version}</version> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-web</artifactId> 75 <version>${spring.version}</version> 76 </dependency> 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-webmvc</artifactId> 80 <version>${spring.version}</version> 81 </dependency> 82 <dependency> 83 <groupId>org.springframework</groupId> 84 <artifactId>spring-test</artifactId> 85 <version>${spring.version}</version> 86 </dependency> 87 88 <!-- mybatis核心包 --> 89 <dependency> 90 <groupId>org.mybatis</groupId> 91 <artifactId>mybatis</artifactId> 92 <version>${mybatis.version}</version> 93 </dependency> 94 95 <!-- mybatis/spring整合包 --> 96 <dependency> 97 <groupId>org.mybatis</groupId> 98 <artifactId>mybatis-spring</artifactId> 99 <version>1.3.1</version> 100 </dependency> 101 102 <!-- 导入Mysql数据库链接jar包 --> 103 <dependency> 104 <groupId>mysql</groupId> 105 <artifactId>mysql-connector-java</artifactId> 106 <version>5.1.41</version> 107 </dependency> 108 109 <!-- 分页 --> 110 <dependency> 111 <groupId>com.github.pagehelper</groupId> 112 <artifactId>pagehelper</artifactId> 113 <version>4.0.3</version> 114 </dependency> 115 116 <!-- druid 数据库连接池--> 117 <dependency> 118 <groupId>com.alibaba</groupId> 119 <artifactId>druid</artifactId> 120 <version>1.0.2</version> 121 </dependency> 122 123 <!-- 日志--> 124 <dependency> 125 <groupId>org.slf4j</groupId> 126 <artifactId>slf4j-api</artifactId> 127 <version>1.7.25</version> 128 </dependency> 129 <!-- Logback相关 --> 130 <dependency> 131 <groupId>ch.qos.logback</groupId> 132 <artifactId>logback-core</artifactId> 133 <version>1.2.3</version> 134 </dependency> 135 <dependency> 136 <groupId>ch.qos.logback</groupId> 137 <artifactId>logback-classic</artifactId> 138 <version>1.2.3</version> 139 </dependency> 140 <dependency> 141 <groupId>org.logback-extensions</groupId> 142 <artifactId>logback-ext-spring</artifactId> 143 <version>0.1.2</version> 144 </dependency> 145 146 <!--json--> 147 <dependency> 148 <groupId>net.sf.json-lib</groupId> 149 <artifactId>json-lib</artifactId> 150 <version>2.2.3</version> 151 <classifier>jdk15</classifier> 152 </dependency> 153 154 <!-- jackson --> 155 <dependency> 156 <groupId>com.fasterxml.jackson.core</groupId> 157 <artifactId>jackson-databind</artifactId> 158 <version>2.5.0</version> 159 </dependency> 160 161 </dependencies> 162 163 <build> 164 <finalName>gennet</finalName> 165 <resources> 166 <!--表示把java目录下的有关xml文件,properties文件编译/打包的时候放在resource目录下--> 167 <resource> 168 <directory>${basedir}/src/main/java</directory> 169 <includes> 170 <include>**/*.properties</include> 171 <include>**/*.xml</include> 172 </includes> 173 <filtering>false</filtering> 174 </resource> 175 <resource> 176 <directory>${basedir}/src/main/resources</directory> 177 </resource> 178 </resources> 179 180 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 181 <plugins> 182 <!--servlet容器 jetty插件--> 183 <plugin> 184 <groupId>org.eclipse.jetty</groupId> 185 <artifactId>jetty-maven-plugin</artifactId> 186 <version>9.3.10.v20160621</version> 187 </plugin> 188 <plugin> 189 <artifactId>maven-clean-plugin</artifactId> 190 <version>3.0.0</version> 191 </plugin> 192 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 193 <plugin> 194 <artifactId>maven-resources-plugin</artifactId> 195 <version>3.0.2</version> 196 </plugin> 197 <plugin> 198 <artifactId>maven-compiler-plugin</artifactId> 199 <version>3.7.0</version> 200 </plugin> 201 <plugin> 202 <artifactId>maven-surefire-plugin</artifactId> 203 <version>2.20.1</version> 204 </plugin> 205 <plugin> 206 <artifactId>maven-war-plugin</artifactId> 207 <version>3.2.0</version> 208 </plugin> 209 <plugin> 210 <artifactId>maven-install-plugin</artifactId> 211 <version>2.5.2</version> 212 </plugin> 213 <plugin> 214 <artifactId>maven-deploy-plugin</artifactId> 215 <version>2.8.2</version> 216 </plugin> 217 </plugins> 218 </pluginManagement> 219 </build> 220 </project>
新建jdbc.properties
1 dbpurchase.jdbc.url=jdbc:mysql://10.10.10.10:3306/xunda?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true 2 dbpurchase.jdbc.username=root 3 dbpurchase.jdbc.password=123456 4 5 mysql.driver=com.mysql.jdbc.Driver 6 #初始化连接数量 7 jdbc.initialSize = 5 8 #最大并发连接数 9 jdbc.maxActive = 10 10 #最小空闲连接数 11 jdbc.minIdle = 3 12 #最大空闲连接数 13 jdbc.maxIdle = 30 14 #配置获取连接等待超时的时间 15 jdbc.maxWait = 60000 16 #超过时间限制是否回收 17 jdbc.removeAbandoned = true 18 #超过时间限制多长 19 jdbc.removeAbandonedTimeout = 180 20 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 21 jdbc.timeBetweenEvictionRunsMillis = 60000 22 #配置一个连接在池中最小生存的时间,单位是毫秒 23 jdbc.minEvictableIdleTimeMillis = 300000 24 #用来检测连接是否有效的sql,要求是一个查询语句 25 jdbc.validationQuery = SELECT 1 FROM DUAL 26 #申请连接的时候检测 27 jdbc.testWhileIdle = true 28 #申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 29 jdbc.testOnBorrow = false 30 #归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 31 jdbc.testOnReturn = false 32 #打开PSCache,并且指定每个连接上PSCache的大小 33 jdbc.poolPreparedStatements = true 34 jdbc.maxPoolPreparedStatementPerConnectionSize = 50 35 #属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 36 # 监控统计用的filter:stat 37 # 日志用的filter:log4j 38 # 防御SQL注入的filter:wall 39 jdbc.filters = mergeStat
新建spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/tx 8 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 9 10 <!-- 数据源相关 --> 11 <bean name="dBPurchase" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 12 <property name="driverClassName" value="${mysql.driver}" /> 13 <property name="url" value="${dbpurchase.jdbc.url}" /> 14 <property name="username" value="${dbpurchase.jdbc.username}" /> 15 <property name="password" value="${dbpurchase.jdbc.password}" /> 16 <property name="initialSize" value="${jdbc.initialSize}" /> 17 <property name="maxActive" value="${jdbc.maxActive}" /> 18 <property name="minIdle" value="${jdbc.minIdle}" /> 19 <property name="maxIdle" value="${jdbc.maxIdle}" /> 20 <property name="maxWait" value="${jdbc.maxWait}" /> 21 <property name="validationQuery" value="${jdbc.validationQuery}" /> 22 <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> 23 <property name="testOnReturn" value="${jdbc.testOnReturn}" /> 24 <property name="testWhileIdle" value="${jdbc.testWhileIdle}" /> 25 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> 26 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> 27 <property name="removeAbandoned" value="${jdbc.removeAbandoned}" /> 28 <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" /> 29 <property name="logAbandoned" value="true" /> 30 <property name="filters" value="${jdbc.filters}" /> 31 </bean> 32 33 <!-- mybatis文件配置,扫描所有mapper文件 --> 34 <bean id="dBPurchaseSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dBPurchase" 35 p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:mapper/*.xml"> 36 </bean> 37 38 <bean id="dBPurchaseMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 39 <property name="basePackage" value="repository.mapper.*" /> 40 <property name="sqlSessionFactoryBeanName" value="dBPurchaseSqlSessionFactory" /> 41 </bean> 42 43 <bean id="dBPurchaseSqlSessionTemplete" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> 44 <constructor-arg index="0" ref="dBPurchaseSqlSessionFactory" /> 45 </bean> 46 47 <!-- 对数据源进行事务管理 --> 48 <bean id="dBPurchaseTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 49 p:dataSource-ref="dBPurchase" /> 50 51 <!--注解事务声明 --> 52 <tx:annotation-driven proxy-target-class="true" transaction-manager="dBPurchaseTransactionManager" /> 53 54 </beans>
新建spring-mvc.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/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 12 13 <!-- 扫描web相关的bean --> 14 <context:component-scan base-package="你项目的地址"/> 15 16 <!-- 开启SpringMVC注解模式 --> 17 <mvc:annotation-driven/> 18 19 <!-- 静态资源默认servlet配置 --> 20 <mvc:default-servlet-handler/> 21 22 <!-- 定义跳转的文件的前后缀 ,视图模式配置--> 23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 24 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 25 <property name="prefix" value="/WEB-INF/views/"/> 26 <property name="suffix" value=".jsp"/> 27 </bean> 28 29 <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> 30 <bean id="multipartResolver" 31 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 32 <!-- 默认编码 --> 33 <property name="defaultEncoding" value="utf-8" /> 34 <!-- 文件大小最大值 --> 35 <property name="maxUploadSize" value="10485760000" /> 36 <!-- 内存中的最大值 --> 37 <property name="maxInMemorySize" value="40960" /> 38 </bean> 39 40 </beans>
新建logback.cml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 3 scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 4 debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 --> 5 <configuration debug="false" scan="true" scanPeriod="60 seconds"> 6 <!-- 上下文变量设置,用来定义变量值,其中name的值是变量的名称,value的值时变量定义的值。 7 通过<property>定义的值会被插入到logger上下文中。定义变量后,可以使“${}”来使用变量。 --> 8 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 9 <!-- 对日志进行格式化。 --> 10 <encoder> 11 <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level [%file:%line] - %msg%n</pattern> 12 </encoder> 13 </appender> 14 15 16 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 17 <!-- 被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。 --> 18 <Prudent>true</Prudent> 19 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 20 <FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern> 21 <!-- keep 60 days worth of history --> 22 <MaxHistory>60</MaxHistory> 23 </rollingPolicy> 24 25 <layout class="ch.qos.logback.classic.PatternLayout"> 26 <Pattern> 27 %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger [%file:%line] [%X{logid}] >> %msg%n 28 </Pattern> 29 </layout> 30 </appender> 31 32 <root level="INFO"> 33 <appender-ref ref="STDOUT" /> 34 <appender-ref ref="FILE" /> 35 </root> 36 </configuration>
修改web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 6 version="3.0"> 7 8 <!--welcome pages--> 9 <welcome-file-list> 10 <welcome-file>/WEB-INF/statics/views/home.html</welcome-file> 11 </welcome-file-list> 12 13 <!-- Logback相关 --> 14 <!-- Logback配置 --> 15 <context-param> 16 <param-name>logbackConfigLocation</param-name> 17 <param-value>WEB-INF/logback.xml</param-value> 18 </context-param> 19 <!-- Logback监听器 --> 20 <listener> 21 <listener-class> 22 ch.qos.logback.ext.spring.web.LogbackConfigListener 23 </listener-class> 24 </listener> 25 26 <!-- Spring上下文配置 --> 27 <context-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:spring-conf.xml</param-value> 30 </context-param> 31 <listener> 32 <listener-class> 33 org.springframework.web.context.ContextLoaderListener 34 </listener-class> 35 </listener> 36 37 <!--配置springmvc DispatcherServlet--> 38 <servlet> 39 <servlet-name>springMVC</servlet-name> 40 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 41 <init-param> 42 <!--Sources标注的文件夹下需要新建一个spring文件夹--> 43 <param-name>contextConfigLocation</param-name> 44 <param-value>classpath:spring-mvc.xml</param-value> 45 </init-param> 46 <load-on-startup>1</load-on-startup> 47 <async-supported>true</async-supported> 48 </servlet> 49 50 <servlet-mapping> 51 <servlet-name>springMVC</servlet-name> 52 <url-pattern>/</url-pattern> 53 </servlet-mapping> 54 55 <servlet-mapping> 56 <servlet-name>default</servlet-name> 57 <url-pattern>*.html</url-pattern> 58 </servlet-mapping> 59 </web-app>
新建spring-conf.xml
1 <!-- 属性配置 --> 2 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 3 <property name="locations"> 4 <list> 5 <value>/WEB-INF/jdbc.properties</value> 6 </list> 7 </property> 8 </bean> 9 10 <!-- 导入配置 --> 11 <!-- 数据仓库配置 --> 12 <import resource="classpath:spring-repository.xml" />