Java基础-SSM之mybatis的树形控件(自关联)

时间:2021-11-22 16:45:31

           Java基础-SSM之mybatis的树形控件(自关联)

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.准备测试环境(创建数据库表)

1>.创建areas表;

use yinzhengjie;

create table areas(id int primary key auto_increment , areaname varchar(30) , pid int) ;

Java基础-SSM之mybatis的树形控件(自关联)

 2>.添加Maven依赖

 <?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>cn.org.yinzhengjie</groupId>
<artifactId>Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</project>

二.编写自定义类(area.java)

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mybatis.domain.self; import java.util.ArrayList;
import java.util.List; /**
* 自关联
*/
public class Area {
private Integer id ; private String areaName ;
//上级区域
private Area parentArea ; public Area(){
} public Area(String name){
this.areaName = name;
} private List<Area> children = new ArrayList<Area>(); public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getAreaName() {
return areaName;
} public void setAreaName(String areaName) {
this.areaName = areaName;
} public Area getParentArea() {
return parentArea;
} public void setParentArea(Area parentArea) {
this.parentArea = parentArea;
} public List<Area> getChildren() {
return children;
} public void setChildren(List<Area> children) {
this.children = children;
} //定义用于绑定关系的方法
public void addChildren(Area...areas){
for(Area a : areas){
this.getChildren().add(a) ;
a.setParentArea(this);
}
}
}

三.编写配置文件

1>.mybatis-config.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>
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:5200/yinzhengjie"/>
<property name="username" value="root"/>
<property name="password" value="yinzhengjie"/>
</properties> <!-- 我们使用typeAliases标签给我们自定义类起个别名。-->
<typeAliases>
<typeAlias type="cn.org.yinzhengjie.mybatis.domain.self.Area" alias="_Area" />
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 我们使用mapper标签指定映射文件,使用resource指定具体的路径,如果没有写绝对路径,默认的根路径就在resources目录中-->
<mapper resource="AreaMapper.xml"/>
</mappers>
</configuration>

2>.AreaMapper.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">
<!-- 定义名字空间 -->
<mapper namespace="areas">
<!-- useGeneratedKeys的值为true时,表示需要使用数据库深层的keys,同时我们需要指定使用哪个深层的key。而指定具体使用哪个key我们使用keyProperty来定义! -->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into areas(areaname,pid) values(#{areaName}, #{parentArea.id}) ;
</insert> <select id="selectOne" resultMap="rm_Area">
select
s.id sid ,
s.areaname sname ,
p.id pid ,
p.areaname pname,
c.id cid ,
c.areaname cname
from
areas s
left outer join areas p on p.id = s.pid
left outer join areas c on s.id = c.pid
where
s.id = #{id} ;
</select> <resultMap id="rm_Area" type="_Area">
<id column="sid" property="id" />
<result column="sname" property="areaName" />
<association column="pid" property="parentArea" javaType="_Area">
<id column="pid" property="id"/>
<result column="pname" property="areaName"/>
</association>
<collection property="children" ofType="_Area" column="sid">
<id column="cid" property="id"/>
<result column="cname" property="areaName"/>
</collection>
</resultMap>
</mapper>

四.编写测试代码

1>.编写测试代码如下:

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mybatis.test; import cn.org.yinzhengjie.mybatis.domain.self.Area;
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.Test; import java.io.InputStream;
import java.util.List; /**
* 测试自关联
*/
public class TestSelf {
@Test
public void testInsertCustomer() throws Exception {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);
SqlSession sess = sf.openSession();
Area china = new Area("zhongguo") ;
Area bj = new Area("beijing") ;
Area hb = new Area("hebeisheng") ;
Area haidian = new Area("haidian") ;
Area chaoyang = new Area("chaoyang") ;
Area shijiangzhuang = new Area("shijiangzhuang") ;
Area handan = new Area("handan") ;
Area guangfugucheng = new Area("guangfugucheng-1") ;
Area wahuanggong = new Area("wahuanggong-2") ; //绑定关联关系
china.addChildren(bj,hb);
bj.addChildren(haidian,chaoyang);
hb.addChildren(shijiangzhuang,handan);
handan.addChildren(guangfugucheng,wahuanggong); //往数据库中插入数据
sess.insert("areas.insert" , china);
sess.insert("areas.insert" , bj);
sess.insert("areas.insert" , hb);
sess.insert("areas.insert" , haidian);
sess.insert("areas.insert" , chaoyang);
sess.insert("areas.insert" , shijiangzhuang);
sess.insert("areas.insert" , handan);
sess.insert("areas.insert" , guangfugucheng);
sess.insert("areas.insert" , wahuanggong); //提交事物
sess.commit();
sess.close(); System.out.println("数据插入成功!");
} @Test
public void testSelectOne() throws Exception {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);
SqlSession sess = sf.openSession();
Area obj = sess.selectOne("areas.selectOne" , 2) ;
List<Area> children = obj.getChildren();
for (Area child : children) {
System.out.println(child.getAreaName());
}
sess.commit();
sess.close();
}
}

2>.查询数据库执行结果如下:

Java基础-SSM之mybatis的树形控件(自关联)