sqlxml

<?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="PersonCondition">
  <!--<where>可以自动去掉后面的第一个and-->
<select id="selectPersonByCondition" parameterType="person" resultMap="BaseResultMap">
select * from person t
<where>
      <if test="name != null">
        name like '%${name}%'
      </if>
      <if test="gender != null">
        and gender = #{gender}
      </if>
      <if test="birthday !=null">
        <![CDATA[
        and birthday > #{birthday}
        ]]>
      </if>
     </where>
</select>
  <!--set标签-->
  <update id="dynamicUpdate" parameterType="person">
    update person t
    <set>
      t.person_id = #{personId},
      <if test="name != null">
        name =#{name},
      </if>
      <if test="gender != null">
         gender = #{gender},
      </if>
      <if test="birthday !=null">
         birthday = #{birthday}
      </if>
    </set>
  </update>
  <!--foreach标签
    map.put("ids",Integer[])
    foreach 遍历集合来组装sql
    collection: map中集合的key
    open:以某种字符开始
    close: 以某种字符结束
    item:集合中的元素
    separator: 以某种字符分隔
    index:当前遍历的索引号
  -->
  <select id="selectPersonByIn" parmameterType="map" resultMap="BaseResultMap">
    select * from person where t.person_id in
    <foreach collection="ids" open="(" close=")" item="pId" separator="," index="indx">
      #{pId}
    </foreach>
  </select>
  <!--mysql中可以实现批量插入输入,insert into person() values (1..),(2..)-->
  <insert id="insertBatch" parameterType="map">
    insert into person (name,gender,addr,birthday)
    values
    <foreach collection="pList" separator="," item="person">
      (#{person.name},#{person.gender},#{person.addr},#{person.birthday})
    </foreach>
  </insert>
<!-- 实现多条件查询,姓名模糊匹配,年龄在最大最小值之间 -->
<select id="getPerson" parameterType="com.stone.bean.ConditionPerson"
resultType="com.stone.bean.Person">
select * from person where
<if test='name !="%null%"'>
name like #{name} and
</if>
age
between
#{minAge} and #{maxAge}
</select> </mapper>

condition java bean

package com.stone.bean;

public class ConditionPerson {
private String name;
private int minAge;
private int maxAge;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMinAge() {
return minAge;
}
public void setMinAge(int minAge) {
this.minAge = minAge;
}
public int getMaxAge() {
return maxAge;
}
public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
}
public ConditionPerson(String name, int minAge, int maxAge) {
super();
this.name = name;
this.minAge = minAge;
this.maxAge = maxAge;
}
public ConditionPerson() {
super();
}
@Override
public String toString() {
return "ConditionPerson [name=" + name + ", minAge=" + minAge
+ ", maxAge=" + maxAge + "]";
} }

java bean

package com.stone.bean;

import java.text.SimpleDateFormat;
import java.util.Date; public class Person { private int id;
private String name;
private Date birthday;
private int age; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} @Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:SS");
return "Person [id=" + id + ", name=" + name + ", birthday="
+ dateFormat.format(birthday) + "]";
} }

test

package com.stone.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.stone.bean.ConditionPerson;
import com.stone.bean.Person;
import com.stone.db.DBAccess; public class DBDaoPerson { public static void main(String[] args) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
String statement = "PersonCondition.getPerson";
ConditionPerson parameter = new ConditionPerson("%a%", 11, 18);
// 通过sqlSession执行SQL语句;
List<Person> list = sqlSession.selectList(statement, parameter);
System.out.println(list);
System.out.println("======================="); } catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
} }

MyBatis动态SQL与模糊查询的更多相关文章

  1. mybatis 动态Sql的模糊查询

    where teacher.tname like concat(concat(#{tName}),'%') 2:distinct的使用 下面先来看看例子: table    id name    1 ...

  2. mybatis的动态sql及模糊查询

    1.动态sql 使用类似于jstl表达式来实现 2.模糊查找 用一个对象来封装条件 步骤: 1)新建一个条件实体 package com.hy.mybatis.entity; public class ...

  3. 动态SQL与模糊查询

    一: 1.需求 实现多条件查询用户(姓名模糊查询,年龄在指定的最小值与最大值之间) 2.结构目录 3.准备数据与建表 CREATE TABLE d_user( id int PRIMARY KEY A ...

  4. 动态SQL之模糊查询

    模糊查询学习了三种: DAO层 // 可以使用 List<User> wherelike01(String user_name); // 忘记 List<User> where ...

  5. 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

    多参数查询,使用parameterType.实例: 用户User[id, name, age] 1.mysql建表并插入数据 2.Java实体类 public class User { public ...

  6. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  7. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  8. MyBatis动态SQL(认真看看, 以后写SQL就爽多了)

    目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...

  9. mybatis动态sql和分页

    mybatis动态sql foreach BookMapper.xml <select id="selectBooksIn" resultType="com.lin ...

随机推荐

  1. Chapter 1 First Sight——6

    "You didn't need to do that, Dad. I was going to buy myself a car." 你不需要这样,父亲,我会自己买一辆车的 &q ...

  2. nodejs连接mysql实例

    1.在工程目录下运行npm install mysql安装用于nodejs的mysql模块: 2.创建db.js模块用于连接mysql,同时定义query查询方法: var mysql = requi ...

  3. Mysql基于GTID主从复制

    Mysql5.6基于GTID全局事务的复制 什么是GTID?   GTID(Global Transaction Identifiers)是全局事务标识 当使用GTIDS时,在主上提交的每一个事务都会 ...

  4. [Big Data]Spark

    SPARK Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室)所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapRed ...

  5. Android 4.0 ICS SystemUI浅析——StatusBar结构分析

    Android 4.0 ICS SystemUI浅析——StatusBar结构分析 分类: Android2012-06-30 14:45 23687人阅读 评论(8) 收藏 举报 androidsi ...

  6. Python核心编程第二版(中文).pdf 目录整理

    python核心编程目录 Chapter1:欢迎来到python世界!-页码:7 1.1什么是python 1.2起源  :罗萨姆1989底创建python 1.3特点 1.3.1高级 1.3.2面向 ...

  7. Jenkins启动、停止脚本

    1.jenkins下载地址:http://pan.baidu.com/s/1o79ZRzs 2.创建shell脚本,如:jenkins.sh #!/bin/bash pid=`ps -ef | gre ...

  8. NYoj1058

    水题,dfs,裸的,本来这道题没什么好写的,只是第一次写的代码慢的出奇,纪念一下那个奇怪的思路 链接:http://acm.nyist.net/JudgeOnline/problem.php?pid= ...

  9. Tyvj P3276

    题目链接:http://www.tyvj.cn/p/3276 这题是一个动归题,一直没有想出动归的做法,后来求教别人之后写了一个记忆化搜索,只有出题者又给我提供了DP的解法,下面我来写写DP的写法 设 ...

  10. Spring学习---JPA配置和使用

      理论的东西如果不实践,永远不会变成自己的东西.本文将介绍用maven管理,用Hibernate作为JPA供应商,使用MYSQL数据库,配置和使用JPA.   以下代码已经上传至GITHUB.   ...