mybatis集成spring

来源:互联网 发布:360解压缩 for mac 编辑:程序博客网 时间:2024/05/23 19:11

主要的架包支持

其它的架包用到了自动加

spring-webmvc

spring-jdbc

mybatis-spring

Dbcp 数据源架包

Aspectjweaver  事务架包(可选)

jackson架包(可选)


pom.xml

<!-- 引入springmvc架包 --><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  <version>4.3.2.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-web</artifactId>  <version>4.3.2.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  <version>4.3.2.RELEASE</version></dependency><!-- mybatis集成spring的核心包 --><dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis-spring</artifactId>  <version>1.2.3</version></dependency><!-- 新的数据源架包dbcp --><dependency>  <groupId>commons-dbcp</groupId>  <artifactId>commons-dbcp</artifactId>  <version>1.4</version></dependency><!-- aspectjweaver 事务架包 --><dependency>  <groupId>org.aspectj</groupId>  <artifactId>com.springsource.org.aspectj.weaver</artifactId>  <version>1.6.8.RELEASE</version></dependency><!-- jackson架包 --><dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-annotations</artifactId>  <version>2.6.0</version></dependency><dependency>  <groupId>org.codehaus.jackson</groupId>  <artifactId>jackson-mapper-asl</artifactId>  <version>1.9.12</version></dependency><dependency>  <groupId>net.sf.json-lib</groupId>  <artifactId>json-lib</artifactId>  <version>2.3</version>  <classifier>jdk15</classifier></dependency><dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-core</artifactId>  <version>2.6.0</version></dependency><dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>         <version>2.6.0</version></dependency>  

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- springmvc的配置只能扫描控制层  spring配置文件不能扫描控制层 --><context:component-scan base-package="cn.et"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan><mvc:default-servlet-handler></mvc:default-servlet-handler><mvc:annotation-driven><!-- 配置消息转换器 --><mvc:message-converters><!-- 设置json转换消息转换器,并且设置supportedMediaTypes  否则抛出406 --><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><!-- 设置响应支持的类型 --><value>text/html;charset=UTF-8</value><!-- 设置请求body支持的类型 --><value>application/x-www-form-urlencoded</value><value>application/json;charset=UTF-8</value></list></property></bean></mvc:message-converters></mvc:annotation-driven></beans>

spring.xml

dbcp连接池配置

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- springmvc的配置只能扫描控制层  spring配置文件不能扫描控制层 --><context:component-scan base-package="cn.et"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 读取jdbc.properties文件 --><context:property-placeholder location="classpath:/cn/et/mybatis/lesson06/utils/jdbc.properties" /><!-- 数据的连接  数据源 --><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="url" value="${url}"></property><property name="driverClassName" value="${driverClass}"></property><property name="username" value="${account}"></property><property name="password" value="${password}"></property><!-- initialSize   10默认生成10个连接,那么要用到连接的时候就直接拿一条出来用就可以了,就不用等到用的时候再去产生连接 --><property name="initialSize" value="10"></property><!-- 发起一条测试的sql语句去连接一下数据库,看是否可以正常连接数据库 --></bean><!-- 创建一个jdbc模板SqlSessionFactoryBean --><bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property></bean><!-- 这里一定要用sqlSessionFactoryBeanName,不然加载不了driverClass --><!-- 扫描接口映射和注解和xml文件 --><bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sessionFactory"></property><!-- 如果映射的是Mapper接口直接放到mapper包里面扫描就发好了如果映射的是xml配置文件需要把配置文件名改成对应的接口一样的名称,并都要放到mapper包下 --><property name="basePackage" value="cn.*..*.lesson06.mapper"></property></bean><!-- 事务管理器 spring帮助我们控制事务 --><bean id="transManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- aspectjweaver 事务架包 --><!-- 当切点拦截到某个操作的方法  发送通知给tx定义的通知管理  调用事务管理器 提交和回滚 --><tx:advice id="myAdvice" transaction-manager="transManager"><tx:attributes><!-- 默认的配置 --><tx:method name="add*" propagation="REQUIRED" /><tx:method name="query*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><!-- *代表了除了上面配置的方法都不使用事务 --><tx:method name="*" read-only="true" /></tx:attributes></tx:advice><!-- 切面 --><aop:config><aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" /><aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" /></aop:config></beans>