题目:

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()+"\t学生年级:"+student.getGradeId()+"\t年级名称:"+student.getGrade().getGradeName()); }*/
Grade grade = gradeService.getStudentByGrade("S1");
for (Student stu : grade.getStulist()) {
System.out.println(stu.getStudentName());
}
}
}

Dao的扩展的更多相关文章

  1. Spring DAO vs Spring ORM vs Spring JDBC

    Pat 的疑惑 最近关注于 Spring 提供的数据访问技术,对于 Spring 相关的这几个项目有何不同我不是太明白: Spring-DAO (http://docs.spring.io/sprin ...

  2. Spring Boot下如何自定义Repository中的DAO方法

    环境配置介绍 jdk 1.8, Spring Boot 1.5.3.RELEASE, Mysql, Spring Data, JPA 问题描述 Spring Data提供了一套简单易用的DAO层抽象与 ...

  3. 泛型T的类型获取

    T.getClass()或者T.class都是非法的,因为T是泛型变量. 由于一个类的类型是什么是在编译期处理的,故不能在运行时直接在Base里得到T的实际类型. /** * 可以在service层直 ...

  4. Spring事务的来龙去脉

    引言 Spring是一个IOC框架,在IOC框架的基础上,提供了DAO集成,AOP事务控制,JNDI等等一系列的高级功能,个人觉得,在Spring中最值得称道的不仅仅它是一个非入侵的IOC容器,而在于 ...

  5. Spring3之JDBC

    Spring提供了统一的数据访问异常层次体系,所涉及到的大部分异常类型都定义在org.springframework.dao包中,出于这个体系中所有异常类型均以org.springframework. ...

  6. MyBatis3整合Spring3、SpringMVC3

    开发环境: System:Windows WebBrowser:IE6+.Firefox3+ JavaEE Server:tomcat5.0.2.8.tomcat6 IDE:eclipse.MyEcl ...

  7. 走进JavaWeb技术世界1:JavaWeb的由来和基础知识

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  8. DAO层,Service层,Controller层、View层 的分工合作

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  9. 扩展 jquery miniui 组件实现自动查询数据

    主题 之前写过一篇文章分享了公司basecode查找数据后台是怎么实现的(http://www.cnblogs.com/abcwt112/p/6085335.html).今天想分享一下公司前台是怎么扩 ...

随机推荐

  1. python学习-41 装饰器 -- 高阶函数

    装饰器:本质就是函数.是为其他函数添加附加功能的. 原则:1.不修改被修饰函数的源代码2.不修改被修饰函数的调用方式 --- 装饰器的知识储备 装饰器=高阶函数+函数嵌套+闭包 高阶函数 1.高阶函数 ...

  2. hadoop 空间配置

    hadoop-------------- 分布式计算框架. common // hdfs //存储 mapreduce //MR,编程模型. yarn //资源调度. 集群部署----------- ...

  3. vue 等比例截图组件,支持缩放和旋转

    <template> <div class="crop-image" :style="wrapStyle"> <img :src= ...

  4. Mac下Sublime Text3激活码

    方法1: 终端中打开文件 /etc/hosts,插入如下语句 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 方法2: 在s ...

  5. springboot_1

    1. 创建一个spring boot项目可以使用哪些工具 1.1 使用start.spring.io 这是一个网站,可以在这个网站选择你需要的组件,然后会自动生成一个项目文件,你可以将它下载到本地,然 ...

  6. element-ui 页面刷新自动弹Message问题

    问题: 通过加载插件的方式引入Message,导致每次页面刷新的时候会自动弹出一个通知消息 该情况只在引入局部插件才会引起 import Vue from 'vue' import 'element- ...

  7. KaiWu 的体验

    KaiWu 的体验 来源 https://www.zhihu.com/question/28950444 >>>>>>>>>>>> ...

  8. sql语句中包含引号处理方法

    1. 背景 在使用Python脚本向数据库导入日志文件时候,突然报错. 2. 解决思路 查看messages文件,发现有一条语句里包含单引号. 查看sql语句,是使用单引号标注str类型. 3. 得出 ...

  9. mac下卸载android studio

    Execute these commands from the terminal rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/P ...

  10. JavaScript,遍历,for

    (for循环,for...in ,for...of ,forEach)(:for in总是得到数组,字符串的下标,而for of和forEach一样,是直接得到值) (forEach() 方法用于调用 ...