String sqlb="insert into employee (id,name,salary,department_id) values(?,?,?,?)";
来源:互联网 发布:lol西门淘宝代练室 编辑:程序博客网 时间:2024/05/18 00:45
package com.tfy.itheima.dao.impl;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.tfy.itheima.domain.Department;
import com.tfy.itheima.domain.Employee;
import com.tfy.itheima.jdbc.util.DbcpUtil;
public class DepartmentDaoImpl {
private QueryRunner qr=new QueryRunner(DbcpUtil.getDataSource());
public void addDepartment(Department dept){
try{
//保存部门的基本信息
String sqla="insert into department (id,name) values(?,?)";
Object params1[]={dept.getId(),dept.getName()};
qr.update(sqla, params1);
//看dept有没有关联的员工信息
List <Employee>emps=dept.getEmps();
//如果有,还要将员工的信息保存到Employeee表中
if(emps!=null && emps.size()>0){
String sqlb="insert into employee (id,name,salary,department_id) values(?,?,?,?)";
Object [][] params2=new Object[emps.size()][];
for(int i=0;i<emps.size();i++){
Employee e=emps.get(i);
params2[i]=new Object[]{e.getId(),e.getName(),e.getSalary(),dept.getId()};
}
qr.batch(sqlb, params2);
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
//对于多的一方,要不要查出来,看业务需求
public Department findDepartmentById(Integer deptId){
return findDepartmentById(deptId, true);
/*try{
String sql1="select * from department where id=?";
Department dept = qr.query(sql1, new BeanHandler<Department>(Department.class),deptId);
String sql2="select * from employee where department_id=?";
List<Employee> emps = qr.query(sql2, new BeanListHandler<Employee>(Employee.class), deptId);
if(emps!=null){
dept.setEmps(emps);
}
return dept;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}*/
}
//对于多的一方,要不要查出来,看业务需求
public Department findDepartmentById(Integer deptId,boolean lazy){
try{
String sql1="select * from department where id=?";
Department dept = qr.query(sql1, new BeanHandler<Department>(Department.class),deptId);
if(lazy){
String sql2="select * from employee where department_id=?";
List<Employee> emps = qr.query(sql2, new BeanListHandler<Employee>(Employee.class), deptId);
if(emps!=null){
dept.setEmps(emps);
}
}
return dept;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.tfy.itheima.domain.Department;
import com.tfy.itheima.domain.Employee;
import com.tfy.itheima.jdbc.util.DbcpUtil;
public class DepartmentDaoImpl {
private QueryRunner qr=new QueryRunner(DbcpUtil.getDataSource());
public void addDepartment(Department dept){
try{
//保存部门的基本信息
String sqla="insert into department (id,name) values(?,?)";
Object params1[]={dept.getId(),dept.getName()};
qr.update(sqla, params1);
//看dept有没有关联的员工信息
List <Employee>emps=dept.getEmps();
//如果有,还要将员工的信息保存到Employeee表中
if(emps!=null && emps.size()>0){
String sqlb="insert into employee (id,name,salary,department_id) values(?,?,?,?)";
Object [][] params2=new Object[emps.size()][];
for(int i=0;i<emps.size();i++){
Employee e=emps.get(i);
params2[i]=new Object[]{e.getId(),e.getName(),e.getSalary(),dept.getId()};
}
qr.batch(sqlb, params2);
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
//对于多的一方,要不要查出来,看业务需求
public Department findDepartmentById(Integer deptId){
return findDepartmentById(deptId, true);
/*try{
String sql1="select * from department where id=?";
Department dept = qr.query(sql1, new BeanHandler<Department>(Department.class),deptId);
String sql2="select * from employee where department_id=?";
List<Employee> emps = qr.query(sql2, new BeanListHandler<Employee>(Employee.class), deptId);
if(emps!=null){
dept.setEmps(emps);
}
return dept;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}*/
}
//对于多的一方,要不要查出来,看业务需求
public Department findDepartmentById(Integer deptId,boolean lazy){
try{
String sql1="select * from department where id=?";
Department dept = qr.query(sql1, new BeanHandler<Department>(Department.class),deptId);
if(lazy){
String sql2="select * from employee where department_id=?";
List<Employee> emps = qr.query(sql2, new BeanListHandler<Employee>(Employee.class), deptId);
if(emps!=null){
dept.setEmps(emps);
}
}
return dept;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
- String sqlb="insert into employee (id,name,salary,department_id) values(?,?,?,?)";
- Column 'name' cannot be null Query: insert into category (id,name,description) values(?,?,?) Paramet
- String sql3="insert into teacher_student (teacher_id,student_id) values(?,?)";
- hibernate不支持insert into values()
- sql双语句: insert into [use](name) values('dd');SELECT @@IDENTITY
- $query = "insert into books values (' ".$isbn." ', ' ".$author." ', ' ".$title." ', ' ".$price." ')"
- 569. Median Employee Salary
- Hibernate 不支持INSERT INTO ... VALUES ...形式
- VC insert into table() values(变量)
- OCP-047 insert into values(defualt)
- oracle insert into values 多条记录
- oracle如何insert into 多个values
- 编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量
- sqlb
- replace into..values/select和insert into ..values/select on duplicate update 异同
- insert into表后获得id
- insert into 同时可用其主键id
- insert into twordcodetemp values('2','YB-88888888','1','2008','0');
- 非阻塞情况下connect产生EINPROGRESS错误
- C/C++中怎样获取日期和时间
- codeforces 230C Shifts DP
- private QueryRunner qr=new QueryRunner();
- mac os x 10.8 安装python-mysqldb血泪史
- String sqlb="insert into employee (id,name,salary,department_id) values(?,?,?,?)";
- 四步创业法
- UVA 10154 Weights and Measures
- Human h = qr.query(sql1, new BeanHandler<Human>(Human.class), id);
- 应该知道的Linux技巧
- String sql3="insert into teacher_student (teacher_id,student_id) values(?,?)";
- C++中内存泄漏的检测方法介绍
- private QueryRunner qr= new QueryRunner(DbcpUtil.getDataSource());
- 从键盘输入一个正整数,按数字的相反顺序输出