使⽤IDEA搭建基于SSM(springmvc+spring+mybatis)的
WEB项⽬
前⾔
本博客的搭建SSM的WEB项⽬内容前提是需要⾃⼰先使⽤IDEA⾃⾏创建基于Maven的WEB⼯程或根据本⼈的另⼀篇博客: 进⾏基于Maven的WEB⼯程创建!
搭建SSM项⽬
1.在创建的Web项⽬的l⽂件中添加SSM的web项⽬依赖,l⽂件中的依赖如下,版本可⾃⾏调整,第⼀次下载依赖可能会稍慢,等待下载完毕即可执⾏下⾯的搭建步骤!
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.servlet.jsp</groupId>-->
<!--<artifactId>jsp-api</artifactId>-->
<!--<version>2</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId&hange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<!-- Spring整合MyBatis -->
<!-- MyBatis中延迟加载需要使⽤Cglib -->
<!-- mvnrepository/batis/mybatis -->        <dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 控制⽇志输出:结合log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
mvc的controller
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- ********其他****************************** -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId&dehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
2.在src/main/⽬录下,创建java和resources两个⽂件夹,并在设置其为对应的sources和resources资源⽂件夹
3.在 src\main\webapp\l ⽂件中集成配置spring和springmvc框架
<!-- 集成Spring框架 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-*.xml</param-value>
</context-param>
<listener>
<!-- 在项⽬启动前初始化当前web应⽤环境 -->
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器,可以保证项⽬不乱码 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
<!-- 集成SpringMVC框架 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:l</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.将上述内容添加到l⽂件中会有两处报红,需要在src/main/resources/⽬录下创建spring⽂件夹,在⽂件夹中再创建spring和springmvc对应的xml配置⽂件
5.在第4步创建好的 l⽂件中添加以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:tx="/schema/tx"
xmlns:aop="/schema/aop"
xsi:schemaLocation="/schema/aop /schema/aop/spring-aop-4.0.xsd
/schema/beans /schema/beans/spring-beans.xsd
/schema/tx /schema/tx/spring-tx-4.0.xs
d
/schema/context /schema/context/spring-context-4.0.xsd">
<!-- 以下IOC配置:创建对象组合对象之间的关系 -->
<!-- 扫描注解类,需要扫描除springmvc组件controller外的所有组件 -->
<context:component-scan base-package="com.hlk.*" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="hange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="sql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://hadoop01:3306/ct?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8"/>        <property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 集成Mybatis框架 -->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean" >
<property name="configLocation" value="classpath:l" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" >
<list>
<value>classpath*:mybatis/mapper-*.xml</value>
</list>
</property>
</bean>
<bean id="mapperScannerConfigurer" class="batis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.hlk.web.dao" />
</bean>
<!-- AOP配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- AOP功能增强器 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- spring默认的回滚策略为java.lang.RuntimeException -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.hlk..*Service.*(..))"/>
</aop:config>
</beans>
添加以上内容后,会有⼏处报红,需要创建对应dao、service层对应的package(根据个⼈项⽬修改对应的包名即可),并且在
src/main/resources/⽬录下创建mybatis⽂件夹,并在该⽂件夹下⾯创建mybatis的配置⽂件l。上述配置⽂件中我所创建的包
如下图所⽰:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。