zoukankan      html  css  js  c++  java
  • Spring和Mybatis整合

    Application.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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 配置数据源 -->
    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
      <property name="url" value="jdbc:mysql://localhost:3306/smbms"></property>
      <property name="username" value="root"></property>
      <property name="password" value="root"></property>
    </bean>

    <!-- 配置sqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="datasource"></property>
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!-- 配置SqlSessionTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <!-- 配置DAo -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="cn.yct.dao"></property>
    </bean>
    <!-- 扫描service层 -->
    <context:component-scan base-package="cn.yct.service"></context:component-scan>

    <!-- 定义事物管理 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="datasource"></property>
    </bean>

    <!-- 通过<tx:Advice>标签为指定的事物管理器设置属性 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- 定义属性,声明事物规定 -->
    <tx:attributes>
      <tx:method name="find*" propagation="SUPPORTS"/>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="del*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>

    <!-- 定义切面 -->
    <aop:config>
      <!-- 定义切点 -->
      <aop:pointcut expression="execution(* cn.yct.service..*.*(..))" id="servicerole"/>
      <!-- 将事物增强与切入点组合 -->
      <aop:advisor advice-ref="txAdvice" pointcut-ref="servicerole"/>
    </aop:config>

    <bean/>

    在Service实现层   如:UserServiceImpl

    @Service("billservice") 
    public class BillServiceImpl implements BillService {

    @Autowired
    private BillDaoMapper billDaoMapper;             映射接口
    public BillDaoMapper getBillDaoMapper() {
      return billDaoMapper;
    }
    public void setBillDaoMapper(BillDaoMapper billDaoMapper) {
      this.billDaoMapper = billDaoMapper;
    }
    @Override
    public List<Smbms_Bill> getlist(Map map) {
      return billDaoMapper.getlist(map);
    }
    }

  • 相关阅读:
    详解机器学习中的熵、条件熵、相对熵、交叉熵
    使用Keras进行深度学习:(三)使用text-CNN处理自然语言(上)
    粒子群优化算法(PSO)之基于离散化的特征选择(FS)(一)
    DNN模型训练词向量原理
    TensorFlow 实战卷积神经网络之 LeNet
    五大经典卷积神经网络介绍:LeNet / AlexNet / GoogLeNet / VGGNet/ ResNet
    Oracle 查询版本号
    C# 递归获取 文件夹的 所有文件
    SQL Server 常用语句
    Oracle 导入大量数据
  • 原文地址:https://www.cnblogs.com/y-c-t/p/8563293.html
Copyright © 2011-2022 走看看