您好,登錄后才能下訂單哦!
這篇“Mybatis對(duì)sql表的一對(duì)多查詢問(wèn)題怎么解決”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Mybatis對(duì)sql表的一對(duì)多查詢問(wèn)題怎么解決”文章吧。
Mybatisd對(duì)sql表的一對(duì)多查詢
select * from projectrecord pr left join projects po on po.pid=pr.pid left join emp e on e.empno = pr.empno where pr.pid=1
<?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="com.msb.mapper.EmpMapper"> <!--resultMap是一個(gè)映射表,對(duì)應(yīng)的字段會(huì)對(duì)應(yīng)數(shù)據(jù)表的字段 --> <resultMap id="projectMap" type="projects"> <id column="pid" property="pid"></id> <result column="pname" property="pname"></result> <result column="money" property="money"></result> <collection property="projectrecordList" ofType="projectrecord"> <id property="pid" column="pid"></id> <id property="empno" column="empno"></id> <association property="emp" javaType="emp"> <id property="empno" column="empno"></id> <result property="ename" column="ename"></result> <result property="job" column="job"></result> <result property="sal" column="sal"></result> <result property="hiredate" column="hiredate"></result> <result property="mgr" column="mgr"></result> <result property="comm" column="comm"></result> <result property="deptno" column="deptno"></result> </association> </collection> </resultMap> <!--Projects findPiddle(int pid);--> <select id="findPiddle" resultMap="projectMap"> select *from emp e inner join projectrecord pr on pr.empno=e.empno inner join projects po on po.pid= pr.pid where pr.pid=#{pid} </select> </mapper>
//mapper表 package com.msb.mapper; import com.msb.pojo.Dept; import com.msb.pojo.Emp; import com.msb.pojo.Projects; import java.util.List; public interface EmpMapper { /** * 實(shí)現(xiàn)根據(jù)項(xiàng)目編號(hào)查看所有參與項(xiàng)目的人員信息 * @param pid 傳入項(xiàng)目號(hào) * @return 返回一個(gè)project的對(duì)象 */ Projects findPiddle(int pid); }
package com.msb.pojo; //一對(duì)一關(guān)系引入 import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor public class Projectrecord implements Serializable { private int empno; private int pid; //pr和e是一對(duì)一的關(guān)系 private Emp emp; }
package com.msb.pojo; //一對(duì)多關(guān)系實(shí)體類設(shè)計(jì) import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.List; @NoArgsConstructor @AllArgsConstructor @Data public class Projects implements Serializable { private int pid; private String pname; private int money; //po和pr是一對(duì)多的關(guān)系 List<Projectrecord> projectrecordList; }
package com.msb.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.sql.Date; @Data @AllArgsConstructor @NoArgsConstructor public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Integer sal; private Integer comm; private Integer deptno; private Dept dept; }
jdbc_driver=com.mysql.cj.jdbc.Driver jdbc_url=jdbc:mysql://127.0.0.1:3306/testtable?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai jdbc_username=root jdbc_password=root
log4j.rootLogger=debug,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=d:/msb.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n
<?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="jdbc.properties"></properties> <!--這里是起的別名,前面是名,后面是路徑--> <!--<typeAliases> <typeAlias alias="dept" type="com.msb.pojo.Dept"/> </typeAliases>--> <!--包別名,用到時(shí)候調(diào)用名字小寫即可,就會(huì)掃描msb下的所以實(shí)體類,用的時(shí)候?qū)嶓w類名小寫--> <typeAliases> <package name="com.msb"/> </typeAliases> <environments default="development"> <environment id="development"> <!-- 簡(jiǎn)單使用了 JDBC 的提交和回滾設(shè)置 --> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc_driver}"/> <property name="url" value="${jdbc_url}"/> <property name="username" value="${jdbc_username}"/> <property name="password" value="${jdbc_password}"/> </dataSource> </environment> </environments> <!--加載mapper映射文件--> <mappers> <package name="com.msb.mapper"/> </mappers> </configuration>
import com.msb.mapper.EmpMapper; import com.msb.pojo.Dept; import com.msb.pojo.Emp; import com.msb.pojo.Projectrecord; import com.msb.pojo.Projects; 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.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class Test1 { SqlSession sqlSession = null; EmpMapper mapper =null; @Before public void test1(){ //首先做一個(gè)對(duì)象SqlSessionFactoryBuilder建立一個(gè)繪話 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); //有一個(gè)文本輸入的io流進(jìn)行讀取操作 InputStream stream = null; try { //這里的路徑直接會(huì)定位到配置文件classes下面;所以這個(gè)文件在次目錄下--編譯和 //-圖紙;對(duì)數(shù)據(jù)庫(kù)文件進(jìn)行讀取,獲取一個(gè)io流,由于配置文件在classes下面,直接寫文件名即可 stream = Resources.getResourceAsStream("sqlMapConfig.xml"); } catch (IOException e) { e.printStackTrace(); } //build需要指向一個(gè)文件進(jìn)行讀取出來(lái)--工廠 SqlSessionFactory factory = ssfb.build(stream); //需要用sqlSession去調(diào)用增刪改查--工人去獲取數(shù)據(jù),打開這個(gè)繪話 sqlSession = factory.openSession(); } /*多對(duì)多的查詢*/ @Test public void test7(){ mapper = sqlSession.getMapper(EmpMapper.class); Projects piddle = mapper.findPiddle(1); System.out.println("Pid"); System.out.println(piddle.getPid()); System.out.println("Pname"); System.out.println(piddle.getPname()); System.out.println("Money"); System.out.println(piddle.getMoney()); System.out.println("ProjectrecordList"); List<Projectrecord> projectrecordList = piddle.getProjectrecordList(); projectrecordList.forEach(System.out::println); } @After public void test3(){ if (sqlSession!=null){ sqlSession.close(); } } }
以上就是關(guān)于“Mybatis對(duì)sql表的一對(duì)多查詢問(wèn)題怎么解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。