六祎-在测试中添加测试数据(不在mysql可视化工具添加数据)
//添加测试数据
@Test
public void testSaveStudents() {
// 创建一个配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
// ServiceRegistry serviceRegistry = new
// StandardServiceRegistryBuilder().applySetting(null,
// config.getProperties()).getBootstrapServiceRegistry();
// ServiceRegistry serviceRegistry = new
// StandardServiceRegistryBuilder().configure().build();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
.buildServiceRegistry();
// 创建sessionFactory
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
// 创建sessio对象
Session session = sessionFactory.getCurrentSession();
//创建事务
Transaction tx = session.beginTransaction();
Students s1 = new Students("$0000001","张三丰","男",new Date(),"武当山");
Students s2 = new Students("$0000002","郭靖","男",new Date(),"桃花岛");
Students s3 = new Students("$0000003","黄蓉","男",new Date(),"桃花岛");
session.save(s1);
session.save(s2);
session.save(s3);
tx.commit();
sessionFactory.close();
}