Spring整合Mybatis(一)------MapperScannerConfigurer

14 篇文章 0 订阅
4 篇文章 0 订阅

前言

此篇仅以一个小例子,讲解一下Spring是如何整合Mybatis的。

讲解

介绍

Spring集成Mybatis时是通过MapperFactoryBean来生成Mapper接口的代理,生成的代理类是实现了Mapper接口的。并且通过MapperScannerConfigurer扫描所有的代理类。

目录结构

目录结构

源码

从上到下依次介绍。

User

一个PoJo类,作为数据层的映射

package cn.ceres.architecture.integration.dao.model;

public class User {

    private Long id;
    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

UserMapper

数据库操作的映射文件,是UserMapper.xml的映射

package cn.ceres.architecture.integration.dao;

import cn.ceres.architecture.integration.dao.model.User;

public interface UserMapper {

    public User getUser(Long id);

}

UserMapper.xml

具体执行Sql访问数据库的映射文件

<?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="cn.ceres.architecture.integration.dao.UserMapper">


    <resultMap id="userMap" type="cn.ceres.architecture.integration.dao.model.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
    </resultMap>

    <!-- 查询 此处如果多参数,建议使用Bean作为参数体-->
    <select id="getUser" parameterType="java.lang.Long" resultMap="userMap">
        SELECT
            <include refid="QUERY_COLUMN_LIST"/>
        FROM
        <include refid="TABLE"/>
        WHERE
        id = #{id}
    </select>

    <!-- 所有查询列 -->
    <sql id="QUERY_COLUMN_LIST">
        <![CDATA[
        `id`,`name`
        ]]>
    </sql>

    <!-- 表-->
    <sql id="TABLE"><![CDATA[ user ]]></sql>

    <!-- 全部条件(更多功能可以通过queryData扩展实现)  -->
    <sql id="QUERY_WHERE_CLAUSE">
        <where>
            <if test="id != null and id != ''"><![CDATA[AND `id` = #{id}]]></if>
            <if test="name != null and name != ''"><![CDATA[AND `name` = #{name}]]></if>
        </where>
    </sql>

    <!-- 智能排序与分页 -->
    <sql id="QUERY_ORDER_LIMIT_CONDITION">
        <if test="orderField != null and orderField != '' and orderFieldType != null and orderFieldType != ''"><![CDATA[ORDER BY ${orderField} ${orderFieldType}]]></if>
        <if test="startIndex != null and startIndex &gt;= 0 and pageSize != null and pageSize &gt; 0"><![CDATA[LIMIT #{startIndex},#{pageSize}]]></if>
    </sql>

</mapper>

spring-config-dao.xml

Spring结合时的配置文件

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd"
       default-autowire="byName">

    <!-- BoneCP configuration -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${spring.datasource.username}"/>
        <property name="password" value="${spring.datasource.password}"/>
        <property name="jdbcUrl" value="${spring.datasource.url}"/>
        <property name="driverClass" value="${spring.datasource.driverClassName}"/>

        <property name="acquireIncrement" value="${spring.datasource.c3p0.acquireIncrement}"/>
        <property name="idleConnectionTestPeriod" value="${spring.datasource.c3p0.idleConnectionTestPeriod}"/>
        <property name="initialPoolSize" value="${spring.datasource.c3p0.initialPoolSize}"/>
        <property name="maxIdleTime" value="${spring.datasource.c3p0.maxIdleTime}"/>
        <property name="maxPoolSize" value="${spring.datasource.c3p0.maxPoolSize}"/>
        <property name="minPoolSize" value="${spring.datasource.c3p0.minPoolSize}"/>
        <!--增加 数据库库测试连接-->
        <!--<property name="automaticTestTable" value="c3p0_test"/>-->
        <!--<property name="testConnectionOnCheckin" value="true"/>-->
        <property name="preferredTestQuery" value=" select 1 "/>

    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Transaction Manager -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 启用事务注解 -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--自动查找类路径下的映射器并将它们自动装配-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory" />
        <property name="basePackage" value="cn.ceres.architecture.integration.dao"/>
    </bean>
</beans>

application.properties

用于替换配置文件中的属性占位符

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:28827/architecture_integration?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=passw0rd

spring.datasource.c3p0.acquireIncrement=2
spring.datasource.c3p0.idleConnectionTestPeriod=50
spring.datasource.c3p0.initialPoolSize=5
spring.datasource.c3p0.maxIdleTime=90
spring.datasource.c3p0.maxPoolSize=3
spring.datasource.c3p0.minPoolSize=1

总结

由于占位符属性基本上是配置在Service层,因此在目录结构中并未体现。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值