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项目. ...
随机推荐
- (C++)C++多态性中的静态绑定和动态绑定
静态绑定和动态绑定是C++多态性的一种特性. 1.对象的静态类型和动态类型: 对象的静态类型: 对象在声明是采用的类型,在编译期确定: 对象的动态类型: 当前对象所指的类型,在运行期决定,对象的动态类 ...
- js看起来比较怪异的写法 (综合)
1.$(function() {}中$是什么意思? <script type="text/javascript"> $(function(){ $("#tre ...
- Unity骨骼动力学应用
原地址:http://blog.csdn.net/libeifs/article/details/7169794 开发环境 Window7 Unity3D 3.4.1 MB525defy Andro ...
- Struts2之文件上传(单文件/多文件)
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- javascript 面向对象编程(工厂模式、构造函数模式、原型模式)
javascript 面向对象编程(工厂模式.构造函数模式.原型模式) CreateTime--2018年3月29日17:09:38 Author:Marydon 一.工厂模式 /** * 工厂模 ...
- cpu_test
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Android应用开发揭秘之优化技术
2013-06-28 第15章 优化技术 不管用什么语言进行开发,所有的优秀代码都会展示出共有的经典品质: 简练,可读性强,模块化,层次性,设计良好,高效,优雅,清晰等. Java程序员能够依 ...
- myeclipse2014安装jad反编译插件
myeclipse上默认不能查看class文件,需要查看的话安装反编译插件 安装步骤: 准备图中框里的两个文件 1. [net.sf.jadclipse_3.3.0.jar]文件拷贝到如下路径([D: ...
- 一对一关系数据库表 java类描述
一对一关系中 从表的主键是 主表的外键 sql语句 create table person( id int primary key, name varchar(100) ); create table ...
- 一个Keygen,参考参考
看到一个Keygen,我觉得还可以,参考参考 //////////////////////////////////////////////////////////////////// //// key ...