1.加载hibernate的核心配置文件

2.创建SessionFactory对象

3.使用SessionFactory创建Session对象

4.开启事务(手动开启)

5.写具体逻辑crud,增删改查操作

6.提交事物

7.关闭资源

代码如下:

 package com.hui.hibernate;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; public class HibernateDemo01 { public static void main(String[] args) {
add(); } @SuppressWarnings("deprecation")
@Test
public static void add() {
// 1.加载hibernate的核心配置文件
Configuration con = new Configuration();//加载配置文件的对象,在src下找到hibernate.cfg.xml文件放到对象中
con.configure();//开始加载
//
// 2.创建SessionFactory对象,读取核心配置文件内容创建SessionFactory,根据映射关系创建表
SessionFactory sf = con.buildSessionFactory();
// 3.使用SessionFactory创建Session对象
//类似于连接
Session se = sf.openSession();
// 4.开启事务(手动开启)
Transaction tr = se.beginTransaction();
// 5.写具体逻辑crud,增删改查操作,操作实体类对象
User u = new User();//uid不用设置值,他为自增长的主键
u.setUsername("刘冬冬");
u.setPassword("123456");
u.setAddress("中国陕西");
//调用Session方法save完成操作
se.save(u); // 6.提交事物
tr.commit();
// 7.关闭资源
se.close();
sf.close(); } }

8.测试,运行报错

报的错误如下

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:375)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:304)
at org.hibernate.cfg.Configuration.add(Configuration.java:518)
at org.hibernate.cfg.Configuration.add(Configuration.java:514)
at org.hibernate.cfg.Configuration.add(Configuration.java:688)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:771)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2257)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2229)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2209)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2162)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at com.hui.hibernate.HibernateDemo01.add(HibernateDemo01.java:21)
at com.hui.hibernate.HibernateDemo01.main(HibernateDemo01.java:12)
Caused by: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 40; 必须为元素类型 "id" 声明属性 "colnum"。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1917)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:742)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2787)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:325)
... 13 more

原因:如下图红线部分column写成了colnum,所以hibernate不能识别了,因而报错改回来就成了

hibernate操作步骤(代码部分)-LMLPHP

9.改后测试结果如下:创建了表t_user而且插入了一条数据

hibernate操作步骤(代码部分)-LMLPHP

hibernate操作步骤(代码部分)-LMLPHP

04-20 13:31