💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
按正常的开发流程中,对类图进行开发的时候,所有的时序图都已经完成了。所以,类图中的方法是完全的。所以我们可以利用UML图表工作,将图转化为代码。 但本章中,我们每建一个action就开始进行对接开发,这使得,在给类补充方法后,无法使用工具直接进行代码的转换,这时候,就只能手动的补充了。 ![https://box.kancloud.cn/422f9535e59bed28a05def71c1eb1d89_676x192.png](https://box.kancloud.cn/422f9535e59bed28a05def71c1eb1d89_676x192.png) 按上图,补充代码如下: ``` package com.mengyunzhi.javaee.server; ... public class KlassServer { ... /** * 获取klass实体 * @param id 关键字 * @return Klass */ public static Klass getKlassById(Long id) { KlassDao klassDao = new KlassDao(); Klass klass = (Klass) klassDao.get(id); return klass; } ... ``` # 单元测试 ``` @Test public void getKlassByIdTest() { Klass klass = KlassServer.getKlassById(1L); System.out.println(klass); } ``` 启动测试后,控制台信息如下: ``` Hibernate: select klass0_.klassId as klassId1_0_1_, klass0_.name as name2_0_1_, klass0_.teacherId as teacherI3_0_1_, teacher1_.teacherId as teacherI1_1_0_, teacher1_.email as email2_1_0_, teacher1_.name as name3_1_0_, teacher1_.password as password4_1_0_, teacher1_.sex as sex5_1_0_, teacher1_.username as username6_1_0_ from Klass klass0_ left outer join Teacher teacher1_ on klass0_.teacherId=teacher1_.teacherId where klass0_.klassId=? com.mengyunzhi.javaee.entity.Klass@210ab13f ``` 有人说,打印出来的klass呢? 这就是:com.mengyunzhi.javaee.entity.Klass@210ab13f 就是由于,在上节中,我们添加完teacher属性性,将klass实体中的toString方法删除掉造成的。为了更好的看到返回的具体值,我们在Klass中使用eclipse自带的方法,生成toString方法。 ``` package com.mengyunzhi.javaee.entity; public class Klass implements IdEntity { ... @Override public String toString() { return "Klass [klassId=" + klassId + ", name=" + name + ", teacher=" + teacher + "]"; } ... ``` 然后保存后,再次进行单元测试 ``` Klass [klassId=1, name=一一班, teacher=Teacher [teacherId=1, name=李四, username=lisi, email=lisi@yunzhiclub.com, sex=false, password=234]] ``` 此时,我们看到,我们不但获取了klass的信息,而且与Klass相关联的Teacher实体信息,也被直接返回了。