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增删改查模板的更多相关文章
- 【Mybatis】简单的mybatis增删改查模板
简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...
- MyBatis增删改查
MyBatis的简介: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...
- 最简单的mybatis增删改查样例
最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...
- SpringBoot+Mybatis增删改查实战
简介 SpringBoot和Mybatis是啥请自行百度,作者这里也是花了几天时间入门了这个框架用来完成任务,并且也算符合要求的完成了任务,期间也各种百度但是没找到自己想要的那种简单易懂的教程,所以踩 ...
- Mybatis增删改查,Demo整合
第一步:MyBatis的Jar包引入mybatis-3.2.7.jarmysql-connector-java-5.1.8.jar MyBatis的pom.xml依赖 <dependencies ...
- Mybatis增删改查(CURD)
前面的小节我们已经讲到用接口的方式编程.使用这种方式,需要注意的一个地方就是,在User.xml 配置文件中,mapper namespace="com.yiibai.mybatis.int ...
- springboot整合mybatis增删改查(四):完善增删改查及整合swgger2
接下来就是完成增删改查的功能了,首先在config包下配置Druid数据连接池,在配置之前先把相关配置在application.preperties中完善 application.preperties ...
- springboot&mybatis 增删改查系列(二)
数据库篇 我的数据库名为data0525,数据表名为user,其中有五列uid,uname,upass,usex,umessage.uid为主键并且自动生成,由于是练习表,所以并没有考虑设计的合理性. ...
- springboot2之结合mybatis增删改查解析
1. 场景描述 本节结合springboot2.springmvc.mybatis.swagger2等,搭建一个完整的增删改查项目,希望通过这个基础项目,能帮忙朋友快速上手springboot2项目. ...
随机推荐
- 【精】iOS6 及其以上版本号自己主动旋转、手动强制旋转方案及布局适配
1.布局适配方式 本文不讨论哪种布局适配方式最好.此处使用的是 Masonry 纯代码布局适配. (Masonry 底层就是 AutoLayout 的 NSLayoutConstraint) 2.iO ...
- 智能化的命令行工具-betty
本文首先公布在 CSDN的文章编辑器实在太烂了,建议大家用Cmd Markdown.它不但支持markdown.还支持VIM模式. 智能化的命令行工具-betty betty tool command ...
- 《iOS用户体验》总结与思考-改动版
假设转载此文.请注明出处:http://blog.csdn.net/paulery2012/article/details/25157347,谢谢. 前言: 本文是在阅读<ios用户体验> ...
- JVM: can not create native thread
检查系统限制,并根据需要修改过低的值: echo "100000" > /proc/sys/kernel/threads-max echo "100000" ...
- android中listview点击事件失效的灵异事件
首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAda ...
- linux 打包和压缩文件
打包成tar文件 tar -cf mydir.tar mydir/ 打包tar压缩成gz tar -czf mydir.tar.gz mydir/ 解压mydirtar文件 tar -xvf mydi ...
- 每日一Vim(1)
来自 上一篇讲过了Vim的基本操作命令(打开,编辑,保存退出)以及Vim的三种基本模式和光标的基本导航(hjkl),今天讲一些稍微高级点的光标移动,以及一些基本的文本操作命令. 翻一页/半页 对于一个 ...
- Entity Framework Code First关系映射约定【l转发】
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
- yum groupinstall报错,处理方法
http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/21/3089970.html ===== 创建repo库 # createrepo - ...
- Nutch相关框架安装使用最佳指南(转帖)
Nutch相关框架安装使用最佳指南 Chinese installing and using instruction - The best guidance in installing and u ...