博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java -- 数据库 多表操作,1对多,多对多,1对1。 基于dbutils框架
阅读量:5162 次
发布时间:2019-06-13

本文共 8762 字,大约阅读时间需要 29 分钟。

1. 1对多,部门--员工 为例, 多的一方建外键。

domain,建立bean对象

public class Department {	private String id;	private String name;	private Set
employees = new HashSet
(); 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
getEmployees() { return employees; } public void setEmployees(Set
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
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
students = new HashSet
(); 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
getStudents() { return students; } public void setStudents(Set
students) { this.students = students; } }
public class Student {	private String id;	private String name;	private Set
teachers = new HashSet
(); 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
getTeachers() { return teachers; } public void setTeachers(Set
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
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
list = (List
) 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);	}

 

转载于:https://www.cnblogs.com/xj626852095/p/3648033.html

你可能感兴趣的文章
github如何查看提交历史呢
查看>>
socket概念详解
查看>>
如何在盘中抓住个股起涨的信号
查看>>
面试记
查看>>
Spring集成ActiveMQ配置 --转
查看>>
python高性能web框架Sanic学习--url
查看>>
hive 入门
查看>>
Java heap size
查看>>
计算机网络
查看>>
SQLSERVER中的 CEILING函数和 FLOOR函数
查看>>
开启子进程的方式2
查看>>
js正则表达
查看>>
1041-线段相交
查看>>
关于老教授之家项目的思考 && 中国互联网+大赛培训
查看>>
redis常用命令
查看>>
The first article
查看>>
Python爬虫scrapy-redis分布式实例(一)
查看>>
poj 3009 Curling 2.0
查看>>
java基础问题--春招准备:java基础第一记
查看>>
n阶乘,位数,log函数,斯特林公式
查看>>