zoukankan      html  css  js  c++  java
  • Dao的扩展

    题目:

    1.查询所有学生记录,包含年级名称
    2.查询S1年级下的学生记录

      一、项目目录

    package com.myschool.entity;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    /**
     * Grade实体类
     * @author FLC
     *
     */
    public class Grade {
        private int gradeId;
        private String gradeName;
        private List<Student> stulist=new ArrayList<Student>();
        public List<Student> getStulist() {
            return stulist;
        }
        public void setStulist(List<Student> stulist) {
            this.stulist = stulist;
        }
        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) {
            this.gradeId = gradeId;
            this.gradeName = gradeName;
        }
        public Grade() {
            
        }
    }
    package com.myschool.entity;
    /**
     * Student实体类
     * @author FLC
     *
     */
    public class Student {
        private int studentNo;
        private String studentName;
        private int gradeId;
    
        
        //只要拿到学生就能拿到对应学生的年级信息
        private Grade grade;
        
        public Grade getGrade() {
            return grade;
        }
        public void setGrade(Grade grade) {
            this.grade = grade;
        }
        public int getStudentNo() {
            return studentNo;
        }
        public void setStudentNo(int studentNo) {
            this.studentNo = studentNo;
        }
        public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        public int getGradeId() {
            return gradeId;
        }
        public void setGradeId(int gradeId) {
            this.gradeId = gradeId;
        }
        public Student(int studentNo, String studentName, int gradeId) {
            this.studentNo = studentNo;
            this.studentName = studentName;
            this.gradeId = gradeId;
        }
        public Student() {
            
        }
        
    }
    /**
         * 数据库连接字符串
         */
        private static final String DRIVER = "com.mysql.jdbc.Driver";
        private static final String URL = "jdbc:mysql://localhost:3306/myschool?useUniCode=true&characterEncoding=utf-8";
        private static final String USERNAME = "root";
        private static final String PASSWORD = "root";
    
        private Connection conn;
        private PreparedStatement statement;
        private ResultSet rs;
        /**
         * 获取连接的方法
         */
        public Connection getConnection() {
            try {
                Class.forName(DRIVER);
                //判断连接对象是否为空或者当前连接对象是否是isClosed()已经关闭的
                if(conn==null||conn.isClosed()){
                    conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                }
                
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
    
        }
    
        /**
         * 增删改
         * 
         * @throws Exception
         */
        public int executeUpdate(String sql, Object... obj) throws Exception {
            // 获取连接
            getConnection();
            // 获取PreparedStatement对象
            
            statement = conn.prepareStatement(sql);
            // 循环加载参数
            for (int i = 1; i <= obj.length; i++) {
                statement.setObject(i, obj[i-1]);
            }
            // 执行SQL
            int count = statement.executeUpdate();
    
            return count;
        }
    
        /**
         * 查询
         * @throws SQLException 
         */
        public ResultSet executeQuery(String sql, Object... obj) throws Exception {
            // 获取连接
            getConnection();
            // 获取PreparedStatement对象
            statement = conn.prepareStatement(sql);
            // 循环加载参数
            for (int i = 1; i <= obj.length; i++) {
                
                statement.setObject(i, obj[i-1]);
            }
            rs = statement.executeQuery();
            return rs;
        }
    
        /**
         * 关闭连接
         * @throws Exception 
         */
        public void closeResource() throws Exception {
            if(rs!=null){
                rs.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(conn!=null){
                //关闭连接
                conn.close();
            }
        }
    }
    package com.myschool.dao.impl;
    
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.myschool.dao.BaseDao;
    import com.myschool.dao.IGradeDao;
    import com.myschool.entity.Grade;
    import com.myschool.entity.Student;
    
    public class IGradeDaoImpl extends BaseDao implements IGradeDao{
    
        @Override
        public Grade getStudentByGrade(String gradeName) throws Exception {
            Grade grade=new Grade();
            String sql="SELECT * FROM Student,Grade WHERE Student.GradeId=Grade.GradeId AND GradeName=?";
            ResultSet rs = executeQuery(sql, gradeName);
            if(rs!=null){
                
                while (rs.next()) {
                    
                    //获取年级信息
                    grade.setGradeName(rs.getString("gradeName"));
                    //获取学生信息
                    Student student=new Student();
                    student.setGradeId(rs.getInt("gradeId"));
                    student.setStudentName(rs.getString("StudentName"));
                    student.setStudentNo(rs.getInt("studentNo"));
                    //将查询出来的学生信息添加到集合当中
                    grade.getStulist().add(student);
                }
                
            }
            return grade;
        }
    
    }
    package com.myschool.dao.impl;
    
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.myschool.dao.BaseDao;
    import com.myschool.dao.IStudentDao;
    import com.myschool.entity.Grade;
    import com.myschool.entity.Student;
    
    public class IStudentDaoImpl extends BaseDao implements IStudentDao{
    
        @Override
        public List<Student> getAllStudent() throws Exception {
            List<Student> list=new ArrayList<Student>();
            String sql="SELECT * FROM Student,Grade WHERE Student.GradeId=Grade.GradeId";
            ResultSet rs = executeQuery(sql);
            if(rs!=null){
                while(rs.next()){
                    //获取学生信息
                    Student student=new Student();
                    student.setGradeId(rs.getInt("gradeId"));
                    student.setStudentName(rs.getString("StudentName"));
                    student.setStudentNo(rs.getInt("studentNo"));
                    //获取的就是当前学生的年级信息
                    Grade grade=new Grade();
                    grade.setGradeName(rs.getString("gradeName"));
                    student.setGrade(grade);
                    
                    
                    list.add(student);
                }
            }
            closeResource();
            return list;
        }
        
        
    
    }
    package com.myschool.service;
    
    import com.myschool.entity.Grade;
    
    public interface IGradeService {
        /**
         * 查询年级下的所有学生信息
         * 一个年级有多名学生
         */
        public Grade getStudentByGrade(String gradeName) throws Exception;
    }
    package com.myschool.service;
    
    import java.util.List;
    
    import com.myschool.entity.Student;
    
    public interface IStudentService {
        /**
         * 查询所有学生信息,包含年级名称
         * 
         * 
         */
        public List<Student> getAllStudent() throws Exception;
    }
    package com.myschool.service.impl;
    
    import com.myschool.dao.IGradeDao;
    import com.myschool.dao.impl.IGradeDaoImpl;
    import com.myschool.entity.Grade;
    import com.myschool.service.IGradeService;
    
    public class IGradeServiceImpl implements IGradeService{
        
        IGradeDao gradeDao=new IGradeDaoImpl();
    
        @Override
        public Grade getStudentByGrade(String gradeName) throws Exception {
            // TODO Auto-generated method stub
            return gradeDao.getStudentByGrade(gradeName);
        }
    
    }
    package com.myschool.service.impl;
    
    import java.util.List;
    
    import com.myschool.dao.IStudentDao;
    import com.myschool.dao.impl.IStudentDaoImpl;
    import com.myschool.entity.Student;
    import com.myschool.service.IStudentService;
    
    public class IStudentServiceImpl implements IStudentService{
        //创建Dao层对象
        IStudentDao studentDao=new IStudentDaoImpl();
        
        
        @Override
        public List<Student> getAllStudent() throws Exception {
            return studentDao.getAllStudent();
        }
    
    }
    package com.myschool.ui;
    
    import java.util.List;
    
    import com.myschool.entity.Grade;
    import com.myschool.entity.Student;
    import com.myschool.service.IGradeService;
    import com.myschool.service.IStudentService;
    import com.myschool.service.impl.IGradeServiceImpl;
    import com.myschool.service.impl.IStudentServiceImpl;
    
    public class MyMian {
        //创建Service对象
        static IStudentService studentService=new IStudentServiceImpl();
        static IGradeService gradeService=new IGradeServiceImpl();
        public static void main(String[] args) throws Exception {
            /*System.out.println("=================查询所有学生信息========================");
            List<Student> allStudent = studentService.getAllStudent();
            for (Student student : allStudent) {
                System.out.println("学生姓名:"+student.getStudentName()+"	学生年级:"+student.getGradeId()+"	年级名称:"+student.getGrade().getGradeName());
                
            }*/
            Grade grade = gradeService.getStudentByGrade("S1");
            for (Student stu : grade.getStulist()) {
                System.out.println(stu.getStudentName());
            }
        }
    }
  • 相关阅读:
    [BZOJ4444][SCOI2015]国旗计划(倍增)
    [BZOJ4423][AMPPZ2013]Bytehattan(对偶图+并查集)
    [BZOJ4416][SHOI2013]阶乘字符串(子集DP)
    [BZOJ3203][SDOI2013]保护出题人(凸包+三分)
    [BZOJ4026]dC Loves Number Theory(线段树)
    51nod部分容斥题解
    [CodeVS4438]YJQ Runs Upstairs
    [HDU4906]Our happy ending
    牛客网NOIP赛前集训营-提高组(第四场)游记
    [BJWC2011]元素
  • 原文地址:https://www.cnblogs.com/ws1149939228/p/10945037.html
Copyright © 2011-2022 走看看