zoukankan      html  css  js  c++  java
  • Mybatis学习笔记15

    两个内置参数:
    除了方法传递过来的参数可以被用来判断,取值外,mybatis默认还有两个内置参数:
    _parameter:代表整个参数
    单个参数:_parameter就代表这个单个参数
    多个参数:参数会被封装为一个map;_parameter就代表这个map
    _databaseId:如果配置了databaseIdProvider标签。_databaseId就代表当前数据库的别名mysql。

    示例代码:

    接口定义:
    package com.mybatis.dao;
    
    import com.mybatis.bean.Employee;
    
    import java.util.List;
    
    public interface EmployeeMapper {
        public List<Employee> getEmpsTestInnerParameter(Employee employee);
    }
    
    
    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">
        <!-- 
            两个内置参数:
    	 	除了方法传递过来的参数可以被用来判断,取值外。
    	 	mybatis默认还有两个内置参数:
    	 	_parameter:代表整个参数
    	 		单个参数:_parameter就是这个参数
    	 		多个参数:参数会被封装为一个map;_parameter就是代表这个map
    	 	
    	 	_databaseId:如果配置了databaseIdProvider标签。
    	 		_databaseId就是代表当前数据库的别名oracle
    	  -->
        <select id="getEmpsTestInnerParameter" resultType="com.mybatis.bean.Employee">
            <if test="_databaseId=='mysql'">
                select * from tbl_employee
                <if test="_parameter!=null">
                    where last_name = #{_parameter.lastName}
                </if>
            </if>
            <if test="_databaseId=='oracle'">
                select * from employees
                <if test="_parameter!=null">
                    where last_name = #{_parameter.lastName}
                </if>
            </if>
        </select>
    </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> list = mapper.getEmpsTestInnerParameter(null);
                for (Employee emp : list) {
                    System.out.println(emp);
                }
            } finally {
                openSession.close();
            }
        }
    }
    
  • 相关阅读:
    Arial Monospaced
    也玩有道难题的双立方数问题:Python 版解法
    用 VB.NET 实现的非确定性计算例子
    Gestalt 在浏览器里用 python/ruby 写客户端脚本
    非确定性计算引擎转化为C#版本并重构
    再谈C#的装箱和拆箱
    谈谈.Net 4.0 中的委托delegate
    在C#中使用SqlDbType.Xml类型参数
    java cmd编译class文件
    Mysql、Oracle插表关于主键的处理
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10351680.html
Copyright © 2011-2022 走看看