• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

java_mybatis框架——多表关系查询

开发技术 开发技术 4小时前 1次浏览

基于Maven框架的整体设计 —— 一多一的关系

java_mybatis框架——多表关系查询

 

 

 

 

思路:导入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);
}

}
}

ok!!!

 


 
 
 

程序员灯塔
转载请注明原文链接:java_mybatis框架——多表关系查询
喜欢 (0)