SSM Spring+SpringMVC+mybatis+maven环境搭建

1、首先右键点击项目区空白处,选择new-》other..在弹出框中输入maven,选择Maven Project。

默认next下一步

选中图片所选next下一步

输入其中所需参数点击Finish完成创建操作

2、标准的maven结构,但是还需要创建几个文件夹,右键点击src下的main-》new-》Folder填写java,右键点击src new-》Folder填写test,同理在test下创建java folder。

在src/main/java下创建基本的层级,如图:

 

上图中jsp的报错暂时不用管,具体原因尚不清楚,不过不影响项目开发启动,在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   
  4   <modelVersion>4.0.0</modelVersion>  
  5   <groupId>com.cjl.ssm</groupId>  
  6   <artifactId>SSM</artifactId>  
  7   <packaging>war</packaging>  
  8   <version>1.0-SNAPSHOT</version>  
  9   <name>SSM</name>  
 10   
 11   <!-- 编码 -->  
 12   <properties>  
 13     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
 14     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
 15   </properties>  
 16   
 17   <!--Maven依赖 需要更换可以到http://mvnrepository.com进行查询-->  
 18   <dependencies>  
 19   
 20     <!--Spring-->  
 21     <dependency>  
 22       <groupId>org.springframework</groupId>  
 23       <artifactId>spring-core</artifactId>  
 24       <version>4.3.5.RELEASE</version>  
 25     </dependency>  
 26     <dependency>  
 27       <groupId>org.springframework</groupId>  
 28       <artifactId>spring-aop</artifactId>  
 29       <version>4.3.5.RELEASE</version>  
 30     </dependency>  
 31     <dependency>  
 32       <groupId>org.springframework</groupId>  
 33       <artifactId>spring-orm</artifactId>  
 34       <version>4.3.5.RELEASE</version>  
 35     </dependency>  
 36     <dependency>  
 37       <groupId>org.springframework</groupId>  
 38       <artifactId>spring-web</artifactId>  
 39       <version>4.3.5.RELEASE</version>  
 40     </dependency>  
 41     <dependency>  
 42       <groupId>org.springframework</groupId>  
 43       <artifactId>spring-webmvc</artifactId>  
 44       <version>4.3.5.RELEASE</version>  
 45     </dependency>  
 46     <dependency>  
 47       <groupId>org.springframework</groupId>  
 48       <artifactId>spring-tx</artifactId>  
 49       <version>4.3.5.RELEASE</version>  
 50     </dependency>  
 51     <dependency>  
 52       <groupId>org.springframework</groupId>  
 53       <artifactId>spring-test</artifactId>  
 54       <version>4.3.5.RELEASE</version>  
 55     </dependency>  
 56     <dependency>  
 57       <groupId>org.springframework</groupId>  
 58       <artifactId>spring-jdbc</artifactId>  
 59       <version>4.3.5.RELEASE</version>  
 60     </dependency>  
 61     <dependency>  
 62       <groupId>org.springframework</groupId>  
 63       <artifactId>spring-mock</artifactId>  
 64       <version>2.0.8</version>  
 65     </dependency>  
 66     <dependency>  
 67       <groupId>org.springframework</groupId>  
 68       <artifactId>spring-context</artifactId>  
 69       <version>4.3.5.RELEASE</version>  
 70     </dependency>  
 71     <dependency>  
 72       <groupId>org.springframework</groupId>  
 73       <artifactId>spring-expression</artifactId>  
 74       <version>4.3.5.RELEASE</version>  
 75     </dependency>  
 76     <dependency>  
 77       <groupId>org.springframework</groupId>  
 78       <artifactId>spring-context-support</artifactId>  
 79       <version>4.3.5.RELEASE</version>  
 80     </dependency>  
 81   
 82     <!-- mybatis-spring -->  
 83     <dependency>  
 84       <groupId>org.mybatis</groupId>  
 85       <artifactId>mybatis-spring</artifactId>  
 86       <version>1.3.1</version>  
 87     </dependency>  
 88   
 89     <!-- mybatis 出现分页使用异常,极有可能是版本问题-->  
 90     <dependency>  
 91       <groupId>org.mybatis</groupId>  
 92       <artifactId>mybatis</artifactId>  
 93       <version>3.2.8</version>  
 94     </dependency>  
 95     <!-- mybatis pagehelper -->  
 96     <dependency>  
 97       <groupId>com.github.pagehelper</groupId>  
 98       <artifactId>pagehelper</artifactId>  
 99       <version>3.7.3</version>  
