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增删改查模板的更多相关文章

  1. 【Mybatis】简单的mybatis增删改查模板

    简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  2. MyBatis增删改查

    MyBatis的简介: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  3. 最简单的mybatis增删改查样例

    最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...

  4. SpringBoot+Mybatis增删改查实战

    简介 SpringBoot和Mybatis是啥请自行百度,作者这里也是花了几天时间入门了这个框架用来完成任务,并且也算符合要求的完成了任务,期间也各种百度但是没找到自己想要的那种简单易懂的教程,所以踩 ...

  5. Mybatis增删改查,Demo整合

    第一步:MyBatis的Jar包引入mybatis-3.2.7.jarmysql-connector-java-5.1.8.jar MyBatis的pom.xml依赖 <dependencies ...

  6. Mybatis增删改查(CURD)

    前面的小节我们已经讲到用接口的方式编程.使用这种方式,需要注意的一个地方就是,在User.xml 配置文件中,mapper namespace="com.yiibai.mybatis.int ...

  7. springboot整合mybatis增删改查(四):完善增删改查及整合swgger2

    接下来就是完成增删改查的功能了,首先在config包下配置Druid数据连接池,在配置之前先把相关配置在application.preperties中完善 application.preperties ...

  8. springboot&mybatis 增删改查系列(二)

    数据库篇 我的数据库名为data0525,数据表名为user,其中有五列uid,uname,upass,usex,umessage.uid为主键并且自动生成,由于是练习表,所以并没有考虑设计的合理性. ...

  9. springboot2之结合mybatis增删改查解析

    1. 场景描述 本节结合springboot2.springmvc.mybatis.swagger2等,搭建一个完整的增删改查项目,希望通过这个基础项目,能帮忙朋友快速上手springboot2项目. ...

随机推荐

  1. Android通过反射打造能够存储不论什么对象的万能SharedPreferences

    我们通常使用SharedPreferences存储一些须要保存在本地.但又不至于存储在数据库里的一些数据.一般我们用它来存储一些username,password等数据是很方便的,那么假设我们想要存储 ...

  2. (1)风色从零单排《C++ Primer》 一个简单的c++程序

    从零单排<C++ Primer> --(1)一个简单的c++程序 本次学习收获 0.写在前面 风色以前上过C++的课程,然而当时并没有认真去学,基本不能使用c++来作项目开发. 这次又一次 ...

  3. wkhtmltoimage(网页剪切功能)

    1.wkhtmltoimage使用wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2版本,最新版本为wkhtmltoimage-0.11.0_rc1-stati ...

  4. 随机数的生成:给定1-n的随机数生成器randn(),生成1-m的随机数

    1.当m < n时比较简单: 只当randn()生成的数落在1-m上时,就输出,否则继续生成: 2.当m > n时就比较麻烦一点, 基本思路还是和第一种情况是一样的,问题是怎样才能利用ra ...

  5. 苹果开发——App内购以及验证store的收据(一)

    原地址:http://zengwu3915.blog.163.com/blog/static/27834897201375105236580?suggestedreading 发了几天时间在网上折腾了 ...

  6. 算法笔记_012:埃拉托色尼筛选法(Java)

    1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...

  7. Oracle学习(五):多表查询

    1.知识点:能够对比以下的录屏进行阅读 SQL> --等值连接 SQL> --查询员工信息: 员工号 姓名 月薪 部门名称 SQL> select empno,ename,sal,d ...

  8. 安装SQL SERVER 2016 CTP (二)[多图]

    内容中包含 base64string 图片造成字符过多,拒绝显示

  9. NSArray利用Cocoa框架进行汉字排序

    NSArray利用Cocoa框架进行汉字排序 在NSString有一个函数localizedCompare:,它的功能是通过自身与给定字符串的比較,返回一个本地化的比較结果.也就是说这个函数是支持汉字 ...

  10. getAttribure()和getParameter()的区别

    1.getAttribute是取得jsp中 用setAttribute设定的attribute 2.parameter得到的是string:attribute得到的是object 3.request. ...