码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis——多条件查询(三)

时间:2017-10-06 15:55:03      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:except   concat   lex   encoding   actor   student   映射文件   object   void   

beans包

package cn.alexander.beans;

/**
 * Created by Shinelon on 2017/10/6.
 */
public class Student {
    private Integer id;
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

dao包中

package cn.alexander.dao;

import cn.alexander.beans.Student;

import java.util.List;
import java.util.Map;

/**
 * Created by Shinelon on 2017/10/6.
 */
public interface IStudentDao {
    /**
     * 如果前台表单中给出的查询条件不能封装成一个对象的时候
     * 我们可以使用三种方式来解决这个我问题!
     * 01、把查询条件  封装成map
     *     使用map 查询姓名中带 小  并且年龄大于12的学生信息
     * 02、使用map 查询姓名中带小,年龄大于12,并且id大于小白id的学生信息
     *
     */
    List<Student> selectStudentByMap(Map<String,Object> map);
    /**
     * 第二种方式
     *     查询姓名中带 小  并且年龄大于12的学生信息
     */
    List<Student> selectStudentByCondition(String name,int age);
}

util包中

package cn.alexander.util;

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 java.io.IOException;
import java.io.InputStream;

/**
 * SqlSessionFactory的单例类
 */
public class SessionFactoryUtil {
    // 创建需要单例的对象实例
    private static SqlSessionFactory sessionFactory;

    // 私有化构造
    private SessionFactoryUtil(){}

    // 对外提供访问接口
    public static synchronized SqlSession getSession(){
        try {
            InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");
            // 判断SqlSessionFactory是否为空,如果为空则创建
            if(sessionFactory==null){
                sessionFactory = new SqlSessionFactoryBuilder().build(stream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sessionFactory.openSession();
    }


}

xml文件

<?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">
<!--映射文件的根节点
  namespace
-->
<mapper namespace="cn.alexander.dao.IStudentDao">
    <!--
        我们在前台表单中,有三个输入框,用户输入几个我们不知道 testMap1
    -->
    <!--<select id="selectStudentByMap" resultType="cn.alexander.beans.Student">
        select id,name,age from student_mybatis
        where name like concat(‘%‘,#{stuName},‘%‘)
        and age>#{stuAge}
    </select>-->
    <!--   testMap2   -->
    <select id="selectStudentByMap" resultType="cn.alexander.beans.Student">
        select id,name,age from student_mybatis
        where name like concat(‘%‘,#{stuName},‘%‘)
        and age>#{stuAge}
        and id>#{stu.id}
    </select>

    <!-- 按照参数的个数进行封装 -->
    <select id="selectStudentByCondition" resultType="cn.alexander.beans.Student">
        select id,name,age from student_mybatis
        where name like concat(‘%‘,#{0},‘%‘)
        and age>#{1}
    </select>

</mapper>

 

MyBatis——多条件查询(三)

标签:except   concat   lex   encoding   actor   student   映射文件   object   void   

原文地址:http://www.cnblogs.com/alexanderthegreat/p/7631590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!