SSM整合环境搭建demo

Posted 20158424-hxlz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合环境搭建demo相关的知识,希望对你有一定的参考价值。

1.项目目录结构

技术分享图片

 

2.项目中用的jar包

技术分享图片

3.web.xml(其中主要配置spring与springmvc)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
 3 <web-app id="WebApp_ID">
 4     <!-- spring配置 -->
 5     <context-param>
 6         <param-name>contextConfigLocation</param-name>
 7         <param-value>classpath:applicationContext.xml</param-value>
 8     </context-param>
 9     <listener>
10         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
11     </listener>
12 
13     <!-- springmvc配置 -->
14     <servlet>
15         <servlet-name>springmvc</servlet-name>
16         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
17         <init-param>
18             <param-name>contextConfigLocation</param-name>
19             <param-value>classpath:springMVC-servlet.xml</param-value>
20         </init-param>
21         <load-on-startup>1</load-on-startup>
22     </servlet>
23     <servlet-mapping>
24         <servlet-name>springmvc</servlet-name>
25         <url-pattern>/</url-pattern>
26     </servlet-mapping>
27 </web-app>

 

4.spring配置文件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:tx="http://www.springframework.org/schema/tx"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 8     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 9         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
13    
14     <context:component-scan base-package="com.hxlz">
15         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
16     </context:component-scan>
17     
18     <context:property-placeholder location="classpath:jdbc.properties" />
19     
20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
21         <property name="user" value="${jdbc.user}"></property>
22         <property name="password" value="${jdbc.password}"></property>
23         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
24         <property name="driverClass" value="${jdbc.driverClass}"></property>
25         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
26         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
27     </bean>
28     
29     <bean id="transactionManager"
30         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
31         <property name="dataSource" ref="dataSource"></property>
32     </bean>
33     
34     <tx:annotation-driven transaction-manager="transactionManager" />
35  
36     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
37         <property name="dataSource" ref="dataSource" />
38         <!-- 指定mybatis配置文件 -->
39         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
40     </bean>
41     
42     <mybatis-spring:scan base-package="com.hxlz.mapper"/>
43 </beans>

 

5.springmvc配置文件springMVC-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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 9 
10     <!-- 只扫描标了controller注解的控制器 -->
11      <context:component-scan base-package="com.hxlz" use-default-filters="false">
12         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
13     </context:component-scan>
14 
15     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16         <property name="prefix" value="/WEB-INF/view/"></property>
17         <property name="suffix" value=".jsp"></property>
18     </bean>
19     
20     <mvc:view-controller path="/" view-name="index"/>
21     
22     <mvc:annotation-driven></mvc:annotation-driven>
23     <mvc:default-servlet-handler/>
24 </beans>

 

6.mybatis配置文件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     <settings>
 7         <setting name="mapUnderscoreToCamelCase" value="true" />
 8         <setting name="lazyLoadingEnabled" value="true"/>
 9         <setting name="aggressiveLazyLoading" value="false"/>
10     </settings>
11     
12     <typeAliases>
13         <typeAlias alias="Employee" type="com.hxlz.bean.Employee" />
14     </typeAliases>
15 
16 </configuration>

 

7.具体的业务逻辑的代码就不贴了,整合的环境基本上就是这个样子了。

   内附上mybaits整合spring的官方给的demo:https://github.com/mybatis/jpetstore-6,希望一起成长,交流,学习。

 

以上是关于SSM整合环境搭建demo的主要内容,如果未能解决你的问题,请参考以下文章

SSM 项目整合详细解读

SSM框架整合 环境搭建

SSM 框架 搭建整合(IDEA)保姆级

阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境

ssm整合-开发环境搭建01

ssm整合-开发环境搭建01