Spring 集成mybatis 配置方法

 

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd     
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


    <util:properties id="config" location="classpath:db.properties"/>
    <!-- 配置连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="#{config.driver}"></property>
        <property name="url" value="#{config.url}"></property>
        <property name="username" value="#{config.username}"></property>
        <property name="password" value="#{config.password}"></property>
    </bean>
    <!-- 配置SqlSessionFactoryBean -->
    <!-- 
        spring集成mybatis,不再需要mybatis的配置文件(使用SqlSessionFactoryBean来
        代替mybatis的配置文件)。
     -->
    <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入连接池 -->
        <!-- 不再使用mybatis自带的连接池,而是使用spring管理的连接池 -->
        <property name="dataSource" ref="ds"></property>
        <!-- 映射文件的位置 -->
        <property name="mapperLocations" value="classpath:entity/*.xml"></property>
    </bean>
    <!-- 配置MapperScannerConfigurer -->
    <!-- 
        MapperScannerConfigurer负责扫描指定包下面的所有的Mapper映射器,
        然后生成符合这些映射器要求的对象(其实,就是调用SqlSession的getMapper方法)。
        另外,还会将这些对象添加到spring容器里面(默认的id是首字母小写之后的接口名
        也可以使用@Respository来重新设置id)。
        
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- Mapper映射器所在的包 -->
        <property name="basePackage" value="dao"></property>
  <!-- 只扫描特定的接口 开放一个注解:@MyBatisRepository-->
 <property name="annotationClass" value="annotations.MyBatisRepository"></property>
    </bean>

</beans>

Mapper.xml文件配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Dept.xml 在com.tarena.entity 包中  -->  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="dao.EmpDAO">
    <insert id="save" parameterType="entity.Emp">
        INSERT INTO emp1 
        VALUES(emp_seq.nextval,#{name},#{age})
    </insert>
    <!-- 
        id:要求唯一。
        resultType:返回的结果的类型
        paremeterType:参数类型
     -->
    <select id="findAll" resultType="entity.Emp">
        SELECT * FROM emp1
    </select>
    
    <select id="findById" parameterType="int" resultType="entity.Emp">
        SELECT * FROM emp1
        WHERE id = #{id1}
    </select>
    
    <update id="modify" parameterType="entity.Emp">
        UPDATE emp1 SET name=#{name}, age=#{age} 
        WHERE id = #{id}
    </update>
    
    <delete id="delete" parameterType="int"> 
        DELETE FROM emp1
        WHERE id = #{id1}
    </delete>
    
    <!-- 返回Map类型的结果  
        map 是java.util.Map的简写形式。
    -->
    <select id="findById2" parameterType="int" resultType="map">
        SELECT * FROM emp1
        WHERE id = #{id1}    
    </select>
    
    <!-- 使用ResultMap解决表的字段名与实体类的属性名不一致的情况 -->
    <select id="findById3" parameterType="int" resultMap="emp2Map">
        SELECT * FROM emp1
        WHERE id = #{id1}
    </select>
    
    <!-- 处理表的字段名与实体类的属性名的对应关系 -->
    <resultMap type="entity.Emp2" id="emp2Map">
        <result property="ename" column="name"/>
        <result property="empNo" column="id"/>
    </resultMap>
    
</mapper>

 

posted @ 2017-08-03 11:51  scha  阅读(197)  评论(0编辑  收藏  举报