MyBatis与Spring结合实例

MyBatis.xml配置


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.1.xsd ">


    
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:mybatis/jdbc.properties" />
    bean>

    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        
        <property name="initialSize" value="${initialSize}" />
        
        <property name="maxActive" value="${maxActive}" />
        
        <property name="maxIdle" value="${maxIdle}" />
        
        <property name="minIdle" value="${minIdle}" />
        
        <property name="maxWait" value="${maxWait}" />
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
        <property name="validationQuery" value="${validationQuery}" />
        <property name="logAbandoned" value="${logAbandoned}" />
    bean>

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

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mk.dao.IDao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>

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

beans>

mybatis/jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://172.16.11.11:3306/dm?useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
username=root
password=520SQL
initialSize=10
maxActive=32
maxIdle=16
minIdle=8
maxWait=3000
removeAbandonedTimeout=10
validationQuery=SELECT 1
logAbandoned=true

映射文件示例



<mapper namespace="com.mk.dao.IDao.IAppDao">

    <resultMap id="BaseResultMap" type="com.mk.dao.vo.app.App">
        <id column="appId" property="appId" jdbcType="VARCHAR" />
        <result column="appName" property="appName" jdbcType="VARCHAR" />
        <result column="appSize" property="appSize" jdbcType="BIGINT" />
        <result column="appVersion" property="appVersion" jdbcType="INTEGER" />
        <result column="appPath" property="appPath" jdbcType="VARCHAR" />
        <result column="appType" property="appType" jdbcType="INTEGER" />
        <result column="appCount" property="appCount" jdbcType="INTEGER" />
        <result column="appNote" property="appNote" jdbcType="VARCHAR" />
        <result column="appCreateTime" property="appCreateTime"
            jdbcType="BIGINT" />
        <result column="appPkg" property="appPkg" jdbcType="VARCHAR" />
        <result column="appVersionName" property="appVersionName"
            jdbcType="VARCHAR" />
        <result column="appPermissions" property="appPermissions"
            jdbcType="BLOB" />
        <result column="appIcon" property="appIcon" jdbcType="VARCHAR" />
    resultMap>
    <sql id="Base_Column_List">
        appId, appName, appSize, appVersion, appPath, appType,
        appCount,
        appNote,appCreateTime,appPkg,appVersionName,appPermissions,appIcon
    sql>
    
    <resultMap id="TypeAppListMap" type="com.mk.dao.vo.app.TypeApp"
        extends="BaseResultMap">
        <result column="typeName" property="appTypeName" jdbcType="VARCHAR" />
    resultMap>
    <sql id="Type_App_Column_List">
        a.appId, a.appName, a.appSize, a.appVersion, a.appPath,
        a.appType,a.appPkg,a.appIcon,
        a.appCount, a.appNote,a.appCreateTime,
        t.typeName
    sql>

    
    <insert id="insertSelective" parameterType="com.mk.dao.vo.app.App">
        insert into tbl_app
        <trim prefix="(" suffix=")" suffixOverrides=",">
            appId,appName,appSize,appVersion,appPath,appType,appCreateTime,
            <if test="appIcon !=null">
                appIcon,
            if>
            <if test="appPermissions !=null">
                appPermissions,
            if>
            <if test="appVersionName !=null">
                appVersionName,
            if>
            <if test="appPkg !=null">
                appPkg,
            if>
            <if test="appCount != null">
                appCount,
            if>
            <if test="appNote != null">
                appNote,
            if>
        trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{appId,jdbcType=VARCHAR},
            #{appName,jdbcType=VARCHAR},
            #{appSize,jdbcType=BIGINT},
            #{appVersion,jdbcType=INTEGER},
            #{appPath,jdbcType=VARCHAR},
            #{appType,jdbcType=INTEGER},
            #{appCreateTime,jdbcType=BIGINT},
            <if test="appIcon !=null">
                #{appIcon,jdbcType=VARCHAR},
            if>
            <if test="appPermissions !=null">
                #{appPermissions,jdbcType=BLOB},
            if>
            <if test="appVersionName !=null">
                #{appVersionName,jdbcType=VARCHAR},
            if>
            <if test="appPkg !=null">
                #{appPkg,jdbcType=VARCHAR},
            if>
            <if test="appCount != null">
                #{appCount,jdbcType=INTEGER},
            if>
            <if test="appNote != null">
                #{appNote,jdbcType=VARCHAR},
            if>
        trim>
    insert>

    
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        delete from
        tbl_app
        where appId = #{appId,jdbcType=VARCHAR}
    delete>

    
    <update id="updateByPrimaryKeySelective" parameterType="com.mk.dao.vo.app.App">
        update tbl_app
        <set>
            <if test="appIcon !=null">
                appIcon=#{appIcon,jdbcType=VARCHAR},
            if>
            <if test="appPkg !=null">
                appPkg=#{appPkg,jdbcType=VARCHAR},
            if>
            <if test="appVersionName !=null">
                appVersionName=#{appVersionName,jdbcType=VARCHAR},
            if>
            <if test="appPermissions !=null">
                appPermissions=#{appPermissions,jdbcType=BLOB},
            if>
            
            <if test="appSize != null">
                appSize = #{appSize,jdbcType=BIGINT},
            if>
            <if test="appVersion != null">
                appVersion = #{appVersion,jdbcType=INTEGER},
            if>
            <if test="appPath != null">
                appPath = #{appPath,jdbcType=VARCHAR},
            if>
            
            <if test="appCount != null">
                appCount = #{appCount,jdbcType=INTEGER},
            if>
            <if test="appNote != null">
                appNote = #{appNote,jdbcType=VARCHAR},
            if>
            <if test="appCreateTime != null and appCreateTime !=''">
                appCreateTime = #{appCreateTime,jdbcType=BIGINT},
            if>
        set>
        where appId = #{appId,jdbcType=VARCHAR}
    update>

    
    <select id="selectApp" resultMap="TypeAppListMap"
        parameterType="com.mk.dao.vo.param.SelectAppParam">
        SELECT
        <include refid="Type_App_Column_List" />
        FROM tbl_app AS a
        JOIN tbl_app_type AS t
        on a.appType = t.typeId
        WHERE
        a.appType = #{appType,jdbcType=INTEGER}
        <if test="appId !=null and appId !=''">
            AND a.appId=#{appId,jdbcType=VARCHAR}
        if>
    select>
mapper>

你可能感兴趣的:(生活上的那些事)