zoukankan      html  css  js  c++  java
  • spring和mybatis整合(三)

    一、方法:

      1、导入jar包

      

      2、配置数据信息

        1)Spring加Mybatis的第一种整合方法

    <!-- 描述数据源信息 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
      <property name="username" value="root"/>
      <property name="password" value="0000"/>
    </bean>

    <!-- 描述会话工厂对象 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="configLocation" value="classpath:configuration.xml"/>
      <property name="mapperLocations">
      <list>
        <value>com/entity/*.xml</value>
      </list>
      </property>
    </bean>

    <!-- 描述一个会话对象 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>

          测试类

    public static void main(String[] args) {
      ApplicationContext txt = new ClassPathXmlApplicationContext("applicationContext.xml");
      SqlSession session = (SqlSession)txt.getBean("sqlSession");
      List<Type> list = session.getMapper(TypeMapper.class).findAll();
      System.out.println(list.size());
    }

        2)Spring加Mybatis的第二种整合方法

    <!-- 描述数据源信息 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
      <property name="username" value="root"/>
      <property name="password" value="0000"/>
    </bean>

    <!-- 描述会话工厂对象 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="configLocation" value="classpath:configuration.xml"/>
      <property name="mapperLocations">
      <list>
        <value>com/entity/*.xml</value>
      </list>
      </property>
    </bean>

    <!-- 创建接口的实现类 -->
    <bean id="typeMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
      <property name="mapperInterface" value="com.mapper.TypeMapper"/>
    </bean>

    <!-- 描述service -->
    <bean id="typeService" class="com.service.TypeService">
      <property name="typepMapper" ref="typeMapper"></property>
    </bean>

          测试类

    public static void main(String[] args) {
      ApplicationContext txt = new ClassPathXmlApplicationContext("applicationContext.xml");
      TypeService typeService = txt.getBean(TypeService.class);
      System.out.println(typeService.findAll().size());
    }

        3)Spring加Mybatis的第三种整合方法

    <!-- 描述数据源信息 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
      <property name="username" value="root"/>
      <property name="password" value="0000"/>
    </bean>

    <!-- 描述会话工厂对象 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="configLocation" value="classpath:configuration.xml"/>
      <property name="mapperLocations">
        <list>
          <value>com/entity/*.xml</value>
        </list>
      </property>
    </bean>

    <!-- 为包下的所有接口创建对应的实现类对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.mapper"></property>
    </bean>

    <bean id="typeService" class="com.service.TypeService">
      <property name="typepMapper" ref="typeMapper"></property>
    </bean>

        测试类

    public static void main(String[] args) {
      ApplicationContext txt = new ClassPathXmlApplicationContext("applicationContext.xml");
      TypeService ts = (TypeService)txt.getBean("typeService");
      System.out.println(ts.findAll().size());
    }

    二、spring 声明式事物

      声明事物的配置文件

    <!-- 描述数据源信息 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
      <property name="username" value="root"/>
      <property name="password" value="0000"/>
    </bean>

    <!-- 描述会话工厂对象 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="configLocation" value="classpath:configuration.xml"/>
      <property name="mapperLocations">
      <list>
        <value>com/entity/*.xml</value>
      </list>
      </property>
    </bean>

    <!-- 为包下的所有接口创建对应的实现类对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.mapper"></property>
    </bean>

    <bean id="typeService" class="com.service.TypeService">
      <property name="typepMapper" ref="typeMapper"></property>
    </bean>

    <!-- 添加事物 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 定义一个通知 -->
    <tx:advice id="m1">
      <tx:attributes>
        <tx:method name="add*"/>
      </tx:attributes>
    </tx:advice>
    <aop:config>
      <aop:pointcut expression="execution(* com.service.*.*(..))" id="txt1"/>
      <aop:advisor advice-ref="m1" pointcut-ref="txt1"/>
    </aop:config>

      测试类

    public static void main(String[] args) {
      ApplicationContext txt = new ClassPathXmlApplicationContext("applicationContext.xml");
      TypeService ts = txt.getBean(TypeService.class);

      List<Type> list = new ArrayList<Type>();
      Type t1 =new Type();
      t1.setTname("哈哈");
      list.add(t1);
      Type t2 =new Type();
      t2.setTname("呵呵");
      list.add(t2);
      Type t3 =new Type();
      t3.setTname("嘿嘿");
      list.add(t3);
      ts.addBatch(list);

    }

      

      注解式事物

    <!-- 描述数据源信息 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
    <property name="username" value="root"/>
    <property name="password" value="0000"/>
    </bean>

    <!-- 描述会话工厂对象 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:configuration.xml"/>
    <property name="mapperLocations">
    <list>
    <value>com/entity/*.xml</value>
    </list>
    </property>
    </bean>

    <!-- 为包下的所有接口创建对应的实现类对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mapper"></property>
    </bean>

    <bean id="typeService" class="com.service.TypeService">
    <property name="typepMapper" ref="typeMapper"></property>
    </bean>

    <!-- 添加事物 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 加载声明驱动 -->
    <tx:annotation-driven/>

    <!-- 定义一个通知 -->
    <!-- <tx:advice id="m1">
    <tx:attributes>
    <tx:method name="add*"/>
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut expression="execution(* com.service.*.*(..))" id="txt1"/>
    <aop:advisor advice-ref="m1" pointcut-ref="txt1"/>
    </aop:config> -->

        方法类中

    public class TypeService {
    private TypeMapper typeMapper;

    public void setTypepMapper(TypeMapper typeMapper) {
      this.typeMapper = typeMapper;
    }

    public List<Type> findAll(){
      return typeMapper.findAll();
    }
    public int add(Type type){
      return typeMapper.add(type);
    }

    @Transactional
    public void addBatch(List<Type> list){
      for (int i = 0; i < list.size(); i++) {
        int row = add(list.get(i));
      }
    }

    }

  • 相关阅读:
    phpstorm 2017 关掉变量提示 parameter name hints,改变打开方式
    RMAN-06496: must use the TO clause when the database is mounted or open
    FAL[client]: Failed to request gap sequence GAP
    搭建rac对单实例的MAA
    Linux下安装Oracle 10g(redhat 4)
    ora-01031:insufficient privileges
    ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
    checking for oracle home incompatibilities failed
    用root帐号切换其他帐号提示 su: warning: cannot change directory to /home/oracle: Permission denied
    an error occured during the file system check
  • 原文地址:https://www.cnblogs.com/newbest/p/9204522.html
Copyright © 2011-2022 走看看