• Mybatis学习笔记17


    示例代码:

    接口定义:
    package com.mybatis.dao;
    
    import com.mybatis.bean.Employee;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface EmployeeMapper {
        public void addEmps(@Param("emps") List<Employee> emps);
    }
    
    
    mapper定义:
    <?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="com.mybatis.dao.EmployeeMapper">
        <insert id="addEmps">
            insert into tbl_employee(
                <include refid="insertSql"/>
            )
            values
            <foreach collection="emps" item="emp" separator=",">
                (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
            </foreach>
        </insert>
    
    
        <!--
            抽取可重用的sql片段。方便后面引用
            1、sql抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用
            2、include来引用已经抽取的sql:
            3、include还可以自定义一些property,sql标签内部就能使用自定义的属性
                    include-property:取值的正确方式${prop},
                    #{不能使用这种方式}
        -->
    
        <sql id="insertSql">
            last_name, email,gender, d_id
        </sql>
    </mapper>
    
    
    测试代码:
    package com.mybatis.demo;
    
    import com.mybatis.bean.Department;
    import com.mybatis.bean.Employee;
    import com.mybatis.dao.EmployeeMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyTest {
        public SqlSessionFactory getSqlSessionFactory() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            return new SqlSessionFactoryBuilder().build(inputStream);
        }
    
        @Test
        public void test() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession(true);
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                List<Employee> emps = new ArrayList<Employee>();
                emps.add(new Employee(null, "sujun", "sujun@qq.com", 1, new Department(1)));
                emps.add(new Employee(null, "minmin", "minmin@qq.com", 1, new Department(2)));
                mapper.addEmps(emps);
            } finally {
                openSession.close();
            }
        }
    }
    
  • 相关阅读:
    Cordova/Cordova.h file not found的解决方法
    使用MethodSwizzle导致按home app进入后台或者app间切换发生crash的解决方法
    基于iOS上MDM技术相关资料整理及汇总
    iOS集成微信支付
    iOS集成支付宝支付
    最新apple邓白氏码申请地址
    游戏的定价
    许久未更,随便侃侃
    记一个有趣的梦
    由《掟上今日子的备忘录》引发的联想
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10352044.html
Copyright © 2020-2023  润新知