100     </dependency>  
101     <dependency>  
102       <groupId>com.github.jsqlparser</groupId>  
103       <artifactId>jsqlparser</artifactId>  
104       <version>0.9.1</version>  
105     </dependency>  
106   
107     <!--servlet、基本包-->  
108     <dependency>  
109       <groupId>javax.servlet</groupId>  
110       <artifactId>servlet-api</artifactId>  
111       <version>3.0-alpha-1</version>  
112     </dependency>  
113     <dependency>  
114       <groupId>javax.servlet</groupId>  
115       <artifactId>jstl</artifactId>  
116       <version>1.2</version>  
117     </dependency>  
118     <dependency>  
119       <groupId>taglibs</groupId>  
120       <artifactId>standard</artifactId>  
121       <version>1.1.2</version>  
122     </dependency>  
123     <dependency>  
124       <groupId>org.aspectj</groupId>  
125       <artifactId>aspectjrt</artifactId>  
126       <version>1.8.10</version>  
127     </dependency>  
128     <dependency>  
129       <groupId>org.aspectj</groupId>  
130       <artifactId>aspectjweaver</artifactId>  
131       <version>1.8.10</version>  
132     </dependency>  
133     <dependency>  
134       <groupId>commons-lang</groupId>  
135       <artifactId>commons-lang</artifactId>  
136       <version>2.6</version>  
137     </dependency>  
138     <dependency>  
139       <groupId>dom4j</groupId>  
140       <artifactId>dom4j</artifactId>  
141       <version>1.6.1</version>  
142     </dependency>  
143   
144     <!--mysql驱动包-->  
145     <dependency>  
146       <groupId>mysql</groupId>  
147       <artifactId>mysql-connector-java</artifactId>  
148       <version>5.1.40</version>  
149     </dependency>  
150     <!--dbcp连接池-->  
151     <dependency>  
152       <groupId>commons-dbcp</groupId>  
153       <artifactId>commons-dbcp</artifactId>  
154       <version>1.4</version>  
155     </dependency>  
156   
157     <!--json、基本包-->  
158     <dependency>  
159       <groupId>com.alibaba</groupId>  
160       <artifactId>fastjson</artifactId>  
161       <version>1.2.23</version>  
162     </dependency>  
163     <dependency>  
164       <groupId>xml-apis</groupId>  
165       <artifactId>xml-apis</artifactId>  
166       <version>2.0.2</version>  
167     </dependency>  
168     <dependency>  
169       <groupId>commons-net</groupId>  
170       <artifactId>commons-net</artifactId>  
171       <version>3.5</version>  
172     </dependency>  
173   
174     <!-- 日志文件管理包 -->  
175     <dependency>  
176       <groupId>org.slf4j</groupId>  
177       <artifactId>slf4j-api</artifactId>  
178       <version>1.7.22</version>  
179     </dependency>  
180     <dependency>  
181       <groupId>org.slf4j</groupId>  
182       <artifactId>slf4j-log4j12</artifactId>  
183       <version>1.7.22</version>  
184     </dependency>  
185     <dependency>  
186       <groupId>log4j</groupId>  
187       <artifactId>log4j</artifactId>  
188       <version>1.2.17</version>  
189     </dependency>  
190   
191     <!-- junit4 -->  
192     <dependency>  
193       <groupId>junit</groupId>  
194       <artifactId>junit</artifactId>  
195       <version>4.12</version>  
196       <scope>test</scope>  
197     </dependency>  
198   
199   </dependencies>  
200   
201   <!--项目编译及其构建-->  
202   <build>  
203     <plugins>  
204       <plugin>  
205         <groupId>org.apache.maven.plugins</groupId>  
206         <artifactId>maven-war-plugin</artifactId>  
207         <version>2.4</version>  
208         <configuration>  
209           <warName>SSM</warName>  
210           <!-- <packagingExcludes>  
211           **/resources/**  
212       </packagingExcludes> -->  
213           <webResources>  
214             <resource>  
215               <directory>src/main/resources/resources/config</directory>  
216               <targetPath>WEB-INF/classes/resources/config</targetPath>  
217               <filtering>true</filtering>  
218             </resource>  
219             <resource>  
220               <directory>src/main/java</directory>  
221               <includes>  
222                 <include>**/*.xml</include>  
223               </includes>  
224               <targetPath>WEB-INF/classes</targetPath>  
225             </resource>  
226           </webResources>  
227         </configuration>  
228       </plugin>  
229   <plugin>  
230       <groupId>org.mybatis.generator</groupId>  
231       <artifactId>mybatis-generator-maven-plugin</artifactId>  
232       <version>1.3.2</version>  
233       <configuration>  
234         <verbose>true</verbose>  
235         <overwrite>true</overwrite>  
236       </configuration>  
237     </plugin>
238       <plugin>  
239         <groupId>org.apache.maven.plugins</groupId>  
240         <artifactId>maven-resources-plugin</artifactId>  
241         <version>2.4.3</version>  
242         <configuration>  
243           <encoding>UTF-8</encoding>  
244         </configuration>  
245       </plugin>  
246       <plugin>  
247         <groupId>org.apache.maven.plugins</groupId>  
248         <artifactId>maven-compiler-plugin</artifactId>  
249       </plugin>  
250       <plugin>  
251         <groupId>org.apache.felix</groupId>  
252         <artifactId>maven-bundle-plugin</artifactId>  
253         <inherited>true</inherited>  
254         <extensions>true</extensions>  
255       </plugin>  
256       <plugin>  
257         <groupId>org.apache.maven.plugins</groupId>  
258         <artifactId>maven-surefire-plugin</artifactId>  
259         <configuration>  
260           <testFailureIgnore>true</testFailureIgnore>  
261         </configuration>  
262       </plugin>  
263       <plugin>  
264         <groupId>org.apache.maven.plugins</groupId>  
265         <artifactId>maven-site-plugin</artifactId>  
266         <!-- 此处由3.2升级为3.3.原因是maven版本3.1的,需要3,3才能兼容.否则会报错:java.lang.NoClassDefFoundError:  
267             org/sonatype/aether/graph/DependencyFilter -->  
268         <version>3.3</version>  
269         <configuration>  
270           <locales>zh_CN</locales>  
271         </configuration>  
272       </plugin>  
273   
274     </plugins>  
275   </build>  
276   
277 </project> 

mavenjar包下载完成后需要在resources文件夹下添加几个配置文件

applicationContext-mybatis.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" 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" xmlns:tx="http://www.springframework.org/schema/tx"  
 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/mvc   
 9         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
10         http://www.springframework.org/schema/context   
11         http://www.springframework.org/schema/context/spring-context.xsd  
12         http://www.springframework.org/schema/tx    
13         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
14   
15     <!-- Mybatis 和 Spring的整合 -->  
16     <!-- 1.数据源:DriverManagerDataSource -->  
17     <bean id="dataSource"  
18         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
19         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
20         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
21         <property name="username" value="root"></property>  
22         <property name="password" value="123456"></property>  
23     </bean>  
24   
25     <!-- 2.Mybatis 的 SqlSession的工厂:SqlSessionFactoryBean dataSource引用数据源 Mybatis   
26         定义数据源,同意加载配置 -->  
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
28         <property name="dataSource" ref="dataSource"></property>  
29         <property name="configLocation" value="classpath:mybatis-config.xml"></property>  
30     </bean>  
31   
32     <!-- 3. Mybatis自动扫描加载Sql映射文件/接口:MapperScannerConfigurer sqlSessionFactory   
33         basePackage:指定sql映射文件/接口所在的包(自动扫描) -->  
34     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
35         <property name="basePackage" value="com.cjl.mapper"></property>  
36         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
37     </bean>  
38   
39     <!-- 4.事务管理:DataSourceTransactionManager dataSource 引用上面定义好的数据源 -->  
40     <bean id="txManager"  
41         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
42         <property name="dataSource" ref="dataSource"></property>  
43     </bean>  
44   
45     <!-- 5.使用声明式事务: transaction-manager = "txManager" tx:advice 这种 是用 aop方式管理事物   
46         annotation-driven 这种是注解方式管理事物 第一种方式,需要在spring配置文件配置一些参数 第二种方式,需要在 类里 加一些注解进行事物管理   
47         用一种就行,没必须都用 -->  
48     <tx:annotation-driven transaction-manager="txManager" />  
49   
50 </beans>  

generatorConfig.xml 这个xml主要是为了使用mybatis生成代码用

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
 3 <generatorConfiguration>  
 4       
 5     <!-- <properties resource="init.properties"/> -->  
 6       
 7     <!-- 指定数据库连接驱动地址 -->  
 8     <classPathEntry  
 9         location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />  
