博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis学习笔记16 - bind标签
阅读量:5167 次
发布时间:2019-06-13

本文共 5652 字,大约阅读时间需要 18 分钟。

1、${}拼串进行模糊查询,不安全

示例代码:

接口定义:package com.mybatis.dao;        import com.mybatis.bean.Employee;        import java.util.List;public interface EmployeeMapper {    public List
getEmpsTestInnerParameter(Employee employee);}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); Employee employee = new Employee("e"); List
list = mapper.getEmpsTestInnerParameter(employee); for (Employee emp : list) { System.out.println(emp); } } finally { openSession.close(); } }}

 2、使用bind标签进行模糊查询

示例代码一:

接口定义:package com.mybatis.dao;import com.mybatis.bean.Employee;import java.util.List;public interface EmployeeMapper {    public List
getEmpsTestInnerParameter(Employee employee);}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); Employee employee = new Employee("e"); List
list = mapper.getEmpsTestInnerParameter(employee); for (Employee emp : list) { System.out.println(emp); } } finally { openSession.close(); } }}

示例代码二:

接口定义:package com.mybatis.dao;import com.mybatis.bean.Employee;import java.util.List;public interface EmployeeMapper {    public List
getEmpsTestInnerParameter(Employee employee);}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); Employee employee = new Employee("i"); List
list = mapper.getEmpsTestInnerParameter(employee); for (Employee emp : list) { System.out.println(emp); } } finally { openSession.close(); } }}

 

转载于:https://www.cnblogs.com/xidian2014/p/10352034.html

你可能感兴趣的文章
Struts 2 常用技术
查看>>
树形DP
查看>>
Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法
查看>>
python flask解决上传下载的问题
查看>>
博客园原始直链视频插入
查看>>
语法测试
查看>>
代码高亮测试
查看>>
CES1
查看>>
CES2
查看>>
python 数据类型_数组和元组
查看>>
python 数据类型_整数_浮点数
查看>>
数据结构----prim算法 最小生成树
查看>>
python 数据类型_字典和集合
查看>>
算法笔记_170:历届试题 分糖果(Java)
查看>>
一种并行随机梯度下降法
查看>>
文件方式实现完整的英文词频统计实例
查看>>
ListControl的用法
查看>>
单个SWF文件loading加载详解(转)
查看>>
Python3 指定文件夹下所有文件(包括子目录下的文件)拷贝到目标文件夹下
查看>>
SQLServer中的CTE通用表表达式
查看>>