mybatis关联多表(一对多)
首先要求的是maven项目哈
在作业中,我们有两张表
一个是policy(包含政策的基本信息,id,type,name,organ,text 等等)
一个是policy_kind(包含pid,type)
老师要求的就是利用policy_kind中与policy中type相同的数据对于所有的policy进行一个分类查询
就比方这个样子:
其实他要求的应该是这个样子,但是我还没研究透怎么把这东西和后端连接起来,所以套了个模板
所以本例子中的一对多,就是一(Menu)对 多(policy)
项目列表(我觉得这个很重要,有时候参考教程的时候都不知道文件建在哪个地方,可能是我笨吧)
首先maven项目的pom.xml 添加mybatis依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>2022.10.25</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>2022.10.25 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties>
//从这开始 <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> </dependencies> //到此结束
实体类
Menu.java
package Bean;
import java.util.List;
public class Menu {
int pid; //注意这个地方
String type;
List<policy> policy; //建立一个policy的list
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Bean.policy> getPolicy() {
return policy;
}
public void setPolicy(List<Bean.policy> policy) {
this.policy = policy;
}
}
policy.java
package Bean;
import java.io.Serializable;
public class policy implements Serializable {
String id;
String type;
String name;
String organ;
String pubdata;
Menu menu; //注意这个地方
public String getOrgan() {
return organ;
}
public void setOrgan(String organ) {
this.organ = organ;
}
public String getPubdata() {
return pubdata;
}
public void setPubdata(String pubdata) {
this.pubdata = pubdata;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
}
建立MenuMapper.java 接口
package Bean;
import java.util.List;
public interface MenuMapper {
// ResultMap标签
public List<Menu> FindListMenu();
}
MenuMapper.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">
<!--所有映射文件中的sql语句,为了区分多个映射文件,使用命名空间+id进行区分。namespace命名空间-->
<mapper namespace="com.dao.MenuMapper(此处需要更改)">
<!--自定义结果映射-->
<resultMap id="resultMap" type="Bean.Menu(此处需要更改)">
<!--如果数据库中有主键,写id映射-->
<id column="pid" property="pid" />(主键记得改)
<result property="type" column="type"/>(policy_kind表里面的元组)
<!--一对多关系映射 (全类名.resultMap)-->
<collection property="policy" ofType="Bean.policy" column="type" >
<id column="id" property="id" />(主键记得改)
<result property="type" column="type"/>
<result property="name" column="name"/>
<result property="organ" column="organ"/>
<result property="pubdata" column="pubdata"/>(需要的policy表中的元组)
</collection>
</resultMap>
<select id="FindListMenu" resultMap="resultMap">(记住FindListMenu这个id)
select c.*,o.* from policy_kind c left join policy o on c.ptype=o.type where c.pid=#{id}
</select>
</mapper>
MenuConfig.xml(自己建的)
<?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>
<typeAliases>
<typeAlias type="Bean.Menu" alias="Menu"/>
<typeAlias type="Bean.policy" alias="policy"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/policy"/>(数据库信息自己改)
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com.dao/MenuMapper.xml"/>(路径自己改)
</mappers>
</configuration>
最后test.java
package Bean;
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 java.io.IOException;
import java.io.Reader;
import java.util.List;
public class test {
public static void main(String[] args) throws IOException{
System.out.println("测试开始");
try {
//使用MyBatis提供的Resources类加载mybatis的配置文件
Reader reader = Resources.getResourceAsReader("com.dao\\MenuConfig.xml");
//构建sqlSession的工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
//通过SqlSessionFactory对象获取SqlSession对象。
SqlSession session = sf.openSession();
Menu menu = session.selectOne("FindListMenu",100);
System.out.println("pid"+menu.getPid()+"type"+menu.getType());
List<policy> list =menu.getPolicy();
for(policy p :list){
System.out.println("id"+p.getId()+
"name"+p.getName());
}
//提交事务
session.commit();
}catch (Exception e){
e.printStackTrace();
}
}
}
测试成功的结果
很多博主就在这个地方设置输出的数据格式,然后方便连接前端的tree等等,但我没搞懂
所以我用的还是老办法
自己换成函数方便放在前端的话可以用这个
参考文件河北省科技政策查询系统(二) - zrswheart - 博客园 (cnblogs.com)
public List<bean> search2(int id){
List<bean> list = new ArrayList<bean>();
try {
//使用MyBatis提供的Resources类加载mybatis的配置文件
Reader reader = Resources.getResourceAsReader("com.dao\\MenuConfig.xml");
//构建sqlSession的工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
//通过SqlSessionFactory对象获取SqlSession对象。
SqlSession session = sf.openSession();
Menu menu = session.selectOne("FindListMenu",id);
List<policy> lists =menu.getPolicy();
for(policy p :lists){
bean lu = new bean();
lu.setId(p.getId());
lu.setType(p.getType());
lu.setOrgan(p.getOrgan());
lu.setPubdata(p.getPubdata());
lu.setName(p.getName());
list.add(lu);
}
//提交事务
session.commit();
}catch (Exception e){
e.printStackTrace();
}
return list;
}