网站首页 文章专栏 springMvc+spring+mybatis整合(SSM框架)
springMvc+spring+mybatis整合(SSM框架)
编辑时间:2018-09-01 14:07:09.0 作者:听雨·湘潭 浏览量:552

新建一个Maven项目,在pom.xml添加如下依赖:

<dependencies>
		<!-- 导入java ee jar 包 -->
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>7.0</version>
		</dependency>
		<!--servlet依赖jar -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<!--jstl 依赖jar -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>



		<!-- spring 核心包【ioc/di/core】 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- aop依赖包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.9</version>
		</dependency>
		<!-- aop依赖包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.9</version>
		</dependency>
		<!-- spring AOP -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- 支持@Resource注解的Jar包 -->
		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>jsr250-api</artifactId>
			<version>1.0</version>
		</dependency>
		<!-- 测试用例 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>

		<!-- 引入 mybatis和redis集成包 -->
		<dependency>
			<groupId>org.mybatis.caches</groupId>
			<artifactId>mybatis-redis</artifactId>
			<version>1.0.0-beta2</version>
		</dependency>
		<!-- mybatis核心包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>
		<!-- 数据库依赖包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.41</version>
		</dependency>
		<!-- 分页 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.1.4</version>
		</dependency>
		<!-- 代码生成 -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.7</version>
		</dependency>
		<!-- druid数据源【连接池】 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.10</version>
		</dependency>

		<!-- 日志jar包 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<!-- spring和mybatis集成jar包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>

		<!-- spring 事务支持 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- spring jdbc实现类 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
	</dependencies>

 

新建MyBatis配置文件  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>

	<!-- 开启mybatis二级缓存 -->
 	<settings>
 	   <setting name="cacheEnabled" value="true"/>
 	</settings>
 	
	
</configuration>

 

新建Spring配置文件 application-config.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 开启自动装配【类上使用注解,自动注册IOC容器中,并且开启DI【依赖关系】】 -->
	<context:component-scan base-package="com.longwang" />
	<!-- 开启Aop注解支持 -->
	<aop:aspectj-autoproxy />
	<!-- 配置druid连接池 -->
	<bean init-method="init" id="dataSource"
		class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<!-- jdbc驱动 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<!-- 数据库连接 -->
		<property name="url" value="jdbc:mysql://localhost:3306/db_spring" />
		<!-- 用户名 -->
		<property name="username" value="root" />
		<!-- 密码 -->
		<property name="password" value="123456" />
		<!-- 连接对象初始化值 -->
		<property name="initialSize" value="50" />
		<!-- 连接对象最大值 -->
		<property name="maxActive" value="200" />
	</bean>

	<!-- 创建SqlSessionFactory对象 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--自动设置别名 -->
		<property name="typeAliasesPackage" value="com.longwang.entity" />
		<!-- 设置分页插件 -->
		<property name="plugins">
			<list>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<value>offsetAsPageNum=false</value>
					</property>
				</bean>
			</list>
		</property>
		<!-- 自动注册sql映射文件 -->
		<property name="mapperLocations" value="classpath:com/longwang/dao/*.xml" />
		<!-- 加载mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 使用Durid数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- mybatis自动扫描映射配置 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 自动扫描映射接口 -->
		<property name="basePackage" value="com.longwang.dao" />
		<!-- 使用sqlSessionFactory -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- 创建事务管理对象 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:advice id="txAdive" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" read-only="false" />
			<tx:method name="insert*" propagation="REQUIRED" read-only="false" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" />
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
			<tx:method name="midfy*" propagation="REQUIRED" read-only="false" />
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	<!-- aop事务拦截 -->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut expression="execution(* com.longwang.service.imple.*.*(..))"
			id="pointcut" />
		<!-- 绑定切入点和事务通知 -->
		<aop:advisor advice-ref="txAdive" pointcut-ref="pointcut" />
	</aop:config>
</beans>

 

新建SpringMvc配置文件 spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 扫描组件 @Controller -->
   <context:component-scan base-package="com.ty.controller"></context:component-scan>
   <!-- @RequestMapping 开启注解支持,映射处理注解 -->
   <mvc:annotation-driven></mvc:annotation-driven>
   <!-- 开启jsp视图的支持 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/page/"/>
      <property name="suffix" value=".jsp"/>
   </bean>
   <!-- 资源文件访问问题 -->
   <mvc:resources location="/public/" mapping="/public/**"></mvc:resources>
</beans>

 

新建log4j配置文件 log4j.properties (可要可不要)

log4j.rootLogger=DEBUG,CONSOLE,file
log4j.logger.com.ibatis=debug 
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
log4j.logger.java.sql.Connection=debug 
log4j.logger.java.sql.Statement=debug 
log4j.logger.java.sql.PreparedStatement=debug 
log4j.logger.java.sql.ResultSet=debug 

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n

 

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- classpath classes目录 -->
    <param-value>classpath:application-config.xml</param-value>
  </context-param>
  
  <!-- 保证ApplicationContext 容器只会初始化一次 -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <!-- 开启springMvc 前端控制器,入口 -->
  <servlet>
    <servlet-name>spring-Mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
     <servlet-name>spring-Mvc</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>commonFilter</filter-name>
    <filter-class>com.ty.filter.CommonFilter</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>commonFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

ssm整合完成!!!

来说两句吧
最新评论