spring和mybatis整合(三)

來源:互聯網
上載者:User

標籤:border   font   ati   .com   添加   test   config   new   bsp   

一、方法:

  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));
  }
}

}

spring和mybatis整合(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.