这篇文章主要讲解了“怎么用java mybatis框架实现多表关系查询功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用java mybatis框架实现多表关系查询功能”吧!
基于Maven框架的整体设计 —— 一多一的关系
思路:导入mybatis、mysql、Junit4.13依赖;
编写两个java实体类;
编写sqMapConfig.xml mybatis核心配置文件
编写dao层接口;
编写mapper 映射文件;
编写测试类。
<!--配置依赖--> <dependencies> <!--配置mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--配合mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--配置单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency> </dependencies>
Students
package com.mybatis.pojo; public class Students { private Integer sid; private String sName; private Integer sCid; private Cards cards; // 一多一的关系 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 + '\'' + '}'; } }
<?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文件 --> <properties resource="jdbc.properties"></properties> <!--配置实体类相对路径 别名--> <typeAliases> <typeAlias type="com.mybatis.pojo.Students" alias="Stu"></typeAlias> <typeAlias type="com.mybatis.pojo.Cards" alias="Car"></typeAlias> </typeAliases> <!--配置mybatis环境--> <environments default="mybatis"> <environment id="mybatis"> <transactionManager type="jdbc"></transactionManager> <dataSource type="pooled"> <property name="driver" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </environment> </environments> <!-- 配置mapper.xml映射文件 resource:相对路径 --> <mappers> <mapper resource="com.mybatis.dao/studentsDao.xml"></mapper> <mapper resource="com.mybatis.dao/CardsDao.xml"></mapper> <!--<package name="com.mybatis.dao"></package>--> </mappers> </configuration>
package com.mybatis.dao; import com.mybatis.pojo.Students; import java.util.List; public interface StudentsDao { // 查询学生信息以及对应的身份证信息 public List<Students> findAll(); }
还有一个略了;
StudentsDao.xml
<?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"> <!--namespace 值是dao层对应的接口--> <mapper namespace="com.mybatis.dao.StudentsDao"> <!-- 配置实体类字段与数据库字段一致 --> <resultMap id="stuMap" type="Stu"> <id property="sid" column="sid"></id> <result property="sName" column="sname"></result> <result property="sCid" column="scid"></result> <association property="cards" resultMap="com.mybatis.dao.CardsDao.cardsMap"></association> </resultMap> <!-- 查询学生信息以及对应的身份证信息 resultMap:实体类与数据库映射的数据类型 --> <select id="findAll" resultMap="stuMap"> select s.sname,c.cnum from students s,cards c where s.scid=c.cid; </select> </mapper>
CardsDao.xml
<?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"> <!--namespace 值是dao层对应接口的权限定名--> <mapper namespace="com.mybatis.dao.CardsDao"> <!--配置实体类字段与数据库一致--> <resultMap id="cardsMap" type="car"> <!-- property:实体字段 column:数据库字段 --> <id property="cid" column="cid"></id> <result property="cnum" column="cnum"></result> </resultMap> </mapper>
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 // 在test方法之前执行 public void init() throws IOException { // 加载核心配置文件 resource = Resources.getResourceAsStream("sqMapConfig.xml"); // 创建sqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); // 创建sqlSessionFactory对象 SqlSessionFactory build = builder.build(resource); // 创建Session对象 session = build.openSession(); // 执行方法 stuDao = session.getMapper(StudentsDao.class); } @After // 在test方法之后执行 public void close() throws IOException { // 关闭资源 session.close(); resource.close(); } @Test public void test01(){ List<Students> list = stuDao.findAll(); for (Students students : list) { System.out.println(students); } } }
感谢各位的阅读,以上就是“怎么用java mybatis框架实现多表关系查询功能”的内容了,经过本文的学习后,相信大家对怎么用java mybatis框架实现多表关系查询功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。