Spring集成Mybatis,spring4.x整合Mybatis3.x
==============================
蕃薯耀 2018年3月14日
http://fanshuyao.iteye.com/
一、spring.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xmlns:p="http://www.springframework.org/schema/p" 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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/> <context:component-scan base-package="com.lqy.ssm" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- druid配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="driverClassName" value="${jdbc_driver_class_name}" /> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat" /> <!-- 配置初始化大小、最小、最大 --> <property name="maxActive" value="20" /> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxOpenPreparedStatements" value="20" /> </bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis全局配置文件,已经不需要 --> <!-- <property name="configLocation" value="classpath:mybatis.xml"></property> --> <property name="dataSource" ref="dataSource"></property> <!-- 指定Mybatis mapper文件的位置 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> <!-- <property name="typeAliasesPackage" value="com.lqy.ssm.bean"></property> --> <property name="plugins"> <array> <!-- 配置pagehelper分页插件,使用见:https://pagehelper.github.io/docs/howtouse/ --> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql reasonable=true supportMethodsArguments=true params=count=countSql autoRuntimeDialect=true </value> </property> </bean> </array> </property> </bean> <!-- 配置一个可以批量执行的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!-- 配置扫描器,将Mybatis接口的实现加入到ioc容器 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描所有dao接口的实现 --> <property name="basePackage" value="com.lqy.ssm.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 注解方式的事务 <tx:advice id="txAdvice" transaction-manager="transactionManager" proxy-target-class="true"> --> <!-- 通过AOP配置提供事务增强,事务如何切入 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 让service包下所有Bean的所有方法拥有事务 --> <tx:method name="*" /> <!-- <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="set*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> --> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="count*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> <tx:method name="list*" propagation="SUPPORTS" read-only="true" /> <tx:method name="page*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <!-- 切点表达式 --> <aop:pointcut id="serviceMethod" expression=" execution(* com.lqy.ssm.service..*(..))" /> <!-- 引用到事务增强 --> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" /> </aop:config> </beans>
二、jdbc.properties文件
jdbc_username=root jdbc_password=12345678 jdbc_driver_class_name=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
三、springMvc.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- use-default-filters设置为false关闭默认扫描 --> <context:component-scan base-package="com.lqy.ssm" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 视图解析器 --> <mvc:view-resolvers> <mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp"/> </mvc:view-resolvers> <!-- 支持对象与json的转换 --> <!-- <mvc:annotation-driven /> --> <mvc:annotation-driven> <!-- conversion-service="conversionService" --> <mvc:message-converters> <!-- UTF-8字符格式化 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>application/xml;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> <value>text/xml;charset=UTF-8</value> </list> </property> </bean> <!-- 处理responseBody 里面日期类型 --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 将springMvc不能处理的请求交给tomcat --> <mvc:default-servlet-handler/> <!-- 配置文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding"> <value>UTF-8</value> </property> <property name="maxUploadSize"> <value>32505856</value> <!-- 上传文件大小限制为31M,31*1024*1024 --> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> </beans>
四、pom.xml文件
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>lqy</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssm Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.3.13.RELEASE</spring.version> <mybatis.version>3.4.6</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> <mybatis.generator.version>1.3.6</mybatis.generator.version> <pagehelper.version>5.1.2</pagehelper.version> <slf4j.version>1.7.7</slf4j.version> <jackson.version>2.6.5</jackson.version> <fastjson.version>1.2.46</fastjson.version> <druid.version>1.1.9</druid.version> <mysql.version>5.1.45</mysql.version> <el.version>2.2.5</el.version> <standard.version>1.1.2</standard.version> <jstl.version>1.2</jstl.version> <jsp.api.version>2.2</jsp.api.version> <javax.servlet.api.version>3.0.1</javax.servlet.api.version> <junit.version>4.12</junit.version> </properties> <dependencies> <!-- spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>${spring.version}</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- fastjson依赖包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis.generator.version}</version> <scope>provided</scope> </dependency> <!-- mybatis分页插件 使用见:https://pagehelper.github.io/docs/howtouse/--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- druid数据连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- jsp页面依赖包 --> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>${el.version}</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>${standard.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet.api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ssm</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)
==============================
蕃薯耀 2018年3月14日
http://fanshuyao.iteye.com/
相关推荐
spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip spring集成mybatis.zip ...
在本教程中,我们将深入探讨如何将MyBatis与Spring 3.x进行集成,以便在实际项目中实现灵活且高效的数据库操作。MyBatis是一个优秀的持久层框架,它简化了SQL映射,而Spring框架则提供了强大的依赖注入和管理功能。...
Spring整合MyBatis是Java开发中常见的数据访问技术组合,它允许我们利用Spring的IoC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)特性来管理MyBatis的SQL映射和数据访问。...
本教程将深入探讨如何在 Spring 5.x 版本中整合 MyBatis 3.x,同时使用阿里巴巴的 Druid 数据连接池。 **1. Spring 5.x 概述** Spring 5.x 是 Spring 框架的一个主要版本,它带来了许多新特性和改进,包括对 Java 8...
在事务管理方面,Mybatis3.x与Spring的集成更加紧密,可以利用Spring的事务管理功能,实现声明式事务,提升了事务处理的规范性和可维护性。此外,3.x版本还支持使用ExecutorType执行器类型,如SIMPLE、REUSE和BATCH...
3. MyBatis-Spring整合库:mybatis-spring。 4. 数据库驱动:如mysql-connector-java(对于MySQL)、ojdbc(对于Oracle)等。 5. 其他依赖库:如log4j或slf4j用于日志记录,asm、cglib等用于AOP代理。 整合过程大致...
通过SpringMvc+Spring+Mybatis+Maven整合,学习用maven搭建框架
《Spring4.2与Mybatis3.3整合详解及项目架构构建》 在现代Java Web开发中,Spring框架和Mybatis作为两个非常重要的组件,常被用来构建高效、灵活的项目架构。本压缩包"Spring4.2Mybatis3.3Spring4.2.zip"包含了这两...
Spring+MyBatis+Redis整合 Title:Spring+MyBatis+Redis整合 Description:Spring+MyBatis+Redis整合 Tag:MyBatis Spring Redis 内容摘要: 本文主要讲述了如何将Spring、MyBatis和Redis三者进行整合,以解决...
spring 搭建Spring和MyBatis框架实验总结.zip
springboot+mybatis在线问卷系统.zipspringboot+mybatis在线问卷系统.zipspringboot+mybatis在线问卷系统.zipspringboot+mybatis在线问卷系统.zipspringboot+mybatis在线问卷系统.zipspringboot+mybatis在线问卷系统...
基于springboot+mybatis学生管理系统.zip基于springboot+mybatis学生管理系统.zip基于springboot+mybatis学生管理系统.zip基于springboot+mybatis学生管理系统.zip基于springboot+mybatis学生管理系统.zip基于...
校社联社团管理系统(Spring MVC+Spring+Mybatis+Redis),用来记录进度,和保存文件,完成一定阶段都上传到小组仓库中。 校社联社团管理系统(Spring MVC+Spring+Mybatis+Redis),用来记录进度,和保存文件,完成...
Spring与MyBatis的整合主要是通过Spring的MyBatis-Spring模块实现的。这个模块提供了一种将MyBatis与Spring容器无缝结合的方式。配置完成后,Spring可以管理SqlSessionFactory和Mapper接口的Bean,使得在业务逻辑中...
Spring 3.x+Spring MVC 3.x+MyBatis 3.x 整合代码 该代码仅截止到《Spring 3.x+Spring MVC 3.x+MyBatis 3.x 整合(五)MyBatis 3.2.5 整合》,原文件地址:http://blog.csdn.net/xz2001/article/details/44346355
<groupId>org.mybatis.spring <artifactId>mybatis-spring <version>2.0.6 <!-- MySQL驱动 --> <groupId>mysql <artifactId>mysql-connector-java <version>8.0.32 <!-- 日志依赖 --> <groupId>org...
spring集成mybatis,资源包括: 中文版MyBatis 3 User Guide.pdf mybatis-spring集成指导.pdf Mybatis.pdf mybatis-3.2.8.jar mybatis-spring-1.2.2.jar
1. **mybatis.jar**:MyBatis的核心库,包含了SQL映射框架的实现,提供动态SQL和事务管理等功能。 2. **mybatis-spring.jar**:这是Spring和MyBatis的整合包,使得MyBatis可以无缝地融入到Spring的IoC容器中,方便...
Spring3 整合 MyBatis3 配置多数据源动态选择 SqlSessionFactory 详细教程 本教程主要介绍了 Spring3 整合 MyBatis3 配置多数据源动态选择 SqlSessionFactory 的详细教程。下面将详细介绍如何实现 Spring 整合 ...
在本文中,我们将探讨如何集成Spring框架与MyBatis,以构建一个完整的实例。这个实例是一个图书管理系统的简单实现,涉及的技术栈包括Spring、MyBatis、Maven和MySQL。以下是详细步骤: **一、创建Maven Web项目** ...