SSM框架整合
Posted sucia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架整合相关的知识,希望对你有一定的参考价值。
SSM框架整合
1,开发环境的搭建
JDK+Eclipse+Tomcat+Maven的安装与配置
参考:http://blog.csdn.net/zhshulin/article/details/30779873
2,Maven Web项目创建
创建一个Maven项目来配置环境
参考:http://blog.csdn.net/zhshulin/article/details/37921705
3,SSM的整合
2个配置文件:
① spring-mybatis.xml,包含spring和mybatis的配置文件,
②spring-mvc的配置文件,
2个资源文件:
①jdbc.propertis
②log4j.properties
目录结构:
3.1框架中引入的Jar包(pom.xml配置,也可以直接导入包)
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>org.projectManagement</groupId> 5 <artifactId>projectManagement</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>projectManagement Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <!-- spring版本号 --> 13 <spring.version>4.0.2.RELEASE</spring.version> 14 <!-- mybatis版本号 --> 15 <mybatis.version>3.2.6</mybatis.version> 16 <!-- log4j日志文件管理包版本 --> 17 <slf4j.version>1.7.7</slf4j.version> 18 <log4j.version>1.2.17</log4j.version> 19 </properties> 20 <dependencies> 21 <!-- 导入java ee jar 包 --> 22 <dependency> 23 <groupId>org.apache.openejb</groupId> 24 <artifactId>javaee-api</artifactId> 25 <version>6.0-1</version> 26 </dependency> 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>4.11</version> 31 <!-- 表示开发的时候引入,发布的时候不会加载此包 --> 32 <scope>test</scope> 33 </dependency> 34 <!-- spring核心包 --> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-core</artifactId> 38 <version>${spring.version}</version> 39 </dependency> 40 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-web</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-oxm</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-tx</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-jdbc</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-webmvc</artifactId> 66 <version>${spring.version}</version> 67 </dependency> 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-aop</artifactId> 71 <version>${spring.version}</version> 72 </dependency> 73 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-context-support</artifactId> 77 <version>${spring.version}</version> 78 </dependency> 79 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-test</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <!-- mybatis核心包 --> 86 <dependency> 87 <groupId>org.mybatis</groupId> 88 <artifactId>mybatis</artifactId> 89 <version>${mybatis.version}</version> 90 </dependency> 91 <!-- mybatis/spring包 --> 92 <dependency> 93 <groupId>org.mybatis</groupId> 94 <artifactId>mybatis-spring</artifactId> 95 <version>1.2.2</version> 96 </dependency> 97 <!-- 导入mysql数据库链接jar包 --> 98 <dependency> 99 <groupId>mysql</groupId> 100 <artifactId>mysql-connector-java</artifactId> 101 <version>5.1.30</version> 102 </dependency> 103 <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> 104 <dependency> 105 <groupId>commons-dbcp</groupId> 106 <artifactId>commons-dbcp</artifactId> 107 <version>1.2.2</version> 108 </dependency> 109 <!-- JSTL标签类 --> 110 <dependency> 111 <groupId>jstl</groupId> 112 <artifactId>jstl</artifactId> 113 <version>1.2</version> 114 </dependency> 115 <!-- 日志文件管理包 --> 116 <!-- log start --> 117 <dependency> 118 <groupId>log4j</groupId> 119 <artifactId>log4j</artifactId> 120 <version>${log4j.version}</version> 121 </dependency> 122 123 124 <!-- 格式化对象,方便输出日志 --> 125 <dependency> 126 <groupId>com.alibaba</groupId> 127 <artifactId>fastjson</artifactId> 128 <version>1.1.41</version> 129 </dependency> 130 131 132 <dependency> 133 <groupId>org.slf4j</groupId> 134 <artifactId>slf4j-api</artifactId> 135 <version>${slf4j.version}</version> 136 </dependency> 137 138 <dependency> 139 <groupId>org.slf4j</groupId> 140 <artifactId>slf4j-log4j12</artifactId> 141 <version>${slf4j.version}</version> 142 </dependency> 143 <!-- log end --> 144 <!-- 映入JSON --> 145 <dependency> 146 <groupId>org.codehaus.jackson</groupId> 147 <artifactId>jackson-mapper-asl</artifactId> 148 <version>1.9.13</version> 149 </dependency> 150 <!-- 上传组件包 --> 151 <dependency> 152 <groupId>commons-fileupload</groupId> 153 <artifactId>commons-fileupload</artifactId> 154 <version>1.3.1</version> 155 </dependency> 156 <dependency> 157 <groupId>commons-io</groupId> 158 <artifactId>commons-io</artifactId> 159 <version>2.4</version> 160 </dependency> 161 <dependency> 162 <groupId>commons-codec</groupId> 163 <artifactId>commons-codec</artifactId> 164 <version>1.9</version> 165 </dependency> 166 <dependency> 167 <groupId>org.mybatis.generator</groupId> 168 <artifactId>mybatis-generator-maven-plugin</artifactId> 169 <version>1.3.2</version> 170 </dependency> 171 </dependencies> 172 <build> 173 <plugins> 174 <plugin> 175 <groupId>org.apache.maven.plugins</groupId> 176 <artifactId>maven-compiler-plugin</artifactId> 177 <configuration> 178 <source>1.8</source> 179 <target>1.8</target> 180 </configuration> 181 </plugin> 182 </plugins> 183 <finalName>projectManagement</finalName> 184 </build> 185 </project>
3.2、Spring与MyBatis的整合
所有需要的JAR包都引入以后,首先进行Spring与MyBatis的整合,然后再进行JUnit测试,
3.2.1、建立JDBC属性文件(jdbc.properties,文件编码修改为utf-8)
1 #<?xml version="1.0" encoding="UTF-8"?> 2 driver=com.mysql.jdbc.Driver 3 url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8 4 username=用户名 5 password=密码 6 #定义初始连接数 7 initialSize=0 8 #定义最大连接数 9 maxActive=20 10 #定义最大空闲 11 maxIdle=20 12 #定义最小空闲 13 minIdle=1 14 #定义最长等待时间 15 maxWait=60000
3.2.2、建立spring-mybatis.xml配置文件
这个文件就是用来完成spring和mybatis的整合的,主要的就是自动扫描,自动注入,配置数据库。
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" xmlns:p="http://www.springframework.org/schema/p" 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-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 自动扫描 --> 13 <context:component-scan base-package="com.projMgr(包名要改)" /> 14 <!-- 引入配置文件<property resource="classpath:jdbc.properties" /> --> 15 <bean id="propertyConfigurer" 16 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 17 <property name="location" value="classpath:jdbc.properties" /> 18 19 </bean> 20 21 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 22 destroy-method="close"> 23 <property name="driverClassName" value="${driver}" /> 24 <property name="url" value="${url}" /> 25 <property name="username" value="${username}" SSM 项目整合详细解读520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“