mybatis动态sql
来源:互联网 发布:js gzip 压缩工具 编辑:程序博客网 时间:2024/06/14 22:52
mybatis的动态sql:
if标签的使用:
package com.cbh.dao;import java.util.List;import com.cbh.beans.Student;public interface IStudent {List<Student>selectStudentsByCondition(String name,int age,int score);List<Student>selectStudentsByConditionif(Student student);List<Student>selectStudentsByConditionwhere(Student student);}mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.cbh.dao.IStudent"><select id="selectStudentsByConditionif" resultType="Student">select id,name,age,score from student1 where 1=1<if test="name !=null and name !=''">and name like '%' #{name} '%'</if><if test="age>0">and age> #{age}</if></select> <select id="selectStudentsByConditionwhere" resultType="Student">select id,name,age,score from student1 <where><if test="name !=null and name !=''">name like '%' #{name} '%'</if><if test="age>0">and age> #{age}</if><if test="score>0">and score>#{score}</if></where></select> <!-- #{}中可以放入的内容1.参数对象的属性2.随意的内容,此时#{}是个占位符3.参数为map时的key4.参数为map时,若key所对应的value为对象,则可以将该对象的属性放入5.参数的索引号(本次就是参数的索引号) --> </mapper>测试类:
package com.cbh.test;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;import org.junit.Before;import org.junit.Test;import com.cbh.Utils.MybatisUtils;import com.cbh.beans.Student;import com.cbh.dao.IStudent;public class mytest {private IStudent dao;@Beforepublic void before() {SqlSession sqlSession=MybatisUtils.getSqlSession();dao =sqlSession.getMapper(IStudent.class) ;}@Afterpublic void after(){if(sqlsession!=null){sqlsession.close();}}@Testpublic void test8(){//Student stu=new Student("大",23,0);Student stu=new Student("",0,0);List<Student> student=dao.selectStudentsByConditionif(stu );for (Student student2 : student) {System.out.println(student2);}}@Testpublic void test9(){Student stu=new Student("",0,100);List<Student> student=dao.selectStudentsByConditionwhere(stu);for (Student student2 : student) {System.out.println(student2);}} }0 1
- mybatis动态SQL语句
- MyBatis动态SQL
- MyBatis 动态SQL
- Mybatis 动态SQL
- MyBatis动态SQL
- MyBatis动态SQL完整版
- mybatis动态sql
- mybatis动态SQL语句
- mybatis动态SQL语句
- MyBatis的动态SQL
- MyBatis动态SQL
- MyBatis动态SQL
- MyBatis 动态SQL语句
- MyBatis动态SQL
- Mybatis组建动态SQL
- MyBatis 动态sql
- mybatis 动态SQL语句
- MyBatis动态SQL详解
- Java总结篇系列:Java多线程(一)
- 文本处理初学笔记01-主题模型
- python入门汇总
- 第9天:正则表达式
- python 实现周志华 机器学习书中 k-means 算法
- mybatis动态sql
- 浅谈Vue的iView
- Java总结篇系列:Java多线程(二)
- 08-图7 公路村村通 (30分)
- 随笔 4. 这是一封感谢信
- static的常见用法
- 通过这 9 本开源书,学好 C++
- 值班登记的级别和首要污染物反了
- Oracle 11gR2 RAC修改SCAN IP