One relationship
All student information query, information is displayed in grades Grade name
A, DAO layer structures:
1. entity classes:
Student categories:
package com.myschool.entity; import java.io.Serializable; import java.util.Date; public class Student implements Serializable { private static final long serialVersionUID = 6439763802252472361L; //定义实体属性 private int studentNo; private String longinPwd; private String studentName; private int sex; private grade grade;//把年级对象作为属性 private String phone; private String address; private Date bornDate; private String email; private String identityCard; //封装 public int getStudentNo() { return studentNo; } public void setStudentNo(int studentNo) { this.studentNo = studentNo; } public String getLonginPwd() { return longinPwd; } public void setLonginPwd(String longinPwd) { this.longinPwd = longinPwd; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public grade getGrade() { return grade; } public void setGrade(grade grades) { this.grade = grades; } 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 Date getBornDate() { return bornDate; } public void setBornDate(Date bornDate) { this.bornDate = bornDate; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIdentityCard() { return identityCard; } public void setIdentityCard(String identityCard) { this.identityCard = identityCard; } //有参构造 public Student(int studentNo, String longinPwd, String studentName, int sex, grade gradeID, String phone, String address, Date bornDate, String email, String identityCard) { this.studentNo = studentNo; this.longinPwd = longinPwd; this.studentName = studentName; this.sex = sex; this.gradeID = gradeID; this.phone = phone; this.address = address; this.bornDate = bornDate; this.email = email; = identityCard this.identityCard; } // constructor with no arguments public Student () { }
Grade categories:
package com.myschool.entity; 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(); } }
Corresponding to a year as a student, but not in the table relates to the student grades names, grades encapsulated objects directly to the student attributes
2.Daobase: database access classes connection
package com.myschool.dao; import java.beans.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class baseDao { // 创建连接参数 private final static String DRIVER = "com.mysql.jdbc.Driver"; private final static String URL = "jdbc:mysql:///myschool"; private final static String USER_NAME = "root"; private final static String PASSWORD = "123"; Connection con = null; PreparedStatement prestatement = null; ResultSet rs = null; // 获取连接 private Connection getConnection() { try { Class.forName(DRIVER); if (con == null) { con = DriverManager.getConnection(URL, USER_NAME, PASSWORD); } } catch (Exception e) { e.printStackTrace(); } returnCON; } // CRUD public int the executeUpdate (String SQL, ... Object obj) throws Exception { // Get connecting the getConnection (); // Get the object prepareStatement prestatement = con.prepareStatement (SQL); // cycle add parameters for ( int I =. 1; I <= obj.length; I ++ ) { prestatement.setObject (I, obj [I -. 1 ]); } // execute SQL statements int COUNT = prestatement.executeUpdate (); return COUNT; } //Check public the ResultSet the executeQuery (String SQL, ... Object obj) throws Exception { // Get connecting the getConnection (); // Get the object prepareStatement prestatement = con.prepareStatement (SQL); // cycle parameters added for ( int I =. 1 ; I ++; <= obj.length I ) { prestatement.setObject (I, obj [I -. 1 ]); } // execute SQL statements RS = prestatement.executeQuery (); return RS; } // resource recovery public void closeResouse ()throws Exception { if (rs!=null) { rs.close(); } if (prestatement!=null) { prestatement.close(); } if (con!=null) { con.close(); } } }
3.DAO Interface:
Package Penalty for com.myschool.dao; Import java.util.List; Import com.myschool.entity.Student; public interface IStudentDao { / * * Query all student records, grades name * / public List <Student> Search () throws Exception; }
4.DAO interface categories:
package com.mychool.dao.impl; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import com.myschool.dao.IStudentDao; import com.myschool.dao.baseDao; import com.myschool.entity.Student; import com.myschool.entity.grade; public class IStudentDaoImpl extends baseDao implements IStudentDao { @Override public List<Student> Search() throws Exception { //创建student对象泛型集合 List <Student> = stus new new the ArrayList <Student> (); String SQL = "studentName the Select, from Student GradeName, WHERE Grade.gradeid = Student.gradeid Grade" ;
// definition of the object receiving resultSet basedao query methods found out data rSet ResultSet = executeQuery (SQL); IF (! rSet = null ) { the while (rSet.next ()) { Student STU = new new Student (); Grade Grade = new new Grade (); // create an object grades // STU. setAddress (rSet.getString ( "address") );// stu.setEmail(rSet.getString("email")); // stu.setIdentityCard(rSet.getString("identityCard")); // stu.setLonginPwd(rSet.getString("longinPwd")); // stu.setPhone(rSet.getString("phone")); // stu.setSex(rSet.getInt("sex")); grade.setGradeName(rSet.getString("gradeName"));//给年级对象赋值 stu.setStudentName(rSet.getString("studentName"));//给学生对象赋值 stu.setGradeID(grade); // stu.setStudentNo(rSet.getInt("StudentNo")); // = New new sdFormat the SimpleDateFormat the SimpleDateFormat ( "the MM-dd-YYYY"); // stu.setBornDate (sdFormat.parse (rSet.getString ( "bornDate"))); stus.add (STU); // add objects to the student the object set } }
// release resources recycling closeResouse () ; return stus; } }
to clean clear and simple, not part of the attribute value query
Two, service layer:
There is no logic what, I'll just quote the dao method again, not to explain
1.service Interface:
package com.myschool.service; import java.util.List; import com.myschool.entity.Student; public interface IService { public List<Student> Search() throws Exception; }
2.service interface categories:
package com.mychool.service.Impl; import java.util.List; import com.mychool.dao.impl.IStudentDaoImpl; import com.myschool.dao.IStudentDao; import com.myschool.entity.Student; import com.myschool.service.IService; public class IServiceImpl implements IService{ IStudentDao isd=new IStudentDaoImpl(); @Override public List<Student> Search() throws Exception { return isd.Search(); } }
Three, UI layer:
package com.myschool.ui; import java.util.List; import com.mychool.service.Impl.IServiceImpl; import com.myschool.entity.Student; import com.myschool.service.IService; public class test { public static void main(String[] args) throws Exception {
//创建service接口实现类的对象 IService isv=new IServiceImpl(); List<Student> lsList=isv.Search();
for (Student student : lsList) { System.out.println(student.getStudentName()+"\t"+student.getGradeID().getGradeName()); } } }
Many relationship
The main difference here is the implementation class package and dao wording:
Since a plurality of grades corresponding to students, the student package a collection of generic objects in the class grade.
DAO implementation class writing: