java mybatis框架实现多表关系查询功能
程序员文章站
2022-03-10 22:50:21
基于maven框架的整体设计 —— 一多一的关系思路:导入mybatis、mysql、junit4.13依赖;编写两个java实体类;编写sqmapconfig.xml mybatis核心配置文件编写...
基于maven框架的整体设计 —— 一多一的关系
思路:导入mybatis、mysql、junit4.13依赖;
编写两个java实体类;
编写sqmapconfig.xml mybatis核心配置文件
编写dao层接口;
编写mapper 映射文件;
编写测试类。
1.导入相关依赖
<!--配置依赖--> <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>
2.编写两个java实体类;
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 + '\'' + '}'; } }
3.编写sqmapconfig.xml mybatis核心配置文件
<?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>
4.编写dao层接口;
package com.mybatis.dao; import com.mybatis.pojo.students; import java.util.list; public interface studentsdao { // 查询学生信息以及对应的身份证信息 public list<students> findall(); }
还有一个略了;
5.编写mapper 映射文件;
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>
6.编写测试类
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多表关系查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!