Flutter开发App的未来及其在各行业的应用潜力分析
1038
2023-01-07
Mybatis中多个对象包含同一个对象的处理操作
多个对象对应一个对象时,应该如何进行查询?
例如
关键字:association : 联系 ,关联 多个人可以关联一个人。
首先做一些准备,如:实体类,工具类和Mybatis核心文件
实体类:
//老师实体类
package com.MLXH.pojo;
public class Teacher {
private int id;
private String name;
public Teacher() {
}
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public voidTpTNpasCtd setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
//学生实体类
package com.MLXH.pojo;
public class Student {
private int id;
private String name;
private Teacher teacher;
public Student() {
}
public Student(int id, String name, Teacher teacher) {
this.id = id;
this.name = name;
this.teacher = teacher;
}
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 Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + teacher +
'}';
}
}
database.properties配置文件数据库需要的数据
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
username = root
password = 123456
Mybatis工具类:mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
工具类:主要是为了使获得sqlsession更为简单方便
package com.MLXH.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
iTpTNpasCtdmport java.io.IOException;
import java.io.InputStream;
//mybatis的工具类,重复的代码的提纯
public class MyBatisUtils {
//类变量不需要设置默认值;
private static SqlSessionFactory sqlSessionFactory;
static {
//在maven中,所有的资源文件一般都放在resources目录下,我们可以直接拿到。
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//设置SqlSessionFactory公共的方法
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
//获得一个带事务自动提交功能的SqlSession公共的方法
public static SqlSession getSqlSession(){
//自动提交事务
return sqlSessionFactory.openSession(true);
}
}
StudentDao接口
package com.MLXH.dao;
import com.MLXH.pojo.Student;
import java.util.List;
public interface StudentDao {
//获得全部学生的信息以及对应的老师
List
//获得全部学生的信息以及对应的老师
List
}
针对于StudentDao接口的实现mapper文件:我们使用如下的方式来进行查询
1.模拟数据库思想:连表查询
StudentMapper.xml
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from mybatis.student
select * from mybatis.teacher where id = #{id}
测试类
@Test
public void getStudents(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
List
for (Student student : students) {
System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName());
}
}
2.模拟面向对象的思想
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select s.id,s.name,t.id as tid,t.name as tname
from mybatis.student as s, mybatis.teacher as t
where s.tid = t.id
测试
@Test
public void getStudentsTwo(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
List
for (Student student : students) {
System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName());
}
}
总结:
mybatis中遇到多对一的情况,要使用关联映射处理:使用association
两种处理思路:
数据库思想 : 联表查询
OOP思想 :关联对象
Mybatis同时传入多个对象及普通参数
当传入多个文件时,mapper接口文件的方法参数要使用@param(“xx”)注释。
例子:
mapper:
//Student是对象,age是String类型。
int getPojo(@param("student") Student student, @param("age") String age );
xml:
select
from student
where 1 = 1
and id = #{student.id}
and age = #{age}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~