第二章 查询

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Mythology_px/article/details/82861362

         Mybatis重在sql语句映射,支持多元化sql语句查询,

如按条件,属性,列等查询,聚合函数查询,模糊查询,分页查询等

 

接口:IstudentMapper

package com.mapper;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.model.Student;

/*
 *	数据库方法接口----------->sql操作的映射文件
 */
public interface IStudentMapper {
	// 按属性查询:性别
	public List<Student> findBySex(String sex);
	// 按指定列查询:姓名,生日
	public List<Student> findPart();
	
	// 查询总数
	public int total();
	// 查询最大出生年月
	public Date maxAge();
	// 查询最小出生年月
	public Date minAge();
	
	// 查询名字中带有o的人
	public List<Student> like(String like);
	
	// 分页查询:逻辑分页---数据库全部查询,后通过RowBounds实现逻辑上的分页
	public List<Student> findByPage1(RowBounds rb);

	// 分页查询:物理查询--数据库查询的时候以传入的数据(begin ,size)为参数,分页查询,返回部分结果集
	public List<Student> findByPage2(Map<String,Object> map);
	/*
	 * 分页查询,可以指定一个参数,或多个参数
	 * 一个参数,可以封装成对象或Map,使用#{属性}或#{key}获取
	 * 两个参数,使用#{下标}获取,从0开始
	 */
}

接口配置文件:StudentMapper.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">
  

<mapper namespace="com.mapper.IStudentMapper">
<!-- 接口对应的sql映射文件 :配置完接口映射文件之后需在mybatis配置文件进行注册-->

	<!-- 执行查询,将结果集数据存入集合中,需自定义返回结果集 -->
	<resultMap type="Student" id="StudentList">
		<id property="stuno" column="stuno" />
		<result property="sname" column="sname" />
		<result property="sex" column="sex" />
		<result property="bir" column="bir" />
		<result property="phone" column="phone" />
	</resultMap>
	
	<!-- 在各种标签中的id属性必须和接口中的方法名相同 , id属性值必须是唯一的,不能够重复使用 -->
	<!-- parameterType属性指明查询时使用的参数类型 -->
	<!-- resultType属性指明查询返回的结果值类型 -->
	<!--#{}中的内容,为占位符,当参数为某个JavaBean时,表示放置该Bean对象的属性值  -->

	<!-- 增,删,改返回是影响的行数,不需要指定返回类型resultType-->
	<!-- 根据性别查询 -->
	<select id="findBySex" parameterType="java.lang.String" resultMap="StudentList">
		select * from stu where sex=#{sex}
	</select>
	<!-- 查询姓名,生日部分字段 -->
	<select id="findPart" resultMap="StudentList">
		select sname,bir from stu
	</select>
	<!-- 查询总记录数 -->
	<select id="total" resultType="java.lang.Integer">
		select count(*) from stu
	</select>
	<!-- 查询最大年龄 -->
	<select id="maxAge" resultType="java.sql.Date">
		select min(bir) from stu
	</select>
	<!-- 查询最小年龄 -->
	<select id="minAge" resultType="java.sql.Date">
		select max(bir) from stu
	</select>
	<!-- 查询名字中带有o的人 -->
	<select id="like" parameterType="java.lang.String" resultMap="StudentList">
		select * from stu where sname like "%"#{like}"%"
	</select>
	<!-- 分页查询:逻辑查询 -->
	<select id="findByPage1" resultMap="StudentList">
		select * from stu
	</select>
	<!-- 分页查询:物理查询 -->
	<select id="findByPage2" parameterType="java.util.Map" resultMap="StudentList">
		select * from stu limit #{begin},#{size}
	</select>
</mapper>

测试

package com.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.mapper.IStudentMapper;
import com.model.Student;
import com.util.DBTools;
/*
 * 	测试
 * */
public class Test {
	public static void main(String[] args) throws IOException {
		// 创建数据库对象
		DBTools db = new DBTools();
		// 获取接口
		IStudentMapper ism =(IStudentMapper)db.getInterfaceSqlSession(IStudentMapper.class);
		
		//	查询性别是man的所有信息
		List<Student> list = ism.findBySex("man");
		for (Student student : list) {
			System.out.println(student);
		}
		list.clear();
		System.out.println("\n\n");
		
		//	查询所有人员的姓名,生日
		list = ism.findPart();
		for (Student student : list) {
			System.out.println(student);
		}
		list.clear();
		System.out.println("\n\n");
		
		// 聚合函数
		System.out.println("人数总数:"+ism.total());
		System.out.println("最大出生年月:"+ism.maxAge());
		System.out.println("最小出生年月:"+ism.minAge());
		System.out.println("\n\n");
		
		// 模糊查询:查询名字中带有o的人
		list = ism.like("o");
		for (Student student : list) {
			System.out.println(student);
		}
		list.clear();
		System.out.println("\n\n");
		
		// 分页查询:逻辑分页
		list = ism.findByPage1(new RowBounds(1, 3));
		for (Student student : list) {
			System.out.println(student);
		}
		list.clear();
		System.out.println("\n\n");
		
		// 分页查询:物理分页
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("begin", 2);
		map.put("size", 2);
		list = ism.findByPage2(map);
		for (Student student : list) {
			System.out.println(student);
		}
		list.clear();
		System.out.println("\n\n");	
		
		
		// 提交事务和关闭
		db.commit();
		db.close();		
	}
}

猜你喜欢

转载自blog.csdn.net/Mythology_px/article/details/82861362