Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java mybatis Framework to realize Multi-table Relational query function

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use the java mybatis framework to achieve multi-table relational query function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use the java mybatis framework to achieve multi-table relational query function" bar!

Overall Design based on Maven Framework-- one-to-one relationship

Idea: import mybatis, mysql, Junit4.13 dependencies

Write two java entity classes

Write sqMapConfig.xml mybatis core configuration file

Write dao layer interface

Write mapper mapping file

Write test classes.

1. Import dependent org.mybatis mybatis 3.5.2 mysql mysql-connector-java 5.1.47 junit junit 4.13 2. Write two java entity classes

Students

Package com.mybatis.pojo;public class Students {private Integer sid; private String sName; private Integer sCid; private Cards cards; / / one-to-one relationship public Integer getSid () {return sid;} public void setSid (Integer sid) {this.sid = sid;} public String getsName () {return sName;} public void setsName (String sName) {this.sName = sName } public Integer getsCid () {return sCid;} public void setsCid (Integer sCid) {this.sCid = sCid;} public Cards getCards () {return cards;} public void setCards (Cards cards) {this.cards = cards } @ Override public String toString () {return "Students {" + "sid=" + sid + ", sName='" + sName +'\'+ ", sCid=" + sCid + ", cards=" + cards +'}';}}

Cards

Package com.mybatis.pojo;public class Cards {private Integer cid; private String cnum; public Integer getCid () {return cid;} public void setCid (Integer cid) {this.cid = cid;} public String getCnum () {return cnum;} public void setCnum (String cnum) {this.cnum = cnum } @ Override public String toString () {return "Cards {" + "cid=" + cid + ", cnum='" + cnum +'\'+'}';}} 3. Write sqMapConfig.xml mybatis core configuration file 4. Write dao layer interface; package com.mybatis.dao;import com.mybatis.pojo.Students;import java.util.List;public interface StudentsDao {/ / query student information and corresponding ID card information public List findAll ();}

There is another one left.

5. Write mapper mapping file

StudentsDao.xml

Select s.sname,c.cnum from students s,cards c where s.scid=c.cid

CardsDao.xml

6. Write the test class import com.mybatis.dao.StudentsDao;import com.mybatis.pojo.Students;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 TestMybatis {InputStream resource; SqlSession session; StudentsDao stuDao @ Before / / execute public void init () throws IOException before test method {/ / load core configuration file resource = Resources.getResourceAsStream ("sqMapConfig.xml"); / / create sqlSessionFactoryBuilder object SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder (); / / create sqlSessionFactory object SqlSessionFactory build = builder.build (resource); / / create Session object session = build.openSession () / / execute method stuDao = session.getMapper (StudentsDao.class);} @ After / / execute public void close () throws IOException {/ / close resource session.close () after test method; resource.close ();} @ Test public void test01 () {List list = stuDao.findAll () For (Students students: list) {System.out.println (students) Thank you for your reading, the above is the content of "how to use java mybatis framework to achieve multi-table relational query function". After the study of this article, I believe you have a deeper understanding of how to use java mybatis framework to achieve multi-table relational query function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report