mybatis注解開發(fā) 一對多嵌套查詢方式
mybatis一對多嵌套查詢
一個class中有許多student,屬于一對多關(guān)系。
本文利用mybatis注解,嵌套查詢方式來實現(xiàn)一對多查詢,在根據(jù)classId查詢class時,順便查詢并展示出class中的student
student實體類
package com.itheima.dao;
public class StudentDao {
private Integer id;
private String name;
private Integer age;
private Integer cid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
@Override
public String toString() {
return "StudentDao{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", cid=" + cid +
'}';
}
}
class實體類
package com.itheima.dao;
import java.util.List;
public class ClassDao {
private Integer id;
private String className;
private List<StudentDao> studentsList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "ClassDao{" +
"id=" + id +
", className='" + className + '\'' +
", studentsList=" + studentsList +
'}';
}
}
mybatis-config.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>
<!--設(shè)置連接數(shù)據(jù)庫的環(huán)境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper class="com.itheima.mapper.StudentMapper"/>
<mapper class="com.itheima.mapper.ClassMapper"/>
</mappers>
</configuration>
studentMapper.java
package com.itheima.mapper;
import com.itheima.dao.StudentDao;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
@Select("select * from student where cid = #{cid}")
@Results({@Result(id = true, property = "id", column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "age",column = "age"),
@Result(property = "cid", column = "cid")})
List<StudentDao> selectStudentByCid(Integer cid);
}
classMapper.java
package com.itheima.mapper;
import com.itheima.dao.ClassDao;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface ClassMapper {
@Select("select * from class where id = #{id}")
@Results({@Result(id =true, property = "id",column = "id"),
@Result(property = "className",column = "className"),
@Result(property ="studentsList",column = "id",
many = @Many(select = "com.itheima.mapper.StudentMapper.selectStudentByCid"))})
ClassDao selectClassById(Integer id);
}
testMybatis.java
import com.itheima.dao.ClassDao;
import com.itheima.dao.StudentDao;
import com.itheima.mapper.ClassMapper;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.annotations.Mapper;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class testMybatis {
@Test
public void test() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
ClassMapper classMapper = sqlSession.getMapper(ClassMapper.class);
ClassDao res = classMapper.selectClassById(2);
System.out.println(res.toString());
}
}
數(shù)據(jù)庫sql文件
INSERT INTO `class` (`id`, `className`) VALUES (1, '軟件19-1');
INSERT INTO `class` (`id`, `className`) VALUES (2, '軟件19-2');
INSERT INTO `student` (`name`, `age`, `cid`, `id`) VALUES ('羅輯', 18, 2, 1);
INSERT INTO `student` (`name`, `age`, `cid`, `id`) VALUES ('張三', 18, 2, 2);
INSERT INTO `student` (`name`, `age`, `cid`, `id`) VALUES ('李四', 18, 2, 3);
查詢結(jié)果可看出,2班的班級信息和2班的學(xué)生信息

注意事項:
1、一對多關(guān)系中,‘’多‘’方實體類存‘’一‘’的那方的主鍵作為外鍵,‘’一‘’的那方的實體類存‘’多‘’的那方的 list<多的那方的實體類屬性>以便查詢時展示出來。
在“一”的那方的mapper接口中定義嵌套查詢。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復(fù)提交校驗
本文主要介紹了SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復(fù)提交校驗,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
Java面試題沖刺第十九天--數(shù)據(jù)庫(4)
這篇文章主要為大家分享了最有價值的三道關(guān)于數(shù)據(jù)庫的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-08-08
java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)
如果我們想要生成一個隨機數(shù),通常會使用Random類。但是在并發(fā)情況下Random生成隨機數(shù)的性能并不是很理想,本文主要介紹了java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù),感興趣的可以了解一下2022-05-05
Mybatis之a(chǎn)ssociation和collection用法
這篇文章主要介紹了Mybatis之a(chǎn)ssociation和collection用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

