配置文件不知道怎么回事老不对,然后网上复制了一个 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory > <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">实体类 package com.isvi.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="test") public class LocalUser { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private Integer loginId; private String userName; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getLoginId() { return loginId; } public void setLoginId(Integer loginId) { this.loginId = loginId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } /** * @param args */ public static void main(String[] args) { } } package com.isvi.utils; import java.util.logging.Logger; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.isvi.entity.LocalUser; public class HbnSessionFactory { private static Logger logger=Logger.getLogger(HbnSessionFactory.class.getName()); private static SessionFactory sf; static{ try{ sf = new AnnotationConfiguration().configure(). buildSessionFactory(); }catch(Throwable tr){ logger.warning("得到SessionFactory失败"+tr); } } public static Session getCurrentSession(){ return sf.getCurrentSession(); } public static void closeSession(){ Session session=getCurrentSession(); if(session!=null){ session.close(); } } public static void main(String[] args) throws Exception { Session session=HbnSessionFactory.getCurrentSession(); session.beginTransaction(); LocalUser lu=new LocalUser(); lu.setLoginId(1); lu.setUserName("helloWorld"); session.save(lu); session.getTransaction().commit(); } } </property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/gggtest</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create-drop</property> <property name="show_sql">true</property> <property name="current_session_context_class">thread</property> <mapping class="com.isvi.entity.LocalUser"/> </session-factory> </hibernate-configuration>