hibernate.cfg.xml配置
<?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>
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8</property>
<!-- 数据库的登陆用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登陆密码 -->
<property name="hibernate.connection.password"></property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="resources/mapperXML/user.xml"/>
</session-factory>
</hibernate-configuration>
mapper配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="practice.entity.User" table="User"> <id name="userid" column="UserId" type="string"> <generator class="uuid"/> </id> <property name="username" column="Username" type="string"/> <property name="birthday" column="Birthday" type="date"/> <property name="job" column="Job" type="string"/> <property name="age" column="Age" type="integer"/> </class> </hibernate-mapping>
---测试配置
public static void main(String[] args) {
//加载配置文件
Configuration cfg=new Configuration().configure();
//创建sessionFactroy
SessionFactory sessionFactory = cfg.buildSessionFactory();
//创建session
Session session=sessionFactory.openSession();
//开启事务
session.beginTransaction();
User user=new User();
user.setUsername("Test");
user.setAge(9);
user.setBirthday(new Date());
user.setJob("job");
session.save(user);
//提交事务
session.getTransaction().commit();
if(session.isOpen()){
session.close();
}
}
java常见的ClassNotFoundException
1 - java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 添加包common-logging.jar
2 - java.lang.ClassNotFoundException: javax.transaction.Synchronization 添加包jta.jar(hiberante)
3 - java.lang.ClassNotFoundException: org.dom4j.DocumentException 添加包dom4j-1.6.1.jar
4 - java.lang.ClassNotFoundException: org.apache.commons.collections.SequencedHashMap 添加包commons-collections.jar
5 - java.lang.ClassNotFoundException: net.sf.cglib.proxy.CallbackFilter java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter 添加包cglib-2.1.3.jar
6 - java.lang.ClassNotFoundException: org.objectweb.asm.Type 添加包asm.jar
7 - java.lang.ClassNotFoundException: antlr.ANTLRException 添加包antlr.jar
8 - java.lang.ClassNotFoundException: net.sf.ehcache.CacheException 添加包ehcache-1.2.3.jar
9-Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V 将cglib.jar 由2.2版本 改为 2.1.