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 mapperPUBLIC "-//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="Object" >update t_users set <trim  suffixOverrides="," ><if test="name != null  and name != '' ">name=#{name},</if><if test="sex != null  and sex != '' ">sex=#{sex},</if><if test="birs != null  ">birs=#{birs},</if><if test="message != null  and message != '' ">message=#{message},</if></trim> where id=#{id}</update><!-- 4.根据id进行删除 --><delete id="delete" parameterType="Object">delete from t_users where id = #{id}</delete><!-- 5.根据id查询 --><select id="queryById" resultMap="BaseResultMap" parameterType="Object">select<include refid="Base_Column_List" />from t_users where id = #{id}</select><!-- 6.查询列表,只查询不为空的字段 --><select id="queryBySelective" resultMap="BaseResultMap" parameterType="Object">select<include refid="Base_Column_List" />from t_users<include refid="Example_Where_Clause" /></select><!-- 7.列表总数 --><select id="queryByCount" resultType="java.lang.Integer" parameterType="Object">select count(1) from t_users<include refid="Example_Where_Clause" /></select><!-- 8.查询列表 --><select id="queryByList" resultMap="BaseResultMap" parameterType="Object">select<include refid="Base_Column_List" />from t_users <include refid="Example_Where_Clause"/></select>
</mapper>

Mapper类接口

package examples.mapper;import java.util.List;public interface UsersMapper<T> {public void add(T t);public void update(T t);public void updateBySelective(T t);public void delete(Object id);public T queryById(Object id);public List<T> queryBySelective(T t);public int queryByCount(T t);public List<T> queryByList(T t);}

类型转换工具类

package examples.service;import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import oracle.sql.CLOB;import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;public class OracleClobTypeHandler implements TypeHandler<Object> {public Object valueOf(String param) {return null;}@Overridepublic Object getResult(ResultSet arg0, String arg1) throws SQLException {CLOB clob = (CLOB) arg0.getClob(arg1);return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length());}@Overridepublic Object getResult(ResultSet arg0, int arg1) throws SQLException {return null;}@Overridepublic Object getResult(CallableStatement arg0, int arg1) throws SQLException {return null;}@Overridepublic void setParameter(PreparedStatement arg0, int arg1, Object arg2, JdbcType arg3) throws SQLException {CLOB clob = CLOB.empty_lob();clob.setString(1, (String) arg2);arg0.setClob(arg1, clob);}
}

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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-autowire="byType"><!-- 配置数据源 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="url"><value>jdbc:oracle:thin:@127.0.0.1:1521:pms</value></property> <property name="username"><value>pms</value></property> <property name="password"><value>pms</value></property></bean><!-- 配完数据源 和 拥有的 sql映射文件 sqlSessionFactory 也可以访问数据库 和拥有 sql操作能力了 -->
<!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml"/></bean>--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mapperLocations"><list><value>classpath:examples/mybatis/oracle/UsersMapper.xml</value></list></property></bean><!-- 通过设置 mapperInterface属性,使接口服务bean 和对应xml文件管理 可以使用其中的sql --><bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean"><!-- 此处等同于 Mybatis 中 ServerDao serverDao = sqlSession.getMapper(ServerDao.class); 指明映射关系 --><property name="mapperInterface" value="examples.mapper.UsersMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>
</beans>

测试类

