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. MongoDB 基础命令 (MongoDB Shell)

    1.我们 mongodb 安装成功后,用上一篇的方法启动 mongodb服务 然后使用 mongodb shell 来做数据库的增删改查 2.创建数据库 语法: use 数据库名称 案例: > ...

  2. java对象的强引用,软引用,弱引用和虚引用

    1.强引用 以前我们使用的大部分引用实际上都是强引用,这是使用最普遍的引用.如果一个对象具有强引用,那就类似于必不可少的生活用品,垃圾回收器绝不会回收它.当内存空 间不足,Java虚拟机宁愿抛出Out ...

  3. Cocos2d-x 处理双击事件的两种方法

    在cocos2d-x的开发过程中有些时候也是需要用到双击的事件处理,那么由于在cocos2d-x中没有实现对双击的事件的处理,那么我们就需要自己用代码实现. 下面介绍两种方式实现双击事件的处理. (一 ...

  4. 算法笔记_046:跳台阶问题(Java)

    目录 1 问题描述 2 解决方案 2.1 递归法 2.2 迭代法   1 问题描述 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级,求总共有多少种跳法. 2 解决方案 2.1 递归法 如果整个台 ...

  5. JUnit 3.8 让所有测试程序 实现 复合的测试(TestSuite)

    之前是单个单个程序测试,这种方式在测试类比较少的时候可行, 但测试类多了,单个单个的这个测试方式就不推荐了,那得使用 复合的测试了 一个TestSuite是一个复合的测试.它运行测试用例集.   这个 ...

  6. SqlServer字段说明查询及快速查看表结构

    SqlServer字段说明查询 SELECT t.[name] AS 表名,c.[name] AS 字段名,cast(ep.[value] )) AS [字段说明] FROM sys.tables A ...

  7. 前端project与性能优化(长文)

    原文链接:http://fex.baidu.com/blog/2014/03/fis-optimize/ 每一个參与过开发企业级 web 应用的前端project师也许都曾思考过前端性能优化方面的问题 ...

  8. chrome 谷歌浏览器插件损坏

      Axure RP Extension for Chrome已停用 CreateTime--2017年7月4日10:19:34Author:Marydon 参考地址:http://blog.csdn ...

  9. 【Linux】cat命令

    用途 cat用于将一个档案的内容连续的打印在屏幕上 全称 cat的全称是Conctaenate 参数 -A :相当于-vTE的整合选项,可列出一些特殊字符而不是空白而已 -b :列出行号,仅针对非空白 ...

  10. VLC命令行参数详解

    VLC命令行参数详解 2012-11-29 14:00 6859人阅读 评论(0) 收藏 举报 Usage: vlc [options] [stream] ...You can specify mul ...