Mybatis之Oracle增删查改示例--转
Posted 一天不进步,就是退步!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis之Oracle增删查改示例--转相关的知识,希望对你有一定的参考价值。
http://blog.csdn.net/bingjie1217/article/details/21088431?utm_source=tuicool&utm_medium=referral
oracle表结构
create table T_USERS
(
ID NUMBER not null,
NAME VARCHAR2(30),
SEX VARCHAR2(3),
BIRS DATE,
MESSAGE CLOB
)
create sequence SEQ_T_USERS_ID
minvalue 1
maxvalue 99999999
start with 1
increment by 1
cache 20;
配置mybatis配置文件UsersMapper.xml
<?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="examples.mapper.UsersMapper" >
<!-- Result Map-->
<resultMap type="examples.bean.Users" id="BaseResultMap">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="birs" column="birs" jdbcType="TIMESTAMP"/>
<result property="message" column="message" jdbcType="CLOB"
javaType = "java.lang.String" typeHandler ="examples.service.OracleClobTypeHandler"/>
</resultMap>
<sql id="Tabel_Name">
t_users
</sql>
<!-- 表中所有列 -->
<sql id="Base_Column_List" >
id,name,sex,birs,message
</sql>
<!-- 查询条件 -->
<sql id="Example_Where_Clause">
where 1=1
<trim suffixOverrides=",">
<if test="id != null">
and id = #{id}
</if>
<if test="name != null and name != ‘‘">
and name like concat(concat(‘%‘, ‘${name}‘), ‘%‘)
</if>
<if test="sex != null and sex != ‘‘">
and sex like concat(concat(‘%‘, ‘${sex}‘), ‘%‘)
</if>
<if test="birs != null">
and birs = #{birs}
</if>
<if test="message != null">
and message = #{message}
</if>
</trim>
</sql>
<!-- 1.新增记录 -->
<insert id="add" parameterType="Object" >
<selectKey resultType="int" order="BEFORE" keyProperty="id">
select seq_t_users_id.nextval as id from dual
</selectKey>
insert into t_users(id,name,sex,birs,message) values(#{id},#{name},#{sex},#{birs},#{message,jdbcType=CLOB})
</insert>
<!-- 2.根据id修改记录-->
<update id="update" parameterType="Object" >
update t_users set name=#{name},sex=#{sex},birs=#{birs},message=#{message} where id=#{id}
</update>
<!-- 3.只修改不为空的字段 -->
<update id="updateBySelective" parameterType=