黄山网站建设策划,wordpress字体插件下载,自己建网站数据怎么做,集团官方网站建设简介 上节课内容中讲解了查询全部, 不需要带条件查, 这节我们讲讲 带条件查询 目标 1. 带一个条件查询-基本数据类型 2.带两个条件查询-连个基本数据类型 3.带一个对象类型查询 为了实现目标, 我们要实现 按照主键 查询某个学生信息, 按照姓名和年级编号查询学生信息 按照学生…简介 上节课内容中讲解了查询全部, 不需要带条件查, 这节我们讲讲 带条件查询 目标 1. 带一个条件查询-基本数据类型 2.带两个条件查询-连个基本数据类型 3.带一个对象类型查询 为了实现目标, 我们要实现 按照主键 查询某个学生信息, 按照姓名和年级编号查询学生信息 按照学生部分信息查询完整信息 实现步骤
按照主键查询学生信息 主键是唯一的,如果按照主键查, 最多查询到一位同学信息 修改 StudentMapper.java 增加以下代码 /*** 按照主键查询学生信息* param id 学生编号* return 学生信息*/Student findById(int id); 修改StudentMapper.xml增加 select !--按照主键查询--select idfindById parameterTypeint resultTypestudentselect * from student where sid#{id}/select 一个参数查询的说明: select 标签的id 属性值 对应 接口中 方法的名字 parameterType 对应 接口中 参数的类型 resultType 对应 接口中的返回值类型 一个参数的查询, 在mybatis中 可以使用 #{任意字符} 来获取, 但 为了与接口一致, 一般使用接口中参数的名字来代替 即 #{参数名} 测试 代码 import entity.Student;
import mapper.StudentMapper;
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.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class TestStudent {private SqlSessionFactory sqlSessionFactory;Beforepublic void init() throws IOException {// mybatis 配置文件的文件名String resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);}Testpublic void testFindAll() throws IOException {//获得SqlSessionSqlSession session sqlSessionFactory.openSession();StudentMapper mapper session.getMapper(StudentMapper.class);ListStudent list mapper.findAll();list.forEach((e)-System.out.println(e));}Testpublic void testFindById(){//获得SqlSessionSqlSession session sqlSessionFactory.openSession();StudentMapper mapper session.getMapper(StudentMapper.class);Student student mapper.findById(2);System.out.println(student);}
}运行结果 按照姓名和年级编号查询学生信息 按照姓名和年级编号查, 可以查询多个学生信息, (一个年级中可以出现同名学生) 修改StudentMapper.java 增加 /*** 按照 姓名及年级编号查询学生信息* param name 姓名* param gid 年级编号* return 学生信息*/ListStudent findBy(String name,int gid); 修改StudentMapper.xml 增加 select 标签 !--按照姓名及年级编号查询--select idfindBy resultTypestudentselect * from student where gid#{param2} and sname#{param1}/select 编写测试类 Testpublic void testFindBy() throws IOException {//获得SqlSessionSqlSession session sqlSessionFactory.openSession();StudentMapper mapper session.getMapper(StudentMapper.class);ListStudent list mapper.findBy(王凯,7);list.forEach((e)-System.out.println(e));} 结果如下 使用 arg0 测试 两个参数总结: 对应 select标签id的值 对应 接口中 方法名 不需要配置 paramterType 通过 #{arg0}或#{param1} 引用获得第一个参数 通过 #{arg1}或#{param2} 引用获得第二个参数, 以此类推 按照多条件查询学生信息 多条件查询 一般推荐 参数使用 实体类本身 修改StudentMapper.java 增加 /*** 多条件查询* param student 多条件* return*/ListStudent find(Student student); 修改StudentMapper.xml 增加 !--按照多条件查询--select idfind resultTypestudent parameterTypestudentselect * from student where gid#{gid} and sname#{sname} and phone#{phone}/select Testpublic void testFind() throws IOException {//获得SqlSessionSqlSession session sqlSessionFactory.openSession();StudentMapper mapper session.getMapper(StudentMapper.class);Student stu new Student();stu.setPhone(13852615963);stu.setSname(王凯);stu.setGid(7);ListStudent list mapper.find(stu);list.forEach((e)-System.out.println(e));} 总结: 使用对象类型作为参数 #{属性名} , 要注意 属性名区分大小写 本文只是为了 讲解 接口中方法参数的写法, 例如像 电话,姓名应该用模糊查询写法, 本文目前先不涉及,后续更新模糊查询