Java -- 数据库 多表操作,1对多,多对多,1对1。 基于dbutils框架
1. 1对多,部门--员工 为例, 多的一方建外键。
domain,建立bean对象
public class Department {
private String id;
private String name;
private Set<Employee> employees = new HashSet<Employee>(); 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 Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public class Employee {
private String id;
private String name;
private double salary;
private Department department;
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
dao层实现增查功能
/*
create table department(
id varchar(40) primary key,
name varchar(40)
); create table employee(
id varchar(40) primary key,
name varchar(40),
salary double,
department_id varchar(40),
constraint department_id_FK foreign key(department_id) references department(id)
);
* */ public class DepartmentDao { public void add(Department department) throws SQLException { QueryRunner qr = new QueryRunner(JdbcUtils_C3P0.getDataSource()); // 1.取出department对象的基本信息存在 department表
String sql = "insert into department(id,name) values(?,?)";
Object[] params = {department.getId(), department.getName()};
qr.update(sql, params); // 2. 取出department对象中所有的员工信息,存在员工表
sql = "insert into employee(id,name,salary,department_id) values(?,?,?,?)";
Set<Employee> set = department.getEmployees();
Object[][] params2 = new Object[set.size()][];
int index = 0;
for(Employee e : set)
{
params2[index++] = new Object[]{e.getId(), e.getName(), e.getSalary(), department.getId()};
}
qr.batch(sql, params2); // 3.更新员工表的外键列,说明员工所属部门
} public Department find(String id) throws SQLException{
QueryRunner qr = new QueryRunner(JdbcUtils_C3P0.getDataSource()); //1.查出部门表的信息,存在department对象中
String sql = "select * from department where id=?";
Department d = (Department) qr.query(sql, id, new BeanHandler(Department.class)); //2.查出部门下的所有员工信息,存在department对象维护的员工对象中
// 不要直接轻易取出所有数据, 数据太多一定要分页
sql = "select id,name,salary from employee where department_id=?";
List list = (List) qr.query(sql, id, new BeanListHandler(Employee.class)); d.getEmployees().addAll(list); return d;
}
}
Service层实现调用, 这里仅用junit做测试
public class BusinessService { DepartmentDao ddao = new DepartmentDao();
TeacherDao tdao = new TeacherDao();
PersonDao pdao = new PersonDao(); @Test
public void addDepartment() throws SQLException
{
Department d = new Department();
d.setId("1");
d.setName("开发部"); Employee e1 = new Employee();
e1.setId("1");
e1.setName("aaa");
e1.setSalary(300); Employee e2 = new Employee();
e2.setId("2");
e2.setName("bbb");
e2.setSalary(300); d.getEmployees().add(e1);
d.getEmployees().add(e2);
ddao.add(d);
} @Test
public void findDepartment() throws SQLException
{
Department d = ddao.find("1");
System.out.println("department name: " + d.getName());
for( Employee e : d.getEmployees() )
{
System.out.println("Employee name: " + e.getName());
}
} @Test
public void addTeacher() throws SQLException
{ Teacher t = new Teacher();
t.setId("1");
t.setName("张三");
t.setSalary(999999); Student s1 = new Student();
s1.setId("1");
s1.setName("aaa"); Student s2 = new Student();
s2.setId("2");
s2.setName("bbb"); t.getStudents().add(s1);
t.getStudents().add(s2); tdao.add(t);
} @Test
public void findTeacher() throws SQLException
{
Teacher t = tdao.find("1");
System.out.println("teacher Name: " + t.getName());
for(Student s : t.getStudents())
{
System.out.println("Student name: " + s.getName());
}
} @Test
public void addPerson() throws SQLException
{
Person p = new Person();
p.setId("id7788");
p.setName("kevin"); Idcard card = new Idcard();
card.setId(p.getId());
card.setAddress("广东中山"); p.setIdcard(card);
pdao.add(p);
} }
2. 多对多,老师--学生为例, 建立中间表
domain,建立bean对象
public class Teacher {
private String id;
private String name;
private double salary;
private Set<Student> students = new HashSet<Student>(); 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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
public class Student {
private String id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>(); 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 Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
Dao层实现 增查功能
/*
create table teacher(
id varchar(40) primary key,
name varchar(40),
salary double
); create table student(
id varchar(40) primary key,
name varchar(40)
); create table teacher_student(
teacher_id varchar(40),
student_id varchar(40),
primary key(teacher_id, student_id),
constraint teacher_id_FK foreign key(teacher_id) references teacher(id),
constraint student_id_FK foreign key(student_id) references student(id)
); * */ public class TeacherDao { public void add(Teacher t) throws SQLException
{
QueryRunner qr = new QueryRunner(JdbcUtils_C3P0.getDataSource()); //1.取出老师的基本信息,存在老师表
String sql = "insert into teacher(id, name, salary) values(?,?,?)";
Object[] params = {t.getId(), t.getName(), t.getSalary()};
qr.update(sql, params); // 2.取出老师下面所有的学生,存在学生表
Set<Student> set = t.getStudents();
for(Student s : set)
{
sql = "insert into student(id, name) values(?,?)";
params = new Object[]{s.getId(), s.getName()};
qr.update(sql, params); // 3.在中间表中说明老师和学生的关系
sql = "insert into teacher_student(teacher_id, student_id) values(?,?)";
params = new Object[]{t.getId(), s.getId()};
qr.update(sql, params);
}
} public Teacher find(String id) throws SQLException
{
QueryRunner qr = new QueryRunner(JdbcUtils_C3P0.getDataSource()); // 1.从老师表中找出老师的基本信息
String sql = "select * from teacher where id=?";
Teacher t = (Teacher) qr.query(sql, id, new BeanHandler(Teacher.class)); // 2.从中间表和学生表中得到老师所有的学生(尽量不取,取则 数据多要分页)
sql = "select s.id,s.name from teacher_student ts,student s where teacher_id=? and ts.student_id=s.id";
List<Student> list = (List<Student>) qr.query(sql, id, new BeanListHandler(Student.class));
t.getStudents().addAll(list);
return t;
}
}
service 层代码 如上 例一。
3. 1对1, 人--身份证为例, 建立主从关系
domain,建立bean对象
public class Person { private String id;
private String name;
private Idcard idcard; 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 Idcard getIdcard() {
return idcard;
}
public void setIdcard(Idcard idcard) {
this.idcard = idcard;
}
}
public class Idcard { private String id;
private String address;
private Person person; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Dao层 实现增功能
/*
create table person(
id varchar(40) primary key,
name varchar(40)
); create table idcard(
id varchar(40) primary key,
address varchar(100),
constraint id_FK foreign key(id) references person(id)
);
* */ public class PersonDao { public void add(Person p) throws SQLException { QueryRunner qr = new QueryRunner(JdbcUtils_C3P0.getDataSource()); // 取出person的信息存在person表
String sql = "insert into person(id,name) values(?,?)";
Object params[] = { p.getId(), p.getName() };
qr.update(sql, params); // 取出身份证的信息存在身份证表
sql = "insert into idcard(id,address) values(?,?)";
params = new Object[] { p.getId(), p.getIdcard().getAddress() };
qr.update(sql, params);
}
Java -- 数据库 多表操作,1对多,多对多,1对1。 基于dbutils框架的更多相关文章
- c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--跨表操作)
上篇文章我们介绍了如何利用DataSet 和 DataAdaper对象来对单张表进行操作. 本文我们将介绍如何进行跨表操作. 我们通过具体例子方式进行演示,例子涉及到三张表. 1)student表(学 ...
- c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--单表操作)
一.概述 前面2篇文章,介绍了使用SqlCommand对象利用sql命令来操作数据库. 这篇文章我们来介绍使用c#的DataSet 和 DataAdaper对象操作操作数据库. 先来介绍下这两个对象是 ...
- SQL Server 基础 01 数据库、表操作
对着书慢慢学习,一天一点点! 数据库操作 (create.alter.drop) --3-3-1 /create database 语句创建数据库 create database testSQL - ...
- Java Web 单表操作
为了测试 JavaWeb 项目,我做了一个关于一张表的更新操作. 1.加群 如果需要本项目的源码,欢迎加群:438255459 2.解压 解压开这个包,然后用 Eclipse 打开项目,至于涉及到 T ...
- 数据库 -- mysql表操作
一,存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 详见:https://www.cnblogs.com/peng104/p/9751738.html 二,表介绍 表相当 ...
- day 46 Django 学习3 数据库单表操作以及反向解析
前情提要: Django 已经学了不少了, 今天学习链接数据库的操作.以及相关的反向解析等 一:反向解析 1:反向解析模板层 跳转时设定url会随着前面的路由改变而改变 2:反向解析之 ...
- zabbix 数据库分表操作
近期zabbix数据库占用的io高,在页面查看图形很慢,而且数据表已经很大,将采用把数据库的数据目录移到新的磁盘,将几个大表进行分表操作 一.数据迁移: 1.数据同步到新的磁盘上,先停止mysql(不 ...
- MySQL-快速入门(1)基本数据库、表操作语句
1.创建数据库 create database db_name;show create database db_name\G; //查看数据创建语句show databases; //查看当前创建的数 ...
- MySQL快速回顾:数据库和表操作
前提要述:参考书籍<MySQL必知必会> 利用空闲时间快速回顾一些数据库基础. 4.1 连接 在最初安装MySQL,可能会要求你输入一个管理登录(通常为root)和一个口令(密码). 连接 ...
随机推荐
- Java Modifier
- 《从零开始学Swift》学习笔记(Day 30)——选择类还是结构体呢?
原创文章,欢迎转载.转载请注明:关东升的博客 类和结构体非常相似,很多情况下没有区别.如果你是设计人员在进行系统设计时候,是将某种类型设计成为类还是结构体? 类和结构体异同: 类和结构体都有如下功能: ...
- <2013 08 26> 雅思听力相关
近两日开始接触雅思题型,初步做了6套剑桥雅思题的听力部分,完成情况还可以,这里做个总结. 1.听力总共约40左右道题目,30min左右完成,结束后有十分钟把答案写到答题卷上.所有听力材料都只播放一遍! ...
- python面向对象(二)
属性查找 类有两种属性:数据属性和函数属性 1. 类的数据属性是所有对象共享的 2. 类的函数属性是绑定给对象用的 class BeijingStudent: school='Beijing' ...
- git原理:.git隐藏文件夹介绍
config 定义项目特有的配置选项description 仅供git web程序使用info/ 包含一个全局排除文件(exclude文件),用于配置不在.gitignore中的忽略模式hooks/ ...
- (4.14)存储:RAID在数据库存储上的应用
关键词:(4.14)存储:RAID在数据库存储上的应用 转自:http://blog.51cto.com/qianzhang/1251260 随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列 ...
- java.sql.SQLException: The user specified as a definer ('root'@'%') does not exist
权限问题,授权给 root 所有sql 权限 在Navicat for MySQL中按F6进入命令列界面 mysql> grant all privileges on *.* to root@& ...
- 4.6 基于STM32+MC20地图显示路径功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- Docker基础入门实践
原文地址 基本概念 Docker三个基本概念 镜像(Image) 容器(Container) 仓库(Repository) 镜像 镜像可以理解为只读模板,如包含一个centos操作系统环境 容器 Do ...
- position:absolute width
position:absolute; left:0px; right:0px; top:0px; bottom:0px; 设置布满整个父范围,设置了absolute的元素可以通过以上4个属性来展开面, ...