数据表与简单java类——一对多映射
2024-09-01 18:44:23
例如:给定一个分类表和子分类表
得到如下信息:
1.一个分类的完整信息;
2.根据分类获取其对应的子分类
package Mapping_transformation; class item
{
private long id ;
private String title ;
private subitem subitems[] ; //一个分类有多个子分类
public item(long id , String title)
{
this.id = id ;
this.title = title ;
} public void setSubitems(subitem[] subitems) {
this.subitems = subitems;
} public subitem[] getSubitems() {
return subitems;
} public String getInfo()
{
return "【分类信息】id = "+this.id + " title = "+this.title ;
} }
class subitem
{
private long id ;
private String title ;
private item item ;
public subitem(long id , String title)
{
this.id = id ;
this.title = title ;
}
public void setItem(item item)
{
this.item = item ;
}
public item getItem()
{
return item ;
}
public String getInfo()
{
return "【子分类信息】id = "+this.id + " title = "+this.title ;
}
}
public class One_to_many {
public static void main(String[] args) {
item item = new item(001,"编程书籍");
subitem subitems[] = new subitem[]{
new subitem(002,"java从入门到放弃"),
new subitem(003,"c++从入门到放弃")
};
item.setSubitems(subitems); //一个分类有多个子分类 for(int i = 0 ; i<subitems.length ; i++)
{
subitems[i].setItem(item);
} System.out.println(item.getInfo());
for(int i = 0 ; i<item.getSubitems().length ; i++)
{
System.out.println(item.getSubitems()[i].getInfo());
}
}
}
resul:
【分类信息】id = 1 title = 编程书籍
【子分类信息】id = 2 title = java从入门到放弃
【子分类信息】id = 3 title = c++从入门到放弃
最新文章
- mysql导入大文件sql
- EhCache缓存
- 难得的中文ASP.NET 5/MVC 6入门教程
- 《SQL Server企业级平台管理实践》读书笔记——SQL Server如何设置自动增长和自动收缩项
- 在 FREEBUF 投放广告
- 使用shape设置只有部分边框有颜色
- Spring事务管理中@Transactional
- D. Green and Black Tea
- Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题
- 向PHP语言进发
- python threading 模块来实现多线程
- rebase
- 【2016年终大典】i春秋一年中不可错过的安全精华
- 【Java面试宝典】深入理解JAVA虚拟机
- Go 标准库 —— sync.Mutex 互斥锁
- Java API概述
- logback配置详解
- 在Linux下面如何查看tomcat已经使用多少线程(Threads)
- int与String互转
- php -- 显示当前时间