//演示多对一的项目
项目的结构图:
一、 1. 多对的一的说明 以多各学生 对一位老师为案例 2. 对学生这方面说 : 使用关键"关联"就是多个学生关联一个老师 -----------多对一 二、 多对一的案例 学生和老师 1. 创建数据库表输入记录
create table teacher( id int not null primary key , name varchar(50) not null )ENGINE =INNODB DEFAULT CHARSET=utf8; insert into teacher(id,name) values (1001,"刘老师"),(1002,"叶老师"); create table student( id int not null primary key, name varchar(50) not null, age int not null , tid int default null, constraint `fk` foreign key (`tid`) references `teacher`(`id`) )ENGINE =INNODB DEFAULT CHARSET=utf8 collate utf8_general_ci insert into student(id,name,age,tid) values (1201,"王冬雪",20,1001), (1202,"好平平",19,1001), (1203,"刘美玲",18,1001), (1204,"王海华",32,1001), (1205,"张李",34,1001), (1206,"江风",45,1001), (1207,"任丽",67,1001); insert into student(id,name,age,tid) values (1201,"王冬雪",20,1001), (1202,"好平平",19,1001), (1203,"刘美玲",18,1001), (1204,"王海华",32,1001), (1205,"张李",34,1001), (1206,"江风",45,1001), (1207,"任丽",67,1001); insert into student(id,name,age,tid) values (1301,"李小龙",20,1002), (1302,"王美玲",19,1002), (1303,"和小",18,1002), (1304,"王丹丹",32,1002), (1305,"厚天天",34,1002), (1306,"李大明",45,1002), (1307,"李铁",43,1002); insert into student(id,name,age,tid) values (1401,"王小妹",20,1003), (1402,"李大牛",19,1003), (1403,"杨杨",18,1003), (1404,"王二小",32,1003), (1405,"李芬丽",34,1003), (1406,"牛小同",45,1003), (1407,"和小明",23,1003);
三、 项目环境搭建 第一步: 导入lombok包 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> 第二步: 建立实体类 Teachar 、Student
package ljg.com.pojo; import lombok.*; @Data @AllArgsConstructor @NoArgsConstructor @Setter @Getter public class Student { private int id; private String name; private int age; // 学生关联老师 ,形成对对一情况 private Teacher teacher; }
package ljg.com.pojo; import lombok.*; @Data @AllArgsConstructor @NoArgsConstructor @Setter @Getter public class Teacher { private int id; private String name; }
第三步: 建立mapper接口
创建 StudentMapper 接口
package ljg.com.mapper; import ljg.com.pojo.Student; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface StudentMapper { @Select("select * from student where id =#{sid}") Student getStudent(@Param("sid") int id); }
创建 TeacherMapper 接口
package ljg.com.mapper; import ljg.com.pojo.Teacher; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface TeacherMapper { @Select("select * from teacher where id =#{tid}") Teacher getTeacher(@Param("tid") int id); }
第四步: 建立Mapper.xml文件
创建 TeacherMapper.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="ljg.com.mapper.TeacherMapper"> </mapper>
创建 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="ljg.com.mapper.StudentMapper"> </mapper>
第五步: 在核心配置文件 中的注册绑定我们的Mapper接口或者是文件
Mybatis-conf.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"/> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <typeAliases> <package name="ljg.com.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper class="ljg.com.mapper.TeacherMapper"/> <mapper class="ljg.com.mapper.StudentMapper"/> </mappers> </configuration>
第六步:创建 db.properties 文件来连接我们的数据库
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/jxgl?serverTimezone=UTC username=root password=123456
第七部:创建测试类
@Test public void getteachaer(){ SqlSession sqlSession = MybatisUntis.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student student = mapper.getStudent(1201); System.out.println(student); TeacherMapper mapper1 = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper1.getTeacher(1001); System.out.println(teacher); }
四、 多对一开发过程----方式一:按照查询嵌套处理 第一步:提出逻辑业务 1查询所有学生的信息,以及对应的教师信息 1. 在学生接口中实现这个业务
public interface StudentMapper { // 1查询所有学生的信息,以及对应的教师信息 List<Student> getStudetn(); }
第二步:在StudentMapper.xml文件中写对应的业务实现 ``` <!--- 思路: 1 查询所有的学生 2. 用查收到学生的tid 查找老师,必须要在这有个查老师的方法 3. 讲两个查询连接起来,连接方法使用resultMap建立复杂的映射关系 复杂的属性用特殊处理 如果是对象 用 <association property="teacher" 学生类的属性是Teacher的对象 column="tid" 学生表的外键是tid javaType="Teacher" 返回查询的结构是Teacher类型 select="getTeacher"/> 使用嵌套查询getTeacher 如果是集合 用 <collection property="uid" column="uid"/> --> ``` ```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="ljg.com.mapper.StudentMapper"> <!--- 思路: 1 查询所有的学生 2. 用查收到学生的tid 查找老师,必须要在这有个查老师的方法 3. 讲两个查询连接起来,连接方法使用resultMap建立复杂的映射关系 复杂的属性用特殊处理 如果是对象 用 <association property="teacher" 学生类的属性是Teacher的对象 column="tid" 学生表的外键是tid javaType="Teacher" 返回查询的结构是Teacher类型 select="getTeacher"/> 使用嵌套查询getTeacher 如果是集合 用 <collection property="uid" column="uid"/> --> <select id="getStudetns" resultMap="StudentTeacher"> select * from student </select> <resultMap id="StudentTeacher" type="Student"> <result column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from Teacher where id=#{id} </select> </mapper>
第三步:编写测试方法 ```java @Test public void getstudent(){ SqlSession sqlSession = MybatisUntis.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> studetns = mapper.getStudetns(); for (Student studetn : studetns) { System.out.println(studetn); } }
四、 多对一开发过程----方式二:按照结果嵌套处理 第一步:提出逻辑业务 1查询所有学生的信息,以及对应的教师信息 1 在学生接口中实现这个业务 ```java package ljg.com.mapper; import ljg.com.pojo.Student; import java.util.List; public interface StudentMapper { // 1查询所有学生的信息,以及对应的教师信息 // List<Student> getStudetns(); // 按照结果查询 List<Student> getStudetns2(); }
第二步:在StudentMapper.xml文件中写对应的业务实现 ``` <!--- 思路: 1 查询所有的学生 2. 用查收到学生的tid 查找老师,必须要在这有个查老师的方法 3. 讲两个查询连接起来,连接方法使用resultMap建立复杂的映射关系 复杂的属性用特殊处理 如果是对象 用 <association property="teacher" 学生类的属性是teacher 对象 javaType="Teacher"> 数据类型是Teacher <result property="name" column="tname" /> 教师的姓名是name 在查询中是tanme </association> 如果是集合 用 <collection property="uid" column="uid"/> -->
<?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="ljg.com.mapper.StudentMapper"> <select id="getStudetns2" resultMap="StudentTeacher2" > select s.id as sid,s.name as sname,age as sage,t.name as tname from student s,teacher t where s.tid=t.id </select> <resultMap id="StudentTeacher2" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="age" column="sage"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> </association> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from Teacher where id=#{id} </select> </mapper>
第三步:编写测试方法 ```java @Test public void getstudent(){ SqlSession sqlSession = MybatisUntis.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> studetns = mapper.getStudetns2(); for (Student studetn : studetns) { System.out.println(studetn); } }