肥猫拉夫尔
2017-10-22 13:25
//控制台信息
十月 22, 2017 1:07:00 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.1.Final}
十月 22, 2017 1:07:00 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十月 22, 2017 1:07:00 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十月 22, 2017 1:07:01 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
十月 22, 2017 1:07:01 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
//测试类代码
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//测试类
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
Configuration config=new Configuration().configure();
//创建配置对象
ServiceRegistry serviceRegistry=(ServiceRegistry) new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
//创建服务注册对象
sessionFactory =config.buildSessionFactory(serviceRegistry);
//创建会话工厂对象
session = sessionFactory.openSession();
//打开会话
transaction = session.beginTransaction();
//打开事务
}
@After
public void destory() {
transaction.commit();//提交事务
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
@Test
public void toSaveStudents() {
Students s = new Students(1, "张三丰", "男", new Date(), "武当山");
session.save(s);//保存进数据库
}
}
//配置类信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.passsword">root</property>
<property name="connection.driver_class">com.mysql.jdbc.driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
看junit有报错信息,如果你的数据库是5.x,那么方言就不对,要换成org.hibernate.dialect.MySQL5Dialect
Hibernate初探之单表映射
74810 学习 · 793 问题
相似问题