package Tst;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import top.dragong.HibernateUtil;
import top.dragong.entity.User;

public class TestAddUser {
	public static void main(String[] args) throws IllegalStateException, SystemException {
		try {
			new TestAddUser().addUser();
		} catch (SecurityException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	private void addUser() throws IllegalStateException, SystemException, SecurityException, RollbackException, HeuristicMixedException, HeuristicRollbackException
	{
		//第一步 加載hibernate核心配置文件
		Configuration config=new Configuration();
		config.configure();
		//第二步 創建Sessionfactory对象
		SessionFactory sessionFactory=config.buildSessionFactory();
		//第三步 使用Sessionfactory创建Session对象
		Session session=sessionFactory.openSession();
		//第四步 开启事务
		org.hibernate.Transaction tx= session.beginTransaction();
		//第五步 写具体逻辑crud
		User user=new User();
		user.setUsername("admm");
		user.setPassword("250");
		user.setName("chinan");
		session.save(user);
		//第六步 提交事务
		tx.commit();
		//第七步  关闭资源
		session.close();
		sessionFactory.close();
	}
	private void getbuid(int i)
	{
		Session session=HibernateUtil.getSession();
		User user=(User)session.get(User.class,i);
		System.out.println(user.getName()+user.getUsername());
		HibernateUtil.closeSession();
				
	}
}
最近下载更多
13043860zj  LV16 2020年8月20日
段朝洪  LV15 2019年11月25日
caozhou  LV14 2019年3月11日
我的名字是凑得  LV8 2018年12月23日
wangmeng20  LV14 2018年11月1日
z980693651  LV10 2018年10月20日
wlzyf冰冰  LV13 2018年7月5日
gongjunjienb  LV15 2018年6月2日
mkl_mzq  LV10 2018年5月16日
812992358  LV11 2018年5月8日
最近浏览更多
CCgGYx 2023年5月28日
暂无贡献等级
cqbscxh  LV11 2022年9月19日
deeedede 2022年5月24日
暂无贡献等级
15116483404  LV2 2021年12月13日
cloisonne  LV2 2021年12月12日
dafqrf  LV1 2021年12月8日
好的好的  LV8 2021年6月22日
zhaolijie  LV2 2021年6月13日
and123456  LV11 2021年4月13日
ayanamirei  LV3 2021年4月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友