spring-springmvc-mybatis(ssm)maven框架整合教程

本文详细介绍了如何使用Spring、SpringMVC和MyBatis(SSM)搭建一个完整的项目框架,包括Maven项目的创建、相关依赖引入、Spring配置、SpringMVC配置、MyBatis配置及数据库连接池配置等内容。
摘要由CSDN通过智能技术生成

第一步,新建maven项目
下图先看下整体项目结构
这里写图片描述

1.先加入相关jar包依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>ssm</groupId>
  <artifactId>ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
		<comLog.version>1.1.1</comLog.version>
		<slf4j.version>1.6.1</slf4j.version>
		<jackson.version>1.9.13</jackson.version>
		<mybatis.version>3.2.6</mybatis.version>
		<spring.version>4.0.2.RELEASE</spring.version>
   </properties>
  
   <dependencies>
		  <dependency>  
		      <groupId>com.fasterxml.jackson.core</groupId>  
		      <artifactId>jackson-core</artifactId>  
		      <version>2.1.0</version>  
		  </dependency>  
		  <dependency>  
		      <groupId>com.fasterxml.jackson.core</groupId>  
		      <artifactId>jackson-databind</artifactId>  
		      <version>2.1.0</version>  
		  </dependency>  
		  <dependency>  
		      <groupId>com.fasterxml.jackson.core</groupId>  
		      <artifactId>jackson-annotations</artifactId>  
		      <version>2.1.0</version>  
		  </dependency>
   
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier><!--指定jdk版本 -->
		</dependency>
	 	<dependency>
	 		<groupId>commons-logging</groupId>
	 		<artifactId>commons-logging</artifactId>
	 		<version>${comLog.version}</version>
	 	</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-asl</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-lgpl</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-jaxrs</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-lgpl</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-smile</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-xc</artifactId>
			<version>${jackson.version}</version>
		</dependency>		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.10</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>servlet-api-2.5</artifactId>
			<version>6.1.14</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>${mybatis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.1</version>
		</dependency>
	    <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.8</version>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.16</version>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>2.7</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
	</dependencies>
  
   <build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2.配置spring文件及事务(applicationContext-beans.xml,applicationContext-transaction.xml)
####applicationContext-beans.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "
>
	<context:component-scan base-package="com.cpt">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:conf/*.properties</value>
			</list>
		</property>
	</bean>
	
	<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="properties" ref="properties"/>
	</bean>
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${db.user}"/>
		<property name="password" value="${db.password}"/>
		<property name="driverClass" value="${db.driverClass}"/>
		<property name="jdbcUrl" value="${db.jdbcUrl}"/>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
		<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
		<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
		<property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}"/>
		<property name="automaticTestTable" value="${c3p0.automaticTestTable}"/>
		<property name="numHelperThreads" value="${c3p0.numHelperThreads}"/>
	</bean>
	
	<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
	
	<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>

	<import resource="classpath:spring/applicationContext-transaction.xml"/>
</beans>

####applicationContext-transaction.xml 配置如下
(采用tx标签声明的事务 如以create,upd·····等开头的方法将被事务控制)

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="upd*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="del*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="execute*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="do*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="set*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
			<tx:method name="get*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
			<tx:method name="show*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
			<tx:method name="list*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
			<tx:method name="select*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
			<tx:method name="query*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
			<tx:method name="has*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
			<tx:method name="ntx*" propagation="NOT_SUPPORTED"/>
			<tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut id="serviceMethod" expression="execution(* com.cpt.service.impl..*.*(..))"/>
		<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
	</aop:config>
</beans>

2.配置springmvc(mySpringMVC-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
	<context:component-scan base-package="com.cpt.controller">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	</context:component-scan>
	
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="synchronizeOnSession" value="true"/>
		<property name="messageConverters">
			<list>
				<ref bean="jsonHttpMessageConverter"/>
			</list>
		</property>
		<property name="webBindingInitializer">
			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
				<property name="conversionService"> <!-- @DateTimeFormat 依赖joda-time -->
					<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
				</property>
			</bean>
		</property>
	</bean>

	<bean id="freeMarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true"/>
		<property name="suffix" value=".ftl"/>
		<property name="contentType" value="text/html;charset=UTF-8"/>
		<property name="requestContextAttribute" value="rc"/>
		<property name="exposeRequestAttributes" value="true"/>
		<property name="exposeSessionAttributes" value="true"/>
		<property name="allowSessionOverride" value="true"/>
		<property name="exposeSpringMacroHelpers" value="true"/>
		<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
	</bean>
	
	<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
	<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPaths">
			<array>
				<value>/WEB-INF/ftl/</value>
			</array>
		</property>
		<property name="freemarkerVariables">
			<map>
				<entry key="xml_escape" value-ref="fmXmlEscape"/>
			</map>
		</property>
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">0</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="number_format">0.##########</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="date_format">yyyy-MM-dd</prop>
				<prop key="time_format">HH:mm:ss</prop>
				<prop key="classic_compatible">true</prop>
				<prop key="template_exception_handler">ignore</prop>
			</props>
		</property>
	</bean>

	<bean id="jsonResolver" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</beans>

3.配置mybatis(applicationContext-mybatis.xml, mybatis-config.xml)
applicationContext-mybatis.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="mainSqlSessionFactory">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
		<property name="mapperLocations"><value>classpath:sqlmaps/**/*.xml</value></property>
	</bean>
 
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.cpt.dao"/>
		<property name="sqlSessionFactoryBeanName" value="mainSqlSessionFactory"/>
	</bean>
</beans>

mybatis-config.xml配置如下
(开启别名)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<package name="com.cpt.model"/>
	</typeAliases>
</configuration>

4.数据库配置文件(c3p0.properties)

db.user=xxx
db.password=xxx
db.driverClass=com.mysql.jdbc.Driver
db.jdbcUrl=jdbc\:mysql\://x-x-x\:3306/demo?useUnicode=true&amp;characterEncoding=UTF8
c3p0.maxPoolSize=20
c3p0.minPoolSize=5
c3p0.initialPoolSize=3
c3p0.maxIdleTime=60
c3p0.acquireRetryAttempts=5
c3p0.idleConnectionTestPeriod=60
c3p0.testConnectionOnCheckin=true
c3p0.automaticTestTable=c3p0Table
c3p0.numHelperThreads=20

说明:这个项目前端整合的是freemarker格式

在此奉上项目源码 https://github.com/chenpt123/ssm.git

以上就是整个项目配置文件,做好相应的部分就可以放在服务器上跑了试试了。亲测成功!
(自己学习笔记,如有错误欢迎留言指正)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值