当前位置:编程学习 > JAVA >>

mybatis+spring 学习笔记

 经过今天一上午的琢磨研究查看文档,终于将spring和mybatis整合在一起了
           首相是放入对应的jar文件
           mybatis-3.0.3.jar                               
           mybatis-spring-1.0.1.jar                  
           mysql-connector-java-5.1.18-bin.jar  
           spring-aop-3.0.5.jar                       
           spring-asm-3.0.5.jar
           spring-beans-3.0.5.jar
           spring-context-3.0.5.jar
           spring-core-3.0.5.jar
           spring-expression-3.0.5.jar
           spring-jdbc-3.0.5.jar
           spring-tx-3.0.5.jar
           spring-web-3.0.5.jar
           aopalliance-1.0.jar
           asm-3.1.jar
           cglib-2.2.jar
           commons-dbcp.jar
           commins-logging-1.1.1.jar          
          commons-pool-1.4.jar
          jstl-1.2.jar
          log4j-1.2.16.jar
      然后就是对spring的配置文件的配置
            首先在beans 中添加要使用到的命名空间
      <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"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         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">
   
     然后添加一个数据源,并使用spring的事物管理
 <context:property-placeholder location="classpath:config.properties"/>
  
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"  value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
 </bean>
 

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <tx:annotation-driven/>

 <!--
定义 myBatis的sqlSessionFactory 
. 1当使用MapperScannerConfigurer时不需要configLocation定义
  2.mapperLocations可以批量载入mapper,但是MapperScannerConfigurer扫描mapper后不会将已存在的mapper加入到Configuration中 
    也可以定义configLocation文件,在里面设置settings和typeAliases等来覆写默认的配置 
 3.如果使用了configLocation文件,并且里面也定义了mappers,那么在MapperScannerConfigurer扫描动作中就不会加入已经存在的mapper了 同mapperLocations情况一样) 
 综上所述:建议采用扫描来完成mapper的注册,并且在sqlSessionFactory的bean配置中不需要设置mapperLocations, 
 如果设置了configLocation文件,那么configLocation文件里也不需要再设置mapper了 
--> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.jsu.lei" />   <!--   这里是model对应的包      -->
    </bean>
 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.jsu.lei.Idao" />   <!-- 这里是dao层接口和映射文件对应的包 -->
    </bean>
 
接下来就是mybatis映射文件
<?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.jsu.lei.Idao.IUserDAO">
  
  <select id="findById" parameterType="int" resultType="user">
   select * from user where id=#{id}
  </select>
  
  <insert id="addUser" parameterType="String" >
   insert into user(name) values(#{name})
  </insert>
 
</mapper> spring mybatis
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,