• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

spring 集成 mybatis的实例详解

mybatis 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论
文章目录[隐藏]

 环境配置

1>先创建maven的quickstart项目;并且创建dao层,service层,controller层,po层,mapper,resources以及下面的配置文件(db.properties,log4j.properties,mybatis.xml,spring.xml).

2>配置pom.xml

修改jdk版本;

添加依赖:

​ junit版本改为4.12;spring-context;spring-test;spring-jdbc;spring-tx(事务);aspectjweaver(切面编程);c3p0(连接池);mybatis;mybatis-spring;mysql-connector-java(mysql驱动包);slf4j-log4j12,slf4j-api(日志打印);

设置资源目录和插件

 <build>
 <!--
  Maven 项目:如果源代码(src/main/java)存在xml、properties、tld 等文件
  Maven 默认不会自动编译该文件到输出目录,如果要编译源代码中xml properties tld 等文件
  需要显式配置 resources 标签
 -->
 <resources>
  <resource>
  <directory>src/main/resources</directory>
  </resource>
  <resource>
  <directory>src/main/java</directory>
  <includes>
   <include>**/*.xml</include>
   <include>**/*.properties</include>
   <include>**/*.tld</include>
  </includes>
  <filtering>false</filtering>
  </resource>
 </resources>
 </build>

3>配置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:context="http://www.springframework.org/schema/context"
  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.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!-- 扫描基本包 -->
  <context:component-scan base-package="com.xxxx" />

  <!-- 加载properties 配置文件 -->
  <context:property-placeholder location="classpath:db.properties" />

  <!-- aop -->
  <aop:aspectj-autoproxy />

  <!-- 配置c3p0 数据源 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
  </bean>

  <!-- 配置事务管理器 -->
  <bean id="txManager"
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!-- 设置事物增强 -->
  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED" />
      <tx:method name="insert*" propagation="REQUIRED" />
      <tx:method name="update*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
    </tx:attributes>
  </tx:advice>

  <!-- aop 切面配置 -->
<a>本文来源gao($daima.com搞@代@#码8网^</a>  <aop:config>
    <aop:pointcut id="servicePointcut" expression="execution(* com.xxxx.service..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
  </aop:config>

  <!-- 配置 sqlSessionFactory -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis.xml" />
    <property name="mapperLocations" value="classpath:com/xxxx/mapper/*.xml" />
  </bean>

  <!-- 配置扫描器 -->
  <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 扫描com.xxxx.dao这个包以及它的子包下的所有映射接口类 -->
    <property name="basePackage" value="com.xxxx.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  </bean>
</beans>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring 集成 mybatis的实例详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址