溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis動(dòng)態(tài)SQL操作之插入學(xué)習(xí)筆記

發(fā)布時(shí)間:2020-06-18 13:18:18 來(lái)源:網(wǎng)絡(luò) 閱讀:645 作者:知止內(nèi)明 欄目:MySQL數(shù)據(jù)庫(kù)

1


import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.itcast.javaee.mybatis.util.MybatisUtil;

/**
 * 持久層 
 * @author AdminTC
 */
public class StudentDao {
    /**
     * 插入學(xué)生
     */
    public void dynaInsert(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.insert("studentNamespace.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,"哈哈",7000D));//insert into 表名(*,*,*) values(?,?,?)
        //dao.dynaInsert(new Student(2,"哈哈",null));//insert into 表名(*,*) values(?,?)
        //dao.dynaInsert(new Student(3,null,7000D));//insert into 表名(*,*) values(?,?)
        dao.dynaInsert(new Student(4,null,null));//insert into 表名(*) values(?)
    }
}

2

<?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="studentNamespace">   

    <resultMap type="cn.itcast.javaee.mybatis.app14.Student" id="studentMap">
        <id property="id" column="students_id"/>
        <result property="name" column="students_name"/>
        <result property="sal" column="students_sal"/>
    </resultMap>

    <!-- sql片段對(duì)應(yīng)字段名,id屬性值任意 -->
    <sql id="key">
        <!-- 去掉最后一個(gè), -->
        <trim suffixOverrides=",">
            <if test="id!=null">
                students_id,
            </if>
            <if test="name!=null">
                students_name,
            </if>
            <if test="sal!=null">
                students_sal,
            </if>
        </trim>
    </sql>

    <!-- sql片段對(duì)應(yīng)?,id屬性值任意 -->
    <sql id="value">
        <!-- 去掉最后一個(gè) -trim suffixOverrides, -->
        <trim suffixOverrides=",">
            <if test="id!=null">
                #{id},
            </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="cn.itcast.javaee.mybatis.app14.Student">
        insert into students(<include refid="key"/>) values(<include refid="value"/>)
    </insert>

</mapper>
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI