标签:查找 cad 基本 sel hashset 数据 out stat ref
apache的dbutils包
一对多
package com.cn.bean; import java.util.HashSet; import java.util.Set; 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; } }
package com.cn.bean; 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; } }
package com.cn.dao; import java.sql.SQLException; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.cn.bean.Department; import com.cn.bean.Employee; import com.cn.utils.JdbcUtils; public class DepartmentDao { /* * 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), on delete set null 这个叫做级联,就是如果删除部门的话,会把外键设置为空,具体参考mysql的api * constraint department_id_FK foreign key(department_id) references department(id) ) on delete set null;; */ // 把员工的信息添加到部门中,因为员工的数量无法判断,所以用循环 public void add(Department d) throws SQLException { // 把department对象的数据插入到department表 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); String sql = "insert into department(id,name) values(?,?)"; Object params[] = { d.getId(), d.getName() }; qr.update(sql, params); // 把department对象中维护的所有员工插入到员工表,并且更新员工表的外键列,说明员工的部门 // 因为这里是department的dao,所有employee不能重新定义,直接调用department中的getEmployee方法即可 Set<Employee> set = d.getEmployees(); for (Employee e : set) { sql = "insert into employee(id,name,salary,department_id) values(?,?,?,?)"; params = new Object[] { e.getId(), e.getName(), e.getSalary(), d.getId() }; qr.update(sql, params); } } // 查询某个部门下面有多少员工 public Department find(String id) throws SQLException { // 找部门表,查询部门的基本信息 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from department where id=?"; Department d = (Department) qr.query(sql, id, new BeanHandler( Department.class)); // 查找员工表,找到部门下面的所有员工 sql = "select * from employee where id=? "; List list = (List) qr.query(sql, id, new BeanListHandler(Employee.class)); d.getEmployees().addAll(list); //addAll方法可以添加另外个集合,注意不能用add方法,因为添加进去的是个list集合,而不是员工信息 return d; } //删除某个部门 public void delete(String id) throws SQLException { QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); String sql ="delete from department where id=?"; qr.update(sql, id); } }
package com.cn.service; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.junit.Test; import com.cn.bean.Department; import com.cn.bean.Employee; import com.cn.dao.DepartmentDao; import com.cn.utils.JdbcUtils; //一对多 public class BService { //添加 public void add() throws SQLException { Department d = new Department(); d.setId("111"); d.setName("开发部"); Employee e1 = new Employee(); e1.setId("1"); e1.setName("aa"); e1.setSalary(10000); Employee e2 = new Employee(); e2.setId("2"); e2.setName("bb"); e2.setSalary(10000); d.getEmployees().add(e1); d.getEmployees().add(e2); DepartmentDao dao = new DepartmentDao(); dao.add(d); } @Test public void find() throws SQLException { DepartmentDao dao = new DepartmentDao(); Department d=dao.find("111"); System.out.println(d); } @Test public void delete() throws SQLException { DepartmentDao dao = new DepartmentDao(); dao.delete("111"); } }
package com.cn.utils; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; //连接池 public class JdbcUtils { private static DataSource ds; static { try { Properties prop = new Properties(); prop.load(JdbcUtils.class.getClassLoader().getResourceAsStream( "dbcpconfig.properties")); BasicDataSourceFactory factory = new BasicDataSourceFactory(); ds = factory.createDataSource(prop); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource() { return ds; } public static Connection getConnection() throws SQLException { return ds.getConnection(); } }
然后就上dbcp的配置文件 并且记得导入相关的包:数据库、dbcp、dbutils
多对多
package com.cn.bean; import java.util.HashSet; import java.util.Set; 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; } }
package com.cn.bean; import java.util.HashSet; import java.util.Set; 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; } }
package com.cn.dao; import java.sql.SQLException; import java.util.List; import java.util.Set; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.cn.bean.Student; import com.cn.bean.Teacher; import com.cn.utils.JdbcUtils; //多对多 public class TeacherDao { // 把学生的信息存入到老师中 public void add(Teacher t) throws SQLException { // 取出老师,存入老师表 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); String sql = "insert into teacher(id,name,salary) values(?,?,?)"; Object params[] = { t.getId(), t.getName(), t.getSalary() }; qr.update(sql, params); // 取出老师的所有学生,存入学生表 Set<Student> set = t.getStudents(); for (Student s : set) { sql = "insert into student(id,name) value(?,?)"; params = new Object[] { s.getId(), s.getName() }; qr.update(sql, params); // 更新中间表,说明老师和学生的关系 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.getDataSource()); // 找老师表,找出老师的基本信息 String sql = "select * from teacher where id=?"; Teacher t = (Teacher) qr.query(sql, id, new BeanHandler( Teacher.class)); /* * 找出老师的所有学生 从中间表去找, 中间表里面学生的id和student中的id必须相同 然后判断中间表里面老师的id和传递的id是否相同 * 因为传递的id里面学生的信息是一样的 */ sql = "select s.* from teacher_student as ts,student as s where ts.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; } public void delete(String id) throws SQLException { QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); String sql ="delete from teacher where id=?"; qr.update(sql, id); } }
package com.cn.service; import java.sql.SQLException; import org.junit.Test; import com.cn.bean.Student; import com.cn.bean.Teacher; import com.cn.dao.TeacherDao; //多对多 public class AService { /* 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), on delete cascade 如果用这个的话,那么删除的话会把该表全部删除 constraint teacher_id_Fk foreign key(teacher_id) references teacher(id) on delete cascade, constraint student_id_Fk foreign key(student_id) references student(id) ); */ @Test public void add() throws SQLException { Teacher t = new Teacher(); t.setId("1"); t.setName("老张"); t.setSalary(100000); 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); TeacherDao dao = new TeacherDao(); dao.add(t); } @Test public void findTeacher() throws SQLException { TeacherDao dao = new TeacherDao(); Teacher t=dao.find("1"); System.out.println(t); } }
package com.cn.utils; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; //连接池 public class JdbcUtils { private static DataSource ds; static { try { Properties prop = new Properties(); prop.load(JdbcUtils.class.getClassLoader().getResourceAsStream( "dbcpconfig.properties")); BasicDataSourceFactory factory = new BasicDataSourceFactory(); ds = factory.createDataSource(prop); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource() { return ds; } public static Connection getConnection() throws SQLException { return ds.getConnection(); } }
标签:查找 cad 基本 sel hashset 数据 out stat ref
原文地址:http://www.cnblogs.com/zengjiao/p/7900868.html