mycat:spring mybatis

=========================================
spring-config-db.xml
=========================================
<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="druidDataSource" />
        <property name="mapperLocations">
            <list>
                <value>classpath*:sqlmap/*Mapper.xml</value>
            </list>
        </property>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>

    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!--initialSize: 初始化连接-->
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <!--minIdle: 最小空闲连接-->
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <!--maxActive: 最大连接数量-->
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <!--removeAbandoned: 是否自动回收超时连接-->
        <property name="removeAbandoned" value="true"/>
        <!--removeAbandonedTimeout: 超时时间(以秒数为单位)-->
        <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
        <!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒-->
        <property name="maxWait" value="${jdbc.maxWait}"/>
        <property name="defaultAutoCommit">
            <value>false</value>
        </property>
        <property name="validationQuery" value="select 1"/>
        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource" />
    </bean>

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

</beans>
=========================================
mapper.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.mycat.test.model.Test">

<resultMap type="com.mycat.test.model.Test" id="testResultMap">
<result property="id" column="id_"/>
  <result property="name" column="name_"/>
  <result property="userId" column="user_id"/>
</resultMap>

    <select id="selectAll" resultMap="testResultMap">
        select * from sam_test
    </select>

    <select id="selectSome" parameterType="java.util.Map" resultMap="testResultMap">
        select * from sam_test limit #{offset},#{limit}
    </select>

    <select id="getObject" resultMap="testResultMap" parameterType="java.lang.Object">
        select * from sam_test where id_ = #{id}
    </select>

    <insert id="insert" parameterType="com.mycat.test.model.Test">
        INSERT INTO sam_test(
        name_,
        user_id
        )
        VALUES(
            #{name,jdbcType=VARCHAR},
            #{userId,jdbcType=BIGINT}
        )
        <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
            select last_insert_id() as id
        </selectKey>
    </insert>

    <update id="update" parameterType="com.mycat.test.model.Test">
        update sam_test
        <set>
            <if test="name != null and name != ''">
              name_ = #{name},
            </if>
            <if test="userId != null">
              user_id = #{userId},
            </if>
        </set>
        where id_=#{id}
    </update>

    <delete id="delete" parameterType="java.lang.Object">
        delete from sam_test where id = #{id}
    </delete>

    <select id="findOneByMap" resultMap="testResultMap">
        select * from sam_test where 1=1 AND
        <foreach item="param" index="key" collection="list"  open="" separator="AND" close="">
            ${param.name} = #{param.value}
        </foreach>
        limit 0, 1
    </select>

    <select id="findOneByObject" parameterType="com.mycat.test.model.Test" resultMap="testResultMap">
        select * from sam_test
        <where>
                <if test="name != null">
                    and name_=#{name}
                </if>
                <if test="userId != null">
                    and user_id=#{userId}
                </if>
        </where>
        limit 0, 1
    </select>

    <select id="findByMap" resultMap="testResultMap">
        select * from sam_test where 1=1 AND
        <foreach item="param" index="key" collection="list" open="" separator="AND" close="">
            ${param.name} = #{param.value}
        </foreach>
    </select>

    <select id="findByObject" parameterType="com.mycat.test.model.Test" resultMap="testResultMap">
        select * from sam_test
        <where>
            <if test="name != null">
                and name_=#{name}
            </if>
            <if test="userId != null">
                and user_id=#{userId}
            </if>
        </where>
    </select>

    <select id="findByIn" resultMap="testResultMap">
        select * from sam_test where 1=1 AND
        <foreach item="param" index="key" collection="list"  open="" separator="AND" close="">
            ${param.name} IN (#{param.value})
        </foreach>
    </select>

    <select id="findByLike" resultMap="testResultMap">
        select * from sam_test where 1=1 AND
        <foreach item="param" index="key" collection="list"  open="" separator="AND" close="">
            ${param.name} LIKE #{param.value}
        </foreach>
    </select>

    <select id="getTotalCount" resultType="int">
        select count(1) from sam_test
    </select>

    <select id="getCount" parameterType="java.util.Map" resultType="int">
        select count(1) from sam_test where 1=1
        <if test="whereClause != null">
        ${whereClause}
        </if>
    </select>

    <select id="query" parameterType="java.util.Map" resultMap="testResultMap">
        select ${fieldsClause} from sam_test where 1=1
        <if test="whereClause != null">
        ${whereClause}
        </if>
        <if test="orderClause != null">
        ${orderClause}
        </if>
        limit ${limit} offset ${offset}
    </select>

</mapper>

猜你喜欢

转载自samson870830.iteye.com/blog/2387075
今日推荐