10     <!-- 一个数据库一个 context -->  
11     <context id="context1">  
12       
13         <commentGenerator>  
14             <!-- 是否取消注释 -->  
15             <property name="suppressAllComments" value="true"></property>  
16             <!-- 是否生成注释代时间戳-->  
17             <property name="suppressDate" value="true"></property>  
18         </commentGenerator>  
19         <!-- jdbc连接配置 -->  
20         <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test" driverClass="com.mysql.jdbc.Driver" password="123456" userId="root"/>  
21               
22         <!-- 类型转换 -->    
23         <javaTypeResolver>    
24             <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->    
25             <property name="forceBigDecimals" value="false"/>    
26         </javaTypeResolver>    
27           
28         <!-- 生成实体类的地址 -->  
29         <javaModelGenerator targetPackage="com.cjl.model"  
30             targetProject="D:\eclipse-workspace_lt\ssm\src\main\java" />  
31               
32         <!-- 生成mapper.xml文件 -->  
33         <sqlMapGenerator targetPackage="com.cjl.mapper"  
34             targetProject="D:\eclipse-workspace_lt\ssm\src\main\resources" />  
35           
36         <!-- 生成mapxml对应的client 也就是接口dao -->  
37         <javaClientGenerator targetPackage="com.cjl.mapper"  
38             targetProject="D:\eclipse-workspace_lt\ssm\src\main\java" type="XMLMAPPER" />  
39               
40         <table schema="test" tableName="tb_department"  domainObjectName="Department" enableCountByExample="false"    
41             enableDeleteByExample="false" enableSelectByExample="false"    
42             enableUpdateByExample="false">  
43             <!-- domainObjectName  指定生成的类的名字 -->  
44         </table>  
45     </context>  
46 </generatorConfiguration>  

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     <!-- 配置打印 SQL 到控制台 以及懒加载 -->  
 7     <settings>  
 8         <setting name="logImpl" value="STDOUT_LOGGING" />  
 9         <!-- 打开延迟加载的全局开关 -->  
10         <setting name="lazyLoadingEnabled" value="true" />  
11         <setting name="aggressiveLazyLoading" value="false" />  
12         <setting name="mapUnderscoreToCamelCase" value="true" />  
13     </settings>  
14     <!-- 为org.bb.ssm.mode.UserInfo 设置一个别名 UserInfo -->  
15     <typeAliases>  
16         <typeAlias type="com.cjl.model.User" alias="User"/>  
17     </typeAliases>  
18     <mappers>  
19        <package name="com.cjl.mapper"/>
20     </mappers>  
21 </configuration> 

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" 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" xmlns:tx="http://www.springframework.org/schema/tx"  
 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/mvc   
 9         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
10         http://www.springframework.org/schema/context   
11         http://www.springframework.org/schema/context/spring-context.xsd  
12         http://www.springframework.org/schema/tx    
13         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
14     <!-- 开启注解 -->  
15     <mvc:annotation-driven/>  
16     <!-- 配置自定扫描包 -->  
17     <context:component-scan base-package="com.cjl.controller"/>  
18     <context:component-scan base-package="com.cjl.service.Impl"/>  
19       
20     <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->  
21     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
22         <property name="prefix" value="/WEB-INF/view/"></property>  
23         <property name="suffix" value=".jsp"></property>  
24     </bean>  
25     <!-- 处理静态资源 -->  
26     <!-- <mvc:default-servlet-handler/>  --> 
27     <mvc:resources mapping="/vendor/**" location="/vendor/" />
28     <mvc:resources mapping="/dist/**" location="/dist/" />
29 </beans>

3、接下来创建一张表,已完成代码测试以及本次ssm框架搭建工作

1 CREATE TABLE `user` (
2   `user_id` varchar(10) NOT NULL default '',
3   `user_name` varchar(50) default NULL,
4   `user_age` varchar(50) default NULL,
5   `department_id` int(10) default NULL,
6   PRIMARY KEY  (`user_id`)
7 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

创建好后需要在generatorConfig.xml生成代码配置文件中修改好适合自己项目的配置方法,右键点击项目选择Run as->Maven Build..->在goals中填写:mybatis-generator:generate -e 点击run

当看到构建成功后即可刷新项目

可以看到代码已经生成完成!!将项目加入tomcat启动

可以看到项目部署已经完成!

 

posted on 2017-11-25 16:50  cui5445  阅读(13085)  评论(2编辑  收藏  举报