多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 增 ~~~ //加载数据库核心配置文件(引入映射文件) Configuration configuration = new Configuration(); configuration.configure(); //获取sessionFactory工厂,相当于连接池 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session,相当于connection Session session = sessionFactory.openSession(); //开启事务(hibernate必须要开启事务) Transaction tx = session.beginTransaction(); //操作 Customer customer = new Customer(); customer.setCust_name("jack"); session.save(customer); //提交事务 tx.commit(); //关闭连接 session.close(); //关闭连接池 sessionFactory.close(); ~~~ ## 删 先查再删. ~~~ Customer customer = session.load(Customer.class, 1L); session.delete(customer); ~~~ ## 改 改之前要先查. ~~~ Customer customer = session.get(Customer.class, 1L); customer.setCust_name("米兰"); session.save(customer); ~~~ ## 查 ### get 1. 方式的SQL语句立刻执行. 2. get查询返回的是Customer类型. 3. get查询不到返回null. ### load 1. load方式是等用到查询的内容的时候才去执行.效率上load更高. 2. load查询返回的是Customer代理类型. 3. load查询不到返回报错信息. ~~~ Customer customer = session.get(Customer.class, 1L); //get方式 Customer customer = session.load(Customer.class, 1L); //load方式 System.out.println(customer); ~~~