Java基础-SSM之mybatis的树形控件(自关联)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.准备测试环境(创建数据库表)
1>.创建areas表;
use yinzhengjie; create table areas(id int primary key auto_increment , areaname varchar(30) , pid int) ;
2>.添加Maven依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>cn.org.yinzhengjie</groupId> 7 <artifactId>Mybatis</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 <dependencies> 10 <dependency> 11 <groupId>junit</groupId> 12 <artifactId>junit</artifactId> 13 <version>4.11</version> 14 </dependency> 15 <dependency> 16 <groupId>mysql</groupId> 17 <artifactId>mysql-connector-java</artifactId> 18 <version>5.1.17</version> 19 </dependency> 20 <dependency> 21 <groupId>org.mybatis</groupId> 22 <artifactId>mybatis</artifactId> 23 <version>3.2.1</version> 24 </dependency> 25 </dependencies> 26 </project>
二.编写自定义类(area.java)
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.mybatis.domain.self; 7 8 import java.util.ArrayList; 9 import java.util.List; 10 11 12 /** 13 * 自关联 14 */ 15 public class Area { 16 private Integer id ; 17 18 private String areaName ; 19 //上级区域 20 private Area parentArea ; 21 22 public Area(){ 23 } 24 25 public Area(String name){ 26 this.areaName = name; 27 } 28 29 private List<Area> children = new ArrayList<Area>(); 30 31 public Integer getId() { 32 return id; 33 } 34 35 public void setId(Integer id) { 36 this.id = id; 37 } 38 39 public String getAreaName() { 40 return areaName; 41 } 42 43 public void setAreaName(String areaName) { 44 this.areaName = areaName; 45 } 46 47 public Area getParentArea() { 48 return parentArea; 49 } 50 51 public void setParentArea(Area parentArea) { 52 this.parentArea = parentArea; 53 } 54 55 public List<Area> getChildren() { 56 return children; 57 } 58 59 public void setChildren(List<Area> children) { 60 this.children = children; 61 } 62 63 //定义用于绑定关系的方法 64 public void addChildren(Area...areas){ 65 for(Area a : areas){ 66 this.getChildren().add(a) ; 67 a.setParentArea(this); 68 } 69 } 70 }
三.编写配置文件
1>.mybatis-config.xml 文件内容
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties> 7 <property name="driver" value="com.mysql.jdbc.Driver"/> 8 <property name="url" value="jdbc:mysql://localhost:5200/yinzhengjie"/> 9 <property name="username" value="root"/> 10 <property name="password" value="yinzhengjie"/> 11 </properties> 12 13 <!-- 我们使用typeAliases标签给我们自定义类起个别名。--> 14 <typeAliases> 15 <typeAlias type="cn.org.yinzhengjie.mybatis.domain.self.Area" alias="_Area" /> 16 </typeAliases> 17 18 <environments default="development"> 19 <environment id="development"> 20 <transactionManager type="JDBC"/> 21 <dataSource type="POOLED"> 22 <property name="driver" value="${driver}"/> 23 <property name="url" value="${url}"/> 24 <property name="username" value="${username}"/> 25 <property name="password" value="${password}"/> 26 </dataSource> 27 </environment> 28 </environments> 29 <mappers> 30 <!-- 我们使用mapper标签指定映射文件,使用resource指定具体的路径,如果没有写绝对路径,默认的根路径就在resources目录中--> 31 <mapper resource="AreaMapper.xml"/> 32 </mappers> 33 </configuration>
2>.AreaMapper.xml 文件内容
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 定义名字空间 --> 5 <mapper namespace="areas"> 6 <!-- useGeneratedKeys的值为true时,表示需要使用数据库深层的keys,同时我们需要指定使用哪个深层的key。而指定具体使用哪个key我们使用keyProperty来定义! --> 7 <insert id="insert" useGeneratedKeys="true" keyProperty="id"> 8 insert into areas(areaname,pid) values(#{areaName}, #{parentArea.id}) ; 9 </insert> 10 11 <select id="selectOne" resultMap="rm_Area"> 12 select 13 s.id sid , 14 s.areaname sname , 15 p.id pid , 16 p.areaname pname, 17 c.id cid , 18 c.areaname cname 19 from 20 areas s 21 left outer join areas p on p.id = s.pid 22 left outer join areas c on s.id = c.pid 23 where 24 s.id = #{id} ; 25 </select> 26 27 <resultMap id="rm_Area" type="_Area"> 28 <id column="sid" property="id" /> 29 <result column="sname" property="areaName" /> 30 <association column="pid" property="parentArea" javaType="_Area"> 31 <id column="pid" property="id"/> 32 <result column="pname" property="areaName"/> 33 </association> 34 <collection property="children" ofType="_Area" column="sid"> 35 <id column="cid" property="id"/> 36 <result column="cname" property="areaName"/> 37 </collection> 38 </resultMap> 39 </mapper>
四.编写测试代码
1>.编写测试代码如下:
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.mybatis.test; 7 8 import cn.org.yinzhengjie.mybatis.domain.self.Area; 9 import org.apache.ibatis.io.Resources; 10 import org.apache.ibatis.session.SqlSession; 11 import org.apache.ibatis.session.SqlSessionFactory; 12 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 13 import org.junit.Test; 14 15 import java.io.InputStream; 16 import java.util.List; 17 18 /** 19 * 测试自关联 20 */ 21 public class TestSelf { 22 @Test 23 public void testInsertCustomer() throws Exception { 24 InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); 25 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in); 26 SqlSession sess = sf.openSession(); 27 Area china = new Area("zhongguo") ; 28 Area bj = new Area("beijing") ; 29 Area hb = new Area("hebeisheng") ; 30 Area haidian = new Area("haidian") ; 31 Area chaoyang = new Area("chaoyang") ; 32 Area shijiangzhuang = new Area("shijiangzhuang") ; 33 Area handan = new Area("handan") ; 34 Area guangfugucheng = new Area("guangfugucheng-1") ; 35 Area wahuanggong = new Area("wahuanggong-2") ; 36 37 //绑定关联关系 38 china.addChildren(bj,hb); 39 bj.addChildren(haidian,chaoyang); 40 hb.addChildren(shijiangzhuang,handan); 41 handan.addChildren(guangfugucheng,wahuanggong); 42 43 44 //往数据库中插入数据 45 sess.insert("areas.insert" , china); 46 sess.insert("areas.insert" , bj); 47 sess.insert("areas.insert" , hb); 48 sess.insert("areas.insert" , haidian); 49 sess.insert("areas.insert" , chaoyang); 50 sess.insert("areas.insert" , shijiangzhuang); 51 sess.insert("areas.insert" , handan); 52 sess.insert("areas.insert" , guangfugucheng); 53 sess.insert("areas.insert" , wahuanggong); 54 55 //提交事物 56 sess.commit(); 57 sess.close(); 58 59 System.out.println("数据插入成功!"); 60 } 61 62 @Test 63 public void testSelectOne() throws Exception { 64 InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); 65 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in); 66 SqlSession sess = sf.openSession(); 67 Area obj = sess.selectOne("areas.selectOne" , 2) ; 68 List<Area> children = obj.getChildren(); 69 for (Area child : children) { 70 System.out.println(child.getAreaName()); 71 } 72 sess.commit(); 73 sess.close(); 74 } 75 }
2>.查询数据库执行结果如下: