首页 > 代码库 > MyBatis增删改查模板

MyBatis增删改查模板

1. 首先,和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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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">
        
    <!-- c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"/>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${user}" />
        <property name="password" value="${password}"/>
    </bean>
    
    <!--mybatis  sessionFactory配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:cn/darrenchan/core/dao/*.xml"/>
        <property name="typeAliasesPackage" value="cn.darrenchan.core.bean"/>
    </bean>
    
    <!-- 扫包  -->
    <!-- 这里和我的云笔记的配置相比少一个 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage"  value="cn.darrenchan.core.dao"/>
    </bean>
    
</beans>

2. 接下来是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="cn.darrenchan.core.dao.product.BrandDao">
    <!-- resultMap该id对应下面select中的resultMap的值 -->
    <!-- column是数据库中的字段,property是实体类中的属性 -->
    <resultMap type="Brand" id="brand">
        <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="description" property="description" />
        <result column="img_url" property="imgUrl" />
        <result column="sort" property="sort" />
        <result column="is_display" property="isDisplay" />
    </resultMap>

    <!-- 查询品牌,get* -->
    <select id="getBrandListWithPage" parameterType="Brand"
        resultMap="brand">
        select id, name, description, img_url, sort, is_display
        from bbs_brand
        <where>
            <if test="name != null">
                name = #{name}
            </if>
            <if test="isDisplay != null">
                and is_display = #{isDisplay}
            </if>
        </where>
        order by id desc
        limit #{startRow},#{pageSize}
    </select>

    <!-- 查询总记录数 -->
    <select id="getBrandCount" parameterType="cn.darrenchan.core.bean.product.Brand"
        resultType="Integer">
        select count(1)
        from bbs_brand
        <where>
            <if test="isDisplay != null">
                is_display = #{isDisplay}
            </if>
            <if test="name != null">
                and name = #{name}
            </if>
        </where>
    </select>
<!-- 添加品牌 --> <insert id="addBrand" parameterType="Brand"> insert into bbs_brand <trim prefix="(" suffix=")"> name, description, img_url, sort, is_display </trim> values <trim prefix="(" suffix=")"> #{name}, #{description}, #{imgUrl}, #{sort}, #{isDisplay} </trim> </insert>
<!-- 单个删除 --> <delete id="deleteBrandByKey" parameterType="Integer"> delete from bbs_brand <where> id=#{id} </where> </delete> <!-- 批量删除 --> <delete id="deleteBrandByKeys" parameterType="Integer"> delete from bbs_brand <where> id in <foreach collection="array" item="id" open="(" close=")" separator=","> #{id} </foreach> </where> </delete> <!-- 修改 --> <update id="updateBrandByKey" parameterType="Brand"> update bbs_brand <set> <if test="name != null"> name = #{name}, </if> <if test="description != null"> description = #{description}, </if> <if test="imgUrl != null"> img_url = #{imgUrl}, </if> <if test="sort != null"> sort = #{sort}, </if> <if test="isDisplay != null"> is_display = #{isDisplay} </if> </set> <where> id=#{id} </where> </update> <!-- 通过ID查询一个品牌对象 --> <select id="getBrandByKey" parameterType="Integer" resultMap="brand"> select id, name, description, img_url, sort, is_display from bbs_brand <where> id=#{id} </where> </select> </mapper>

 

MyBatis增删改查模板