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. k-means聚类算法C++实现

    原文:http://www.cnblogs.com/luxiaoxun/archive/2013/05/09/3069594.html Clustering 中文翻译作“聚类”,简单地说就是把相似的东 ...

  2. java Socket通信使用BufferedReader和BufferedWriter的注意事项

    注意事项:readLine()要求有换行标识,write()要输出换行标识,要调用flush()刷新缓冲区. 以下是取自java socket通信中的一小段代码. BufferedReader rea ...

  3. es5||es6 - array

    导航目录 /** * javascript - array * * ES5: * join() * push() * pop() * shift() * unshift() * sort() * re ...

  4. javascript 创建对象的几种方式

    1. //基于已有对象扩充其属性和方法var object = new Object(); object.name = "zhangsan"; object.sayName = f ...

  5. 如何配置 Oracle VirtualBox 中的客户机与物理机网络

    当你在 Oracle VirtualBox 虚拟机软件 中安装了各种操作系统时,你可能需要实现物理机与虚拟机之间的相互访问. 在这篇文章中,我们将会以最简单明了的方式来说明如何配置客户机与 Linux ...

  6. nyoj847 S + T(贪心)

    题目847 题目信息 执行结果 本题排行 讨论区 S + T 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 给你一个长度为n的整数序列A1.A2,--,An,找出两个 ...

  7. 【TP5.0】页面布局,引入公共的模版文件

    1.实例:如后台admin模块,公用一个header.html和footer.hml 2.模块结构: 3.使用方式: {include file="common/header"} ...

  8. PHP-静态方法(static)继承等分析

    <?php class A { const CONST_NAME = 'A'; public static $static_name = 'A'; public static $static_n ...

  9. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  10. EM 算法 实例

    #coding:utf-8 import math import copy import numpy as np import matplotlib.pyplot as plt isdebug = T ...