第三章:学生管理模块

显示学生资料——准备

1.显示学生资料

2.删除学生资料

3.学生主键生成策略

4.添加学生资料

5.修改学生资料

 

1.显示学生资料

  • 添加测试数据
     1 @Test
     2     public void testSaveStudents()
     3     {
     4         //创建配置对象
     5         Configuration config=new Configuration().configure();
     6         //创建服务注册对象
     7         ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
     8         //创建sessionFactory
     9         SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
    10         //创建session对象
    11         Session session = sessionFactory.getCurrentSession();
    12         //创建事务对象
    13         Transaction tx = session.beginTransaction();
    14         
    15         Students s1 = new Students("s0000001","张三丰","男",new Date(),"武当山");
    16         Students s2 = new Students("s0000002","郭靖","男",new Date(),"桃花岛");
    17         Students s3 = new Students("s0000003","黄蓉","女",new Date(),"桃花岛");
    18         
    19         session.save(s1);
    20         session.save(s2);
    21         session.save(s3);
    22         
    23         tx.commit();
    24         sessionFactory.close();
    25     }
  •  设计学生业务逻辑接口
 1 package service;
 2 
 3 import java.util.List;
 4 
 5 import entity.Students;
 6 
 7 //学生的业务逻辑接口
 8 public interface StudentsDAO {
 9     
10     //查询所有学生资料
11     public List<Students> queryAllStudents();
12     
13     //根据学生编号查询学生资料
14     public Students queryStudentsBySid(String sid);
15     
16     //添加学生资料
17     public boolean addStudents(Students s);
18     
19     //修改学生资料
20     public boolean updateStudents(Students s);
21     
22     //删除学生资料
23     public boolean deleteStudents(String sid);
24 
25 }
  • 设计学生业务逻辑接口实现类
 1 package service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.Test;
 6 
 7 import entity.Students;
 8 
 9 import service.StudentsDAO;
10 
11 public class TestStudentsDAOImpl {
12 
13     @Test
14     public void testQueryAllStudents()
15     {
16         StudentsDAO sdao = new StudentsDAOImpl();//接口类型的引用
17         List<Students> list=sdao.queryAllStudents();
18     
19         for(int i=0;i<list.size();i++)
20         {
21             System.out.println(list.get(i));
22         }
23     }
24 }

 

  • 设计学生Action类:实现显示学生资料Action
     1 package action;
     2 
     3 import java.util.List;
     4 
     5 import entity.Students;
     6 
     7 import service.StudentsDAO;
     8 import service.impl.StudentsDAOImpl;
     9 
    10 //学生Action类
    11 public class StudentsAction extends SuperAction{
    12 
    13     /**
    14      * 
    15      */
    16     private static final long serialVersionUID = 1L;
    17     
    18     //查询所有学生的动作
    19     public String query(){
    20         StudentsDAO sdao = new StudentsDAOImpl();
    21         List<Students> list=sdao.queryAllStudents();
    22         //放进session中
    23         if(list!=null&&list.size()>0)//找到学生记录
    24         {
    25             session.setAttribute("students_list", list);
    26         }
    27         return "query_success";//返回视图
    28     }
    29 }
  • 显示学生资料
    • 页面调用
  • 显示数据

2.删除学生资料

  • 界面调用
  • 编写业务逻辑代码
     1 public boolean deleteStudents(String sid) {
     2         // TODO Auto-generated method stub
     3         Transaction tx=null;//生成事务对象
     4         //String hql="";//执行的hql语句
     5         try
     6         {
     7             Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();//获得会话
     8             tx = session.beginTransaction();//开启事务
     9             Students s = (Students)session.get(Students.class, sid);//获得学生对象
    10             session.delete(s);//删除这个学生
    11             tx.commit();//提交事务
    12             return true;
    13         }
    14         catch(Exception ex)
    15         {
    16             ex.printStackTrace();
    17             tx.commit();
    18             return false;
    19         }
    20         finally
    21         {
    22             if(tx!=null){
    23                 tx=null;
    24             }
    25         }
    26     }

     

  • 编写删除action
    1 //删除学生动作
    2     public String delete()
    3     {
    4         StudentsDAO sdao = new StudentsDAOImpl();
    5         String sid = request.getParameter("sid");
    6         sdao.deleteStudents(sid);//调用删除方法
    7         return "delete_success";
    8     }

     

  • 测试

3.学生主键生成策略

4.添加学生资料

  • 界面原型演示
  • 编写添加学生业务逻辑代码
  • 编写添加action
  • 页面调用

添加学生资料

  • 学生的主键生成策略
     1 //生成学生的学号
     2     private String getNewSid()
     3     {
     4         Transaction tx=null;//生成事务对象
     5         String hql="";
     6         String sid = null;//返回学生编号
     7         try
     8         {
     9             Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();//获得会话
    10             tx = session.beginTransaction();//开启事务
    11             //获得当前学生的最大编号   
    12             hql="select max(sid) from Students";
    13             Query query=session.createQuery(hql);
    14             sid = (String)query.uniqueResult();
    15             if(sid==null||"".equals(sid))
    16             {
    17                 //给一个默认的最大编号
    18                 sid="s0000001";
    19             }
    20             else
    21             {
    22                 String temp = sid.substring(1);//去掉第一个字母s,只取后面的七位
    23                 int i=Integer.parseInt(temp);//转成数字
    24                 i++;
    25                 //再还原为字符串
    26                 temp=String.valueOf(i);
    27                 int len=temp.length();
    28                 //凑够七位
    29                 for (int j = 0; j <7-len; j++) 
    30                 {
    31                     temp="0"+temp;
    32                 }
    33                 sid = "S"+temp;
    34             }
    35             return sid;
    36         }
    37         catch(Exception ex)
    38         {
    39             ex.printStackTrace();
    40             return null;
    41         }
    42         finally
    43         {
    44             //关闭事务,把事务的引用置成空
    45             if(tx!=null){
    46                 tx=null;
    47             }
    48         }
    49     }

     

  • 编写添加学生业务逻辑代码
public boolean addStudents(Students s) {
        // TODO Auto-generated method stub
        s.setSid(getNewSid());//设置学生学号
        Transaction tx=null;//生成事务对象
        try
        {
            Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();//获得会话
            tx = session.beginTransaction();//开启事务
            session.save(s);
            tx.commit();
            return true;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        finally
        {
            //把事务置成空
            if(tx!=null){
                tx=null;
            }
        }        
    }

 

5.修改学生资料

  • 界面原型演示
  • 编写修改学生业务逻辑代码
    页面显示学生资料
    保存修改后的学生资料
  • 编写修改action
  • 页面调用
posted on 2018-08-25 08:54  songsong_p_blue  阅读(454)  评论(0编辑  收藏  举报