INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)的解决

今天按照学习视频学习了Hibernate的入门课程:

前提:用的mysql数据库

前提:导入了hibernate的jar包和JUnit4.0的jar包,还有mysql的jdbc驱动jar包,就是这货


1 创建Hibernate的配置文件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>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">jdbc.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>
        <!--         com.hibernate.po. -->
        <mapping resource="com/hibernate/po/Students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


2.创建持久化类:

/**
 * 学生类
 */
package com.hibernate.po;

import java.util.Date;

/**
 * @author admin
 *
 */
public class Students {

	/**
	 * 学号
	 */
	private int sId ;
	
	/**
	 * 学生姓名
	 */
	private String name ;
	
	/**
	 * 学生性别
	 */
	private String gender ;
	
	/**
	 * 学生生日
	 */
	private Date birthday ;
	
	/**
	 * 居住地址
	 */
	private String address ;

	public Students() {
		
	}
	
	public Students(int sId, String name, String gender, Date birthday,
			String address) {
		super();
		this.sId = sId;
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
		this.address = address;
	}


	public int getsId() {
		return sId;
	}

	public void setsId(int sId) {
		this.sId = sId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Students [sId=" + sId + ", name=" + name + ", gender=" + gender
				+ ", birthday=" + birthday + ", address=" + address + "]";
	}
	
}
3.创建对象-关系映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-11-21 18:17:10 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.hibernate.po.Students" table="STUDENTS">
        <id name="sId" type="int" access="field">
            <column name="SID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>

当然,这个步骤是eclipse中的Hibernate插件自动生成的:

具体如下图:


4.用API编写访问数据库的代码:

package hibernatePractice;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.hibernate.po.Students;

public class StudentsTest {
	
	private SessionFactory sessionFactory ;
	private Session session ;
	private Transaction transaction;

	@Before
	public void init()
	{
		//创建配置对象
		Configuration config = new Configuration().configure();
		//创建服务注册对象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
	    //创建会话工厂对象
		sessionFactory = config.buildSessionFactory(serviceRegistry);
		//会话对象
		session = sessionFactory.openSession();
		//开启事务
		transaction = session.beginTransaction();
		
	}
	
	@After
	public void destroy()
	{
		transaction.commit();//提交事务
		session.close();//关闭会话
		sessionFactory.close(); //关闭会话工厂
	}
	
	
	@Test
	public void testSaveStudents()
	{
		Students students = new Students(001,"道明寺","男",new Date(),"北京");
		session.save(students);//保存对象进入数据库
	}
	
}

结果用JUnit测试,是这个样子的:

为啥这样了呢?原因:

还记得那货吗、?主要是hibernate.cfg.xml这个文件不对,

<property name="connection.driver_class">jdbc.mysql.jdbc.Driver</property>这句的问题,应该是com.mysql.jdbc.Driver


另外:hibernate配置文件的用户名密码不要写错,写错了会:

十一月 21, 2015 10:06:45 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions

ERROR: Access denied for user 'root'@'localhost' (using password: YES)



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值