package examples.service;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import examples.bean.Users;
import examples.mapper.UsersMapper;public class TestUsersService {@SuppressWarnings("unchecked")public static void main(String[] args) throws ParseException {ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/examples/service/spring.xml");UsersMapper<Users> dao = (UsersMapper<Users>)ac.getBean("dao");//删除表中所有信息Users nullBean = new Users();List<Users> delList = dao.queryByList(nullBean);if(delList != null) {for(Users user : delList) {dao.delete(user.getId());}}//新增Users bean = new Users();bean.setName("ding");bean.setSex("男");bean.setBirs(new SimpleDateFormat("yyyy-MM-dd").parse("1985-01-01"));bean.setMessage("This is Clob!");dao.add(bean);List<Users> list = dao.queryByList(nullBean);if(list != null) {for(Users user : list) {System.out.println(user);}}//查询并更新bean = new Users();bean.setName("ding");List<Users> queList = dao.queryByList(bean);if(queList != null) {for(Users user : list) {user.setSex("女");dao.updateBySelective(user);}}list = dao.queryByList(nullBean);if(list != null) {for(Users user : list) {System.out.println(user);}}//查询并更新bean = new Users();bean.setName("ding");List<Users> queList2 = dao.queryByList(bean);if(queList != null) {for(Users user : queList2) {user.setSex("男");user.setMessage("");dao.update(user);}}list = dao.queryByList(nullBean);if(list != null) {for(Users user : list) {System.out.println(user);}}int num = dao.queryByCount(nullBean);System.out.println("num=" + num);}}

转载于:https://www.cnblogs.com/davidwang456/p/5599683.html

Mybatis之Oracle增删查改示例--转相关推荐

  1. oracle是CLOB类型mybatis,Mybatis之Oracle增删查改示例(含Date、Clob数据类型操作)

    oracle表结构 create table T_USERS ( ID NUMBER not null, NAME VARCHAR2(30), SEX VARCHAR2(3), BIRS DATE, ...

  2. 最全的Spring Boot +Mybatis 简单的增删查改

    在resources包下创建mapping包然后创建UserMapper.xml UserMapper.xml <?xml version="1.0" encoding=&q ...

  3. (4) hibernate增删查改+批量操作+类似Mybatis动态sql

    简介 采用spring + hibernate + freemaker+ maven搭建起来的一个hibernate增删查改和 类似mybatis动态sql查询的一个案例 增删查改demo + 动态s ...

  4. MyBatis实现数据的增删查改

    MyBatis的配置请参考我的上一篇文章,在上一篇文章的基础上我们实现数据的增删查改. 创建实现增删查改的xml文件,这里才是真正实现增删查改的文件. 创建完后要在配置文件中注册创建好的xml文件: ...

  5. oracle存储过程之数据增删查改及调用

    本文转载自:https://blog.csdn.net/u011955534/article/details/14647217 原文更易读,请参与原文. 存储过程: 存储过程是 SQL, PL/SQL ...

  6. mybatis 介绍 入门 mapper配置文件 增删查改 别名配置 #和 $的区别

    mybatis 介绍 今天,一起来说说mybits这个框架吧.这是一个持久层的框架.之前叫做ibatis. 所以,在它的代码中出现ibatis这个词的时候,不要感到惊讶.不是写错了,它确实就是这个样子 ...

  7. Mybatis、SpringBoot入门实战(微型项目) -- Mysql增删查改、写接口、测试接口

    Mybatis入门实战(微型项目) – Mysql增删查改.写接口.测试接口 开发环境: 1.Window10 v1909 2.idea 2019 3.jdk 1.8 4.mybatis 3.5.5 ...

  8. java调用js查询mongo_MongoDB增删查改操作示例【基于JavaScript Shell】

    本文实例讲述了MongoDB增删查改操作.分享给大家供大家参考,具体如下: MongoDB自带了一个JavaScript Shell,所以在其中使用js语法是可以的. Insert操作: 单条插入 v ...

  9. spring和mybatis结合做简单的增删查改系统_springbootamp;amp;vue简单的景点信息管理系统...

    springboot&&vue简单的景点信息管理系统 这两天闲着没有什么事,就根据陈哥的教程,试着写了一个springboot和vue的简单的景点信息管理系统.也就大致实现了最基本的增 ...

最新文章

  1. MySQL关于check约束无效的解决办法
  2. .NET:动态代理的 “5 + 1” 模式
  3. 第三章:ioctl 函数详解
  4. appcan+html查看效果,appcan常用的窗口方法
  5. JAVA随机存储_java-如何将随机整数存储到类的实例中
  6. 通信 / HTTPS 过程详解
  7. 语法分析器c语言 递归子程序,RecursiveSubroutine
  8. iOS中常用的正则表达式
  9. Mr.J--.c 和.cpp 后缀详解
  10. MyEclipse中将项目的编码从默认GBK改变为默认UTF-8
  11. android 开发套件_Android套件
  12. C++ string与vectorfloat类型相互转换之stringstream
  13. 使用shadow dom封装web组件
  14. 09-网格划分质量查看
  15. 乱谈企业化信息规划与实施
  16. csdn如何上传附件
  17. 收集整理的一些windows好用的工具(持续更新)
  18. Linux之iptables防火墙
  19. nic占用率很高 Linux,linux – 许多丢弃了我的NIC数据包
  20. org.apache.ibatis.builder.IncompleteElementException:Could not find result map cn.lyp.entity.Book

热门文章

  1. iis php session丢失,Session丢失的解决办法小结
  2. 笔记本电脑性价比排行2019_办公笔记本电脑排名2019 五款适合办公的笔记本电脑推荐...
  3. cath数据库fasta备注_数据库(同源)搜索软件 FASTA 和 BLAST
  4. java int == integer_java int与integer的区别
  5. java 雷电_【我叫辰先森】教大家用js写一个雷电
  6. Java高阶代码_Java高阶语法---Volatile
  7. bash 判断 os 版本_鸿蒙OS手机将至,华为手机用户提问,老款机器能升级吗?
  8. android gridview显示本地图片大小,在Android上的GridView中调整图像大小
  9. java src 文件路径_java中获得src路径下文件的常用方法
  10. CCF 202104 Python