mybatis多对一处理两种处理方式
生活随笔
收集整理的這篇文章主要介紹了
mybatis多对一处理两种处理方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.Teacher.java
public class Teacher {private int id;private String name;//省略getter、setter及toString方法 }2.Student.java
public class Student {private int id;private String name;private int age;private int tid;//多個學生對應一個老師private Teacher teacher;3.StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.manytoone.entity.StudentMapper"><!-- 多對一的處理1.按查詢結果嵌套處理2.按查詢嵌套--><!-- 1.按查詢結果嵌套處理 --><select id="selectStudent" resultMap="studentTeacher">select s.id,s.name,s.age,s.tid,t.id tids,t.name tname from student s,teacher t where s.tid=t.id</select><resultMap type="Student" id="studentTeacher"><id column="id" property="id"/><result column="name" property="name"/><result column="age" property="age"/><result column="tid" property="tid"/><!-- 關聯對象property在實體類student中 的屬性--><association property="teacher" javaType="Teacher"><id column="tids" property="id"/><result column="tname" property="name"/></association></resultMap><!-- 2.按查詢結果處理 --><select id="getStudents" resultMap="studentMapper">select * from student</select><resultMap type="Student" id="studentMapper"><association property="teacher" column="tid" javaType="Teacher"select="selectTeacher"></association></resultMap><select id="selectTeacher" resultType="com.manytoone.entity.Teacher">select * from teacher where id=#{id}</select> </mapper>mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><!-- 加載數據庫配置文件--><properties resource="sql.properties"></properties><typeAliases><!-- <typeAlias type="com.mybatis.entity.Pet" alias="Pet"/> --><!-- 默認別名是對應的類名 --><package name="com.mybatis.entity"/><package name="com.manytoone.entity"></package></typeAliases><!--environments 指mybatis可以配置多個環境默認,default只想默認的環境每個sqlSessionFactory對應一個environment--><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- UNPOOLED訪問數據庫時每次都會打開關閉連接POOLED數據庫池減少了每次初始化、創建連接、和請求驗證的時間 --><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><!-- 定義SQL語句的映射文件 --><mapper resource="com/mybatis/entity/PetMapper.xml"/><mapper resource="com/manytoone/entity/StudentMapper.xml"/><mapper resource="com/manytoone/entity/TeacherMapper.xml"/><mapper class="com.annotation.dao.PetDao"/></mappers> </configuration>StudentDao.java
package com.manytoone.dao;import java.io.IOException; import java.util.List;import org.apache.ibatis.session.SqlSession;import com.manytoone.entity.Student; import com.mybatis.util.MyBatisUtil;public class StudentDao {public List<Student> selectAll() throws IOException{SqlSession sqlSession=MyBatisUtil.getSqlSession();List<Student> students=sqlSession.selectList("com.manytoone.entity.StudentMapper.selectStudent");return students;}public List<Student> getAll() throws IOException{SqlSession sqlSession=MyBatisUtil.getSqlSession();List<Student> students=sqlSession.selectList("com.manytoone.entity.StudentMapper.getStudents");return students;} }Test.java
package com.manytoone.test;import java.io.IOException; import java.util.List;import com.manytoone.dao.StudentDao; import com.manytoone.entity.Student;public class Test {public List<Student> selectAll() throws IOException{StudentDao studentDao=new StudentDao();List<Student> students=studentDao.selectAll();return students;}public List<Student> getAll() throws IOException{StudentDao studentDao=new StudentDao();List<Student> students=studentDao.selectAll();return students;}public static void main(String[] args) throws IOException {Test test=new Test();List<Student> students=test.getAll();for(Student stu:students) {System.out.println(stu);}} }?
總結
以上是生活随笔為你收集整理的mybatis多对一处理两种处理方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis分页查询
- 下一篇: IntelliJ IDEA快捷键学习