Mybatis中多個(gè)對象包含同一個(gè)對象的處理操作
例如
關(guān)鍵字:association : 聯(lián)系 ,關(guān)聯(lián) 多個(gè)人可以關(guān)聯(lián)一個(gè)人。
首先做一些準(zhǔn)備,如:實(shí)體類,工具類和Mybatis核心文件
實(shí)體類:
//老師實(shí)體類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 void setName(String name) {this.name = name; } @Override public String toString() {return 'Teacher{' +'id=' + id +', name=’' + name + ’’’ +’}’; }}
//學(xué)生實(shí)體類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配置文件數(shù)據(jù)庫需要的數(shù)據(jù)
driver = com.mysql.jdbc.Driverurl = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8username = rootpassword = 123456
Mybatis工具類: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='database.properties'/> <!--Mybatis設(shè)置--> <settings><!--默認(rèn)日志實(shí)現(xiàn)--><!--<setting name='logImpl' value='STDOUT_LOGGING'/>--><!--Log4j實(shí)現(xiàn)--><setting name='logImpl' value='LOG4J'/> </settings> <!--配置別名--> <typeAliases><package name='com.MLXH.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><!--class對應(yīng)的是一個(gè)接口類--><!--resource對應(yīng)的是一個(gè)接口類的映射文件--><mapper resource='com/MLXH/dao/StudentMapper.xml'/> </mappers></configuration>
工具類:主要是為了使獲得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;import java.io.IOException;import java.io.InputStream;//mybatis的工具類,重復(fù)的代碼的提純public class MyBatisUtils { //類變量不需要設(shè)置默認(rèn)值; 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();} } //設(shè)置SqlSessionFactory公共的方法 public static SqlSessionFactory getSqlSessionFactory(){return sqlSessionFactory; } //獲得一個(gè)帶事務(wù)自動(dòng)提交功能的SqlSession公共的方法 public static SqlSession getSqlSession(){//自動(dòng)提交事務(wù)return sqlSessionFactory.openSession(true); }}
StudentDao接口
package com.MLXH.dao;import com.MLXH.pojo.Student;import java.util.List;public interface StudentDao { //獲得全部學(xué)生的信息以及對應(yīng)的老師 List<Student> getStudents(); //獲得全部學(xué)生的信息以及對應(yīng)的老師 List<Student> getStudentsTwo();}
針對于StudentDao接口的實(shí)現(xiàn)mapper文件:我們使用如下的方式來進(jìn)行查詢
1.模擬數(shù)據(jù)庫思想:連表查詢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'><!--namespace不能寫別名--><mapper namespace='com.MLXH.dao.StudentDao'> <!--遇到問題:學(xué)生類中關(guān)聯(lián)老師: 多個(gè)學(xué)生對應(yīng)一個(gè)老師 --><!--解決問題方式一:按查詢結(jié)果嵌套處理,模擬數(shù)據(jù)庫思想;--> <select resultMap='StudentTeacher'>select * from mybatis.student </select> <resultMap type='Student'><id column='id' property='id'/><result column='name' property='name'/><!--屬性和字段對應(yīng) , 類和表對應(yīng) , 對象和記錄關(guān)聯(lián)一個(gè)字段需求:拿到老師這個(gè)類的屬性association : 關(guān)聯(lián),多對一 column : 數(shù)據(jù)庫對應(yīng)的列名 property : 對應(yīng)屬性名 javaType : 多對一字段對應(yīng)的Java類型 select : 關(guān)聯(lián)一個(gè)語句--><association column='tid' property='teacher' javaType='Teacher' select='getTeacher'/> </resultMap> <select resultType='Teacher'>select * from mybatis.teacher where id = #{id} </select></mapper>
測試類
@Test public void getStudents(){SqlSession sqlSession = MyBatisUtils.getSqlSession();StudentDao mapper = sqlSession.getMapper(StudentDao.class);List<Student> students = mapper.getStudents();for (Student student : students) { System.out.println('學(xué)生姓名:'+student.getName()+'t老師姓名:'+student.getTeacher().getName());} }2.模擬面向?qū)ο蟮乃枷?p><?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapperPUBLIC '-//mybatis.org//DTD Mapper 3.0//EN''http://mybatis.org/dtd/mybatis-3-mapper.dtd'><!--namespace不能寫別名!!!!!--><mapper namespace='com.MLXH.dao.StudentDao'> <!-- 解決方式二:一個(gè)resultMap解決 , 模擬面向?qū)ο蟮乃枷?-> <select resultMap='StudentTeacher2'>select s.id,s.name,t.id as tid,t.name as tnamefrom mybatis.student as s, mybatis.teacher as twhere s.tid = t.id </select> <!--設(shè)置結(jié)果集映射ResultMap --> <resultMap type='Student'><id property='id' column='id'/><result property='name' column='name'/><!--直接關(guān)聯(lián)一個(gè)老師--><association property='teacher' javaType='Teacher'> <id property='id' column='tid'/> <result property='name' column='tname'/></association> </resultMap></mapper>
測試
@Testpublic void getStudentsTwo(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); List<Student> students = mapper.getStudentsTwo(); for (Student student : students) {System.out.println('學(xué)生姓名:'+student.getName()+'t老師姓名:'+student.getTeacher().getName()); }}
mybatis中遇到多對一的情況,要使用關(guān)聯(lián)映射處理:使用association
兩種處理思路:
數(shù)據(jù)庫思想 : 聯(lián)表查詢 OOP思想 :關(guān)聯(lián)對象Mybatis同時(shí)傳入多個(gè)對象及普通參數(shù)當(dāng)傳入多個(gè)文件時(shí),mapper接口文件的方法參數(shù)要使用@param(“xx”)注釋。
例子:mapper:
//Student是對象,age是String類型。int getPojo(@param('student') Student student, @param('age') String age );
xml:
<select resultMap='BaseResultMap'> select <include refid='Base_Column_List' /> from student where 1 = 1 <!-- 使用參數(shù)一(是個(gè)自己的對象) --> <if test='student.id != null'>and id = #{student.id} </if> <!-- 使用參數(shù)二 String類型 --> <if test='age!= null'>and age = #{age} </if></select>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 關(guān)于Oracle數(shù)據(jù)庫熱備份腳本深入剖析2. Sql server數(shù)據(jù)庫開發(fā)常用匯總3. ORACLE中如何實(shí)現(xiàn)ASCII字符串和16進(jìn)制串互相轉(zhuǎn)換4. Access數(shù)據(jù)庫安全的幾個(gè)問題5. Oracle的PDB數(shù)據(jù)庫創(chuàng)建DIRECTORY時(shí)遇到ORA-65254問題及解決方法6. MySQL雙主(主主)架構(gòu)配置方案7. MySQL 的啟動(dòng)選項(xiàng)和系統(tǒng)變量實(shí)例詳解8. mysql-bin.000001文件的來源及處理方法9. MySQL存儲(chǔ)過程例子(包含事務(wù)、參數(shù)、嵌套調(diào)用、游標(biāo)循環(huán)等)10. 如何遠(yuǎn)程調(diào)用ACCESS數(shù)據(jù)庫
