三层架构
数据访问层
业务逻辑层
表示层
方便团队开发,代码复用
不属于三层,但跟三层息息相关——实体类(跟数据库表对应的类)
数据访问层
连接数据库,执行sql语句
cn.edu.xcu.sims.dao
BaseDao
//增删改的封装
public int executeUpdate(String sql,Object ...args) {
try {
Connection conn=this.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
if(args!=null) {
for(int i=0;i
StudentDao
接口
//数据访问对象
public interface StudentDao {
public boolean insertStudent(Students stu);
public boolean deleteStudent(int stuid);
public boolean updateStudent(Students stu);
public Students findStudent(int stuid);
public List queryStudentByGrade(int gradeid);
public List queryStudentByName(String stuname);
public List queryAllStudent();
public List queryAllStudentBykeyword(String stunameKeyword);
public default Students parseResultSet(ResultSet rs) {
try {
Students stu=new Students();
stu.setStuid(rs.getInt("stuid"));
stu.setStuname(rs.getString("stuname"));
stu.setGender(rs.getString("gender"));
stu.setPhone(rs.getString("phone"));
stu.setAddress(rs.getString("address"));
Grade grade=new Grade(rs.getInt("gradeId"),rs.getString("gradeName"));
stu.setGrade(grade);
return stu;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
cn.edu.xcu.sims.dao.impl
StudentDaoImpl
实现类
public class StudentDaoImpl extends BaseDao1 implements StudentDao{
Scanner input=new Scanner(System.in);
@Override
public boolean insertStudent(Students stu) {
BaseDao1 bd=new BaseDao1();
String sql="insert into students values(sql_students_stuid.nextval,?,?,?,?,?)";
return bd.executeUpdate(sql,stu.getStuname(),stu.getGender(),stu.getGrade().getGradeId(),stu.getPhone(),stu.getAddress())>0;
}
@Override
public boolean deleteStudent(int stuid) {
String sql="delete from students where stuid=?";
return super.executeUpdate(sql, stuid)>0;
}
@Override
public boolean updateStudent(Students stu) {
System.out.println("请选择要修改的信息:修改姓名请输入1,修改性别请输入2,修改年级请输入3,修改电话请输入4,修改地址请输入5");
String sql="update students set stuname=?,gender=?,gradeid=?,phone=?,address=? where students.stuid=?";
switch (input.nextInt()) {
case 1:
System.out.println("请输入姓名:");
String stuname=input.next();
int rows1=super.executeUpdate(sql, stuname,stu.getGender(),stu.getGrade().getGradeId(),stu.getPhone(),stu.getAddress(),stu.getStuid());
return rows1>0;
case 2:
System.out.println("请输入性别:");
String gender=input.next();
int rows2=super.executeUpdate(sql, stu.getStuname(),gender,stu.getGrade().getGradeId(),stu.getPhone(),stu.getAddress(),stu.getStuid());
return rows2>0;
case 3:
System.out.println("请输入年级号:");
String gradeid=input.next();
int rows3=super.executeUpdate(sql, stu.getStuname(),stu.getGender(),gradeid,stu.getPhone(),stu.getAddress(),stu.getStuid());
return rows3>0;
case 4:
System.out.println("请输入性别:");
String phone=input.next();
int rows4=super.executeUpdate(sql, stu.getStuname(),stu.getGender(),stu.getGrade().getGradeId(),phone,stu.getAddress(),stu.getStuid());
return rows4>0;
case 5:
System.out.println("请输入性别:");
String address=input.next();
int rows5=super.executeUpdate(sql, stu.getStuname(),stu.getGender(),stu.getGrade().getGradeId(),stu.getPhone(),address,stu.getStuid());
return rows5>0;
default:
break;
}
return false;
}
@Override
public Students findStudent(int stuid) {
String sql="select students.*,gradeName from students inner join grade on students.gradeid=grade.gradeId where stuid=?";
Students stu=new Students();
ResultSet rs=super.executeQuery(sql, stuid);
try {
if(rs.next()) { stu.setStuid(rs.getInt("stuid")); stu.setStuname(rs.getString("stuname"));
stu.setGender(rs.getString("gender"));
stu.setPhone(rs.getString("phone"));
stu.setAddress(rs.getString("address"));
Grade grade=new Grade(rs.getInt("gradeId"),rs.getString("gradeName"));
stu.setGrade(grade);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return stu;
}
@Override
public List queryStudentByGrade(int gradeid) {
String sql="select students.*,grade.gradeName from students inner join grade on students.gradeid=grade.gradeId where students.gradeid=?";
List list=new ArrayList();
try {
ResultSet rs=super.executeQuery(sql,gradeid);
while(rs.next()) {
Students stu=this.parseResultSet(rs);
list.add(stu);
list.add(stu);
}
return list;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//模糊查询
@Override
public List queryStudentByName(String stuname) {
String sql="select students.*,grade.gradeName from students inner join grade on students.gradeid=grade.gradeId where students.stuname=?";
List list=new ArrayList();
ResultSet rs=super.executeQuery(sql,stuname);
try {
//将游标的一行数据转换成学生对象
while(rs.next()) {
Students stu=this.parseResultSet(rs);
list.add(stu);
}
return list;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List queryAllStudent(){
String sql="select students.*,grade.gradename from students inner join grade on students.gradeid=grade.gradeId";
try {
ResultSet rs=super.executeQuery(sql);
List list=new ArrayList();
while(rs.next()) {
Students stu=this.parseResultSet(rs);
list.add(stu);
}
return list;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List queryAllStudentBykeyword(String stunameKeyword) {
String sql="select students.*,grade.gradeName from students inner join grade on students.gradeid=grade.gradeId where students.stuname like ?";
List list=new ArrayList();
ResultSet rs=super.executeQuery(sql,"%"+stunameKeyword+"%");
try {
while(rs.next()) {
Students stu=this.parseResultSet(rs);
list.add(stu);
}
return list;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
封装接口和实现类
封装对表的操作方法
业务逻辑层
封装对象的,业务方法,需要什么方法,封装什么方法
封装业务逻辑
执行多个sql语句才能完成的业务
转账、商品折扣等都属于业务
业务接口
cn.edu.xcu.sims.biz
StudentBiz StudentService StudentManager
StudentBiz
public interface StudentBiz {
public List getAllStudent();
public List getAllStudentByName(String stuname);
public Students getStudentById(int stuid);
public boolean addStudent(Students stu);
public boolean removeStudent(int stuid);
public boolean modifyStudent(int stuid);
public List getAllStudentByKeyword(String stunameKeyword);
}
业务实现类
cn.edu.xcu.sims.biz.impl
StudentBizImpl
封装业务方法
public class StudentBizImpl implements StudentBiz {
StudentDao dao=new StudentDaoImpl();
@Override
public List getAllStudent() {
List list=dao.queryAllStudent();
return list;
}
@Override
public List getAllStudentByName(String stuname) {
List list=dao.queryStudentByName(stuname);
return list;
}
//数据访问层不允许写输出,测试可以,不能显示在ui界面,可以抛出异常
//输入输出只存在于表示层
@Override
public boolean addStudent(Students stu) {
List list=dao.queryStudentByName(stu.getStuname());
boolean exists=false;
for(Students s:list) {
if(s.getStuname().equals(stu.getStuname())) {
exists=true;
break;
}
}
if(exists) {
throw new StudentNameExistsException(stu.getStuname()+"已经存在,不能添加");
}
else {
return dao.insertStudent(stu);
}
}
@Override
public boolean removeStudent(int stuid) {
return dao.deleteStudent(stuid);
}
@Override
public boolean modifyStudent(int stuid) {
Students stu=dao.findStudent(stuid);
if(stu==null) {
throw new RuntimeException("学号不存在");
}
else {
return dao.updateStudent(stu);
}
}
@Override
public Students getStudentById(int stuid) {
return dao.findStudent(stuid);
}
@Override
public List getAllStudentByKeyword(String stunameKeyword) {
List list=dao.queryAllStudentBykeyword(stunameKeyword);
return list;
}
表示层
程序运行出来可看到的部分
UI
包名 cn.edu.xcu.sims.web(网页)/ui(控制台)
cn.edu.xcu.sims.ui
Menu
public class Menu {
Scanner input=new Scanner(System.in);
public void MainMenu() {
System.out.println("***********************");
System.out.println("1--查询所有学生信息");
System.out.println("2--从姓名获得学生信息");
System.out.println("3--从学号获得学生信息");
System.out.println("4--添加学生信息");
System.out.println("5--删除学生信息");
System.out.println("6--修改学生信息");
System.out.println("7--输入姓名关键字模糊查询");
System.out.println("请选择你将要执行的业务:");
System.out.println("***********************");
switch(input.nextInt()) {
case 1:
showStudent();
break;
case 2:
showStudentByname();
break;
case 3:
showStudentById();
break;
case 4:
addStudent();
break;
case 5:
deleteStudent();
break;
case 6:
modifyStudent();
break;
case 7:
selectnameBykeyword();
break;
}
System.out.println("是否继续");
if(input.next().equalsIgnoreCase("y")) {
MainMenu();
}
else {
System.exit(0);
}
}
public void selectnameBykeyword() {
System.out.println("请输入要查询的学生姓名:");
StudentBiz biz=new StudentBizImpl();
List list=biz.getAllStudentByKeyword(input.next());
if(list.size()>0) {
System.out.println("学号\t姓名\t性别\t年级\t电话\t地址\t");
for(Students stu:list) {
System.out.println(stu.getStuid()+"\t"+stu.getStuname()+"\t"+stu.getGender()+"\t"+stu.getGrade().getGradeId()+"\t"+stu.getPhone()+"\t"+stu.getAddress());
}
}
else {
System.out.println("查无此人");
}
}
public void modifyStudent() {
try {
System.out.println("请输入学生学号:");
int stuid=input.nextInt();
StudentBiz biz=new StudentBizImpl();
if(biz.modifyStudent(stuid)) {
System.out.println("修改成功");
}
} catch (Exception e) {
System.out.println("程序故障,删除失败"+e.getMessage());
}
}
public void deleteStudent() {
System.out.println("请输入要删除的学生的学号:");
StudentBiz biz=new StudentBizImpl();
int stuid=input.nextInt();
Students stu=biz.getStudentById(stuid);
if(stu==null) {
System.out.println("查无此人");
}
else {
System.out.println("你确定要删除学号是"+stuid+" 姓名是"+stu.getStuname()+"的学生吗");
{
if(input.next().equalsIgnoreCase("y")) {
if(biz.removeStudent(stuid)) {
System.out.println("删除成功");
}
else {
System.out.println("删除失败");
}
}
}
}
}
public void showStudentById() {
System.out.println("请输入要查询的学生学号:");
StudentBiz biz=new StudentBizImpl();
Students stu=biz.getStudentById(input.nextInt());
if(stu!=null) {
System.out.println("学号\t姓名\t性别\t年级\t电话\t地址\t");
System.out.println(stu.getStuid()+"\t"+stu.getStuname()+"\t"+stu.getGender()+"\t"+stu.getGrade().getGradeId()+"\t"+stu.getPhone()+"\t"+stu.getAddress());
}
else {
System.out.println("查无此人");
}
}
public void showStudent() {
StudentBiz biz=new StudentBizImpl();
List list=biz.getAllStudent();
System.out.println("学号\t姓名\t性别\t年级\t电话\t地址\t");
for(Students stu:list) {
System.out.println(stu.getStuid()+"\t"+stu.getStuname()+"\t"+stu.getGender()+"\t"+stu.getGrade().getGradeId()+"\t"+stu.getPhone()+"\t"+stu.getAddress());
}
}
public void showStudentByname() {
System.out.println("请输入要查询的学生姓名:");
StudentBiz biz=new StudentBizImpl();
List list=biz.getAllStudentByName(input.next());
if(list.size()>0) {
System.out.println("学号\t姓名\t性别\t年级\t电话\t地址\t");
for(Students stu:list) { System.out.println(stu.getStuid()+"\t"+stu.getStuname()+"\t"+stu.getGender()+"\t"+stu.getGrade().getGradeId()+"\t"+stu.getPhone()+"\t"+stu.getAddress());
}
}
else {
System.out.println("查无此人");
}
}
public void addStudent() {
Students stu=new Students();
System.out.println("请输入学生姓名");
stu.setStuname(input.next());
System.out.println("请输入学生性别");
stu.setGender(input.next());
System.out.println("请输入学生年级编号");
Grade grade=new Grade();
grade.setGradeId(input.nextInt());
stu.setGrade(grade);
System.out.println("请输入学生电话");
stu.setPhone(input.next());
System.out.println("请输入学生地址");
stu.setAddress(input.next());
StudentBiz biz=new StudentBizImpl();
try {
biz.addStudent(stu);
System.out.println("添加成功");
} catch (StudentNameExistsException e) { System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println("添加失败"+e.getMessage());
}
}
Start
public class Start {
public static void main(String[] args) {
Menu menu=new Menu();
menu.MainMenu();
}
}
关系
表示层通过——业务逻辑层——数据访问层——访问数据库——封装成对象——返回业务逻辑层——表示层
实体类——封装数据 每层都要用
实体类
cn.edu.xcu.sims.entity
Students
public class Students implements Serializable{
private int stuid;
private String stuname;
private String gender;
private Grade grade;
private String phone;
private String address;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Students() {
super();
// TODO Auto-generated constructor stub
}
public Students(int stuid, String stuname, String gender, Grade grade, String phone, String address) {
super();
this.stuid = stuid;
this.stuname = stuname;
this.gender = gender;
this.grade = grade;
this.phone = phone;
this.address = address;
}
@Override
public String toString() {
return "Students [stuid=" + stuid + ", stuname=" + stuname + ", gender=" + gender + ", grade=" + grade
+ ", phone=" + phone + ", address=" + address + "]";
}
}
Grade
public class Grade {
private int gradeId;
private String gradeName;
public int getGradeId() {
return gradeId;
}
public void setGradeId(int gradeId) {
this.gradeId = gradeId;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
public Grade(int gradeId, String gradeName) {
super();
this.gradeId = gradeId;
this.gradeName = gradeName;
}
public Grade() {
super();
}
@Override
public String toString() {
return "Grade [gradeId=" + gradeId + ", gradeName=" + gradeName + "]";
}
}
异常
cn.edu.xcu.sims.exception
StudentNameExistsException
public class StudentNameExistsException extends RuntimeException{
public StudentNameExistsException(String e) {
super(e);
}
}