需求:向数据库中插入一条数据

//id,name,sal非空,三个字段都插入
insert into student(id,name,sal) values (?,?,?)
//id,name非空,只对id和name插入值
insert into student(id,name) values (?,?)
//id,sal非空
insert into student(id,sal) values (?,?)

为null的字段忽略掉.

<?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"> <!--
namespace的取值可以是实体的全限定名,这样有好处!
-->
<mapper namespace="com.winner.entity.Student">
<resultMap id="studentMap" type="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap>
<!-- sql片段对应字段名,id属性值任意 -->
<sql id="key">
<!-- 去掉最后一个, -->
<trim suffixOverrides=",">
       //id是类的属性名
<if test="id!=null">
         //id是表的字段名
id,</if>
       //name是类的属性名
<if test="name!=null">
         //name是表的字段名
name,
</if>
       //sal是类的属性名
<if test="sal!=null">
         //sal是表的字段名
sal,
</if>
</trim>
</sql> <!-- sql片段对应?,id属性值任意 -->
<sql id="value">
<!-- 去掉最后一个, -->
<trim suffixOverrides=",">
<if test="id!=null">
#{id},//肯定是调用了get方法得到的值,所以是实体类中的属性名
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal},
</if>
</trim>
</sql>
<!-- <include refid="key"/>和<include refid="value"/>表示引用上面定义的sql片段 -->
<insert id="dynaInsert" parameterType="Student">
INSERT INTO student(<include refid="key"/>) VALUES(<include refid="value"/>)
</insert>
</mapper>
public class StudentDao {
/**
* 插入学生
*/
public void dynaInsert(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.insert(Student.class.getName() + ".dynaInsert", student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
} public static void main(String[] args) throws Exception{
StudentDao dao = new StudentDao();
//dao.dynaInsert(new Student(1,"zhangsan1",1000d));//insert into 表名(*,*,*) values(?,?,?)
//dao.dynaInsert(new Student(3,"lisi",null));//insert into 表名(*,*) values(?,?)
dao.dynaInsert(new Student(5,null,7000D));//insert into 表名(*,*) values(?,?)
//dao.dynaInsert(new Student(4,null,null));//insert into 表名(*) values(?)
}
}

MyBatis的动态SQL操作--插入的更多相关文章

  1. MyBatis的动态SQL操作--查询

    查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,

  2. mybatis之动态SQL操作之更新

    1)  更新条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层*/ public class StudentDao { /** * 动态SQL--更新 */ public ...

  3. mybatis之动态SQL操作之查询

    1)  查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层 * @author AdminTC */ public class StudentDao { /** * ...

  4. mybatis之动态SQL操作之插入

    1)  根据条件,插入一个学生 /** * 持久层*/ public class StudentDao { /** * 动态SQL--插入 */ public void dynaSQLwithInse ...

  5. MyBatis的动态SQL操作--删除

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUYAAAC/CAIAAAANX+LCAAAYvElEQVR4nO2dWWycV9nHDyC6UEGBGy

  6. MyBatis的动态SQL操作--更新

    更新条件不确定,需要根据具体的情况生成sql语句. id是主键,一般不会去更新. 1.只更新name的值 update student set name = ? where id = ? 2.只更新s ...

  7. mybatis之动态SQL操作之删除

    /** * 持久层 */ public class StudentDao { /** * 动态SQL--删除 */ public void dynaSQLwithDelete(int... ids) ...

  8. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  9. Java-MyBatis:MyBatis 3 动态 SQL

    ylbtech-Java-MyBatis:MyBatis 3 动态 SQL 1.返回顶部 1. 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架 ...

随机推荐

  1. L005-oldboy-mysql-dba-lesson05

    L005-oldboy-mysql-dba-lesson05 在线改表工具:pt-onine-schema-change 来自为知笔记(Wiz)

  2. 从对偶问题到KKT条件

    转自:http://xuehy.github.io/%E4%BC%98%E5%8C%96/2014/04/13/KKT/ 从对偶问题到KKT条件 Apr 13, 2014 对偶问题(Duality) ...

  3. Unity学习笔记(2):注册映射

    在上一篇文章中(认识Unity)中概要介绍了Unity和Ioc,本节主要介绍IoC中的注册映射,并使用代码和配置文件两种方式进行说明. 定义依赖注入相关信息 定义ILogger接口 public in ...

  4. [大牛翻译系列]Hadoop(6)MapReduce 排序:总排序(Total order sorting)

    4.2.2 总排序(Total order sorting) 有的时候需要将作业的的所有输出进行总排序,使各个输出之间的结果是有序的.有以下实例: 如果要得到某个网站中最受欢迎的网址(URL),就需要 ...

  5. 35 个必须有的Bootstrap工具和生成器

    Bootstraptor If you think that bootstrap templates are not enough for you, you should go with bootst ...

  6. Spark Streaming揭秘 Day12 数据安全容错(Executor篇)

    Spark Streaming揭秘 Day12 数据安全容错(Executor篇) 今天,让我们研究下SparkStreaming在Executor端的数据安全及容错机制. 在SparkStreami ...

  7. DB2分区表删除和添加分区

    1.数据库版本 2.具体procedure DROP PROCEDURE DB2USER.TOOLS_PARTITION_TABLE_SHOW (VARCHAR ()); )) /********** ...

  8. c语言背后的运行机制

    目的:通过分析c语言转换成汇编代码后的执行过程对汇编语言和X86构架有一个初步认识 实验代码 #include <stdio.h> int g(int x) { ; } int f(int ...

  9. closest()一个在评论里很有用的函数

    实例 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("click", fu ...

  10. iOS 基础 第五天(0811)

    0811 ARC ARC判断准则:只要没有强指针指向对象,就会释放对象 指针 指针分两种: 强指针:默认情况下,搜有的指针都是强指针 弱指针:week修饰(一个是控件,一个是delegate代理) 循 ...