标签:服务 主键 upd nbsp settings 添加 org registry print
显示学生资料——准备
1.显示学生资料
2.删除学生资料
3.学生主键生成策略
4.添加学生资料
5.修改学生资料
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 }
标签:服务 主键 upd nbsp settings 添加 org registry print
原文地址:https://www.cnblogs.com/songsongblue/p/9532894.html