MyBatis查询对象信息
1、使用动态SQL语句查询
例如想要根据学生的姓名或年龄查询:
1、在mapper文件中写入:
<select id="queryStudentByNOrAWithSQLTag" parameterType="student" resultType="student"> select stuno,stuname,stuage from student <where> <!-- if test="student有stuname属性且不为null" --> <if test="stuName !=null and stuName!='' "> and stuname='${stuName}' </if> <if test="stuAge !=null and stuAge!=0 "> and stuage=${stuAge} </if> </where> </select>
2、在mapper接口声明方法
3、测试类中写入:
//查询单个学生,使用了SQL标签 public static void queryStudentByNOrAWithSQLTag() throws IOException{ //Connection - SqlSession //conf.xml -> reader Reader reader = Resources.getResourceAsReader("conf.xml"); //reader -> SqlSession //可以通过build的第二个参数指定数据库环境 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); StudentMapper studentMapper = session.getMapper(StudentMapper.class); Student stu = new Student(); stu.setStuAge(21); stu.setStuName("ls"); Student student = studentMapper.queryStudentByNOrAWithSQLTag(stu);//接口中的方法->SQL语句 System.out.println(student); session.close(); }
2、使用foreach迭代查询
1>迭代属性(Grade类: List<Integer> ids)
1、在mapper文件中写入:
<!--将多个元素放入List集合中-->
<select id="queryStudentWithNosInGrade" parameterType="grade" resultType="student"> select * from student <where> <if test="stuNos!=null and stuNos.size>0"> <foreach collection="stuNos" open=" and stuno in (" close=")" item="stuNo" separator=","> #{stuNo} </foreach> </if> </where> </select>
2、在mapper接口声明方法
3、测试类中写入:
//查询全部学生,使用grade public static void queryStudentWithNosInGrade() throws IOException{ //Connection - SqlSession //conf.xml -> reader Reader reader = Resources.getResourceAsReader("conf.xml"); //reader -> SqlSession //可以通过build的第二个参数指定数据库环境 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); // List<Student> students = session.selectList(statement); StudentMapper studentMapper = session.getMapper(StudentMapper.class); Grade grade = new Grade(); List<Integer> stuNos = new ArrayList<>(); stuNos.add(1); stuNos.add(2); stuNos.add(13); grade.setStuNos(stuNos); List<Student> students = studentMapper.queryStudentWithNosInGrade(grade); System.out.println(students); session.close(); }
2>使用简单类型的数组
注意:无论编写代码时,传递的是什么参数名(stuNos),在mapper.xml中 必须用array代替该数组
<!-- 将多个元素放入数组中 --> <select id="queryStudentsWithArray" parameterType="int[]" resultType="student"> select * from student <where> <if test="array!=null and array.length"><!-- 必须用array代替数组 --> <foreach collection="array" open=" and stuno in (" close=")" item="stuNo" separator=","> #{stuNo} </foreach> </if> </where> </select>
3>使用集合
注意:无论编写代码时,传递的是什么参数名(stuNos),在mapper.xml中 必须用list代替该数组
4>使用对象数组
Student[] students = {student0,student1,student2} 每个studentx包含一个学号属性
注意:
parameterType="Object[]" <foreach collection="array" open=" and stuno in (" close=")" item="student" separator=","> #{student.stuNo} </foreach>
3、SQL片段
<sql id="自己取的名字"> 想要提取的片段 </sql>
在想要使用的地方加入
<include refid="SQL名字"></include>
4、关联查询
1>一对一
a.业务扩展类
<select id="queryStudentByNoWithOO" parameterType="int" resultType="StudentBusiness"><!-- 因为是两个表查询,所以新建一个类其中包含这两个表所对应类的信息 --> select s.*,c.* from student s inner join studentcard c on s.cardid = c.cardid where s.stuno = #{stuNo} </select>
//继承一个类,另一个类中的代码写在此类中,即本类包含两个类 public class StudentBusiness extends Student{//学生业务扩展类 private int cardId; private String cardInfo; public int getCardId() { return cardId; } public void setCardId(int cardId) { this.cardId = cardId; } public String getCardInfo() { return cardInfo; } public void setCardInfo(String cardInfo) { this.cardInfo = cardInfo; } public String toString(){ return super.toString()+","+this.cardId+","+this.cardInfo; } }
b>利用resultMap
1、通过属性成员 将两个类建立联系
2、
<!-- 使用resultMap实现一对一 --> <select id="queryStudentByNoWithOO2" parameterType="int" resultMap="student_card_map"> select s.*,c.* from student s inner join studentcard c on s.cardid = c.cardid where s.stuno = #{stuNo} </select> <resultMap type="student" id="student_card_map"><!-- 此时学生类包含两个类中的信息 --> <!-- 学生的信息 --> <id property="stuNo" column="stuNo"/> <result property="stuName" column="stuName"/> <result property="stuAge" column="stuAge"/> <!-- 一对一时,对象成员使用association映射 javaType指定该属性的类型--> <association property="card" javaType="StudentCard"> <id property="cardId" column="cardId"/> <result property="cardInfo" column="cardInfo"/> </association> </resultMap>
2>一对多
<!-- 一对多 --> <select id="queryClassAndStudents" parameterType="int" resultMap="class_student_map"> <!-- 查询g1班的班级信息和g1班的所以学生信息 --> select c.*,s.* from student s inner join studentclass c on c.classid = s.classid where s.classid=#{classId} </select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!