spring数据源配置及事务管理--mybatis3

一、包依赖

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId> <!-- mybatis -->
  <version>3.2.1</version>
 </dependency>
 
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>   <!-- mybatis -->
  <version>1.2.0</version>
 </dependency>

二、配置文件(applicationContext-mybatis.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
    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.0.xsd
   
http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   
    <bean id="dataSource_dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="password" value="root"/>
  <property name="username" value="root"/>
  <property name="url" value="jdbc:mysql://localhost:3306/ppt?useUnicode=true&amp;characterEncoding=UTF-8"/>
  <property name="maxActive" value="20"/>
  <property name="maxIdle" value="15"/>
  <property name="minIdle" value="5"/>
  <property name="maxWait" value="60000"/>
  <property name="initialSize" value="1"/>
  <property name="removeAbandoned" value="true"/>
  <property name="removeAbandonedTimeout" value="180"/>
  <property name="logAbandoned" value="true"/>
  <property name="timeBetweenEvictionRunsMillis" value="3600000"/>
  <property name="minEvictableIdleTimeMillis" value="3600000"/>
  <property name="defaultAutoCommit" value="false"/>
 </bean>
   
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource">
      <ref local="dataSource_dbcp"/>
     </property>
     <property name="mapperLocations" value="mybatis/*"></property>
    </bean>
    <!--
    <bean id="mybatisDemoInterface" class="org.mybatis.spring.mapper.MapperFactoryBean">
     <property name="mapperInterface" value="com.csair.dao.MybatisDemoInterface"/>
     <property name="sqlSessionFactory">   <!--自动代理,帮助实现接口-->
      <ref local="sqlSessionFactory"/>
     </property>
    </bean>
    -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.csair.dao"></property> <!-- 扫描接口定义的方式,自动帮实现 -->
    </bean>


    <bean id="mybatisDemo" class="com.csair.dao.MybatisDemo">
     <property name="mybatisDemoInterface"><ref local="mybatisDemoInterface"/></property>
    </bean>
   
</beans>

三、查询语句映射文件(UserAnotationMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper 
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
  "
http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csair.dao.MybatisDemoInterface">
 <select id="selectUser" resultType="com.csair.dao.UserAnotation">
  select * from user
 </select>
 <insert id="addUser">
  insert into user(username, password) value('mybatis', 'mybatis')
 </insert>
</mapper>

四、接口

public interface MybatisDemoInterface {
 List<UserAnotation> selectUser();
 void addUser();
}

五、测试类

public class MybatisDemo {
 private MybatisDemoInterface mybatisDemoInterface;
 public static void main(String[] args) {
  String configLocation = "applicationContext-mybatis.xml";
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
  MybatisDemo demo = context.getBean("mybatisDemo", MybatisDemo.class);
  demo.query();
 }
 public void query() {
  List<UserAnotation> user = mybatisDemoInterface.selectUser();
  System.out.println(user.toString());
 }
 public void insert() {
  mybatisDemoInterface.addUser();
 }
 public MybatisDemoInterface getMybatisDemoInterface() {
  return mybatisDemoInterface;
 }
 public void setMybatisDemoInterface(MybatisDemoInterface mybatisDemoInterface) {
  this.mybatisDemoInterface = mybatisDemoInterface;
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值