zoukankan      html  css  js  c++  java
  • 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);
    	}










     

  • 相关阅读:
    linux之sed用法
    vim 设置tab空格个数
    centos 7远程登陆win10
    linux find命令学习
    CENTOS 7 修改默认启动内核
    Centos7更改默认启动模式
    centos 7创建桌面快捷方式
    修改centos中文为英文显示
    正则的sub
    超时或错误重试
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648033.html
Copyright © 2011-2022 走看看