使用hibernate连接mysql数据库
1:项目搭建好之后,在lib包中添加必要的jar包,和mysql数据库驱动jar包:
jar包可以在hibernate的下载包(hibernate3.3.2.GA)中找到,这里所需要的jar包是:
hibernate3.jar,lib/required目录下的所有jar包;
连接数据库所需要的jar包:mysql-connector-java-5.1.7-bin.jar;
如图:
2:jar包引入后编写实体类及映射文件:
实体类是xx.java文件;映射文件为xx.hbm.xml文件,文件名称需要相同;
.java文件内容此处省略,
.hbm.xml文件测试代码:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.yim.entity">
<class name="user" table="user">
<id name="userId" column="USERID">
<generator class="uuid" />
</id>
<property name="userName" type="java.lang.String" column="USERNAME"
length="20" />
</class>
</hibernate-mapping>
3:添加hibernate.cfg.xml及hibernate配置文件
该文件在hibernate的下载包中可以找到模板:
配置文件模板:hibernate3.3.2.GA\project\tutorials\web\src\main\resources\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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/yim</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate‘s current session context -->
<property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- 映射文件声明 -->
<mapping resource="com/yim/entity/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
4:上面的步骤都配置后.可以进行数据库连接测试:
测试类代码:
package com.yim.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.yim.entity.user;
/**
* 测试mysql数据库连接
*
* @author Administrator
*
*/
public class loginDao {
private Session session = null;
private Transaction tran = null;
public loginDao() {
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
this.session = factory.openSession();
}
public void save(user user) {
try {
tran = this.session.beginTransaction();
this.session.save(user);
tran.commit();
System.out.println("信息保存");
} catch (Exception e) {
// TODO: handle exception
} finally {
this.session.close();
}
}
public static void main(String[] args) {
user user = new user();
user.setUserName("用户名称");
new loginDao().save(user);
}
}
Run as -->java application执行测试类就可以了;
当控制台输出所执行的sql语句:
Hibernate: insert into user (USERNAME, USERID) values (?, ?)
信息保存
表示数据库连接成功!