Eclipse、Hibernate、mysql安装配置及例子

来源:互联网 发布:淘宝开放平台sdk下载 编辑:程序博客网 时间:2024/06/07 19:26

各个软件可在各自的官网中寻找,这里为了方便放上自己百度云的链接,

在学习hibernate的过程中,发现因为各种软件版本的问题,一直都很难找到一个完全符合自己的教程,

最后在查看多个教程之后,本人在win8.1上配置运行成功,

给自己也给别人留下一个参考,这里给出本人参考较多的一个教程:http://blog.csdn.net/aboy123/article/details/10085635 ,

感谢这位博主的分享,让我少走许多弯路@aboy123 ,这位博主的教程中的版本与这里的并不一样,本人在配置的时候基本采用的是新的版本了,

并且对其中的一些代码进行了修改,

另:在下面hibernate的压缩包中有hibernate的一些参考代码和实例(遇到问题可参考里面的写法)


eclipse版本:eclipse-jee-mars-R-win32-x86_64(http://pan.baidu.com/s/1hqiyahe)

hibernate版本:hibernate-release-4.3.10.Final (http://pan.baidu.com/s/1qWDVL5U)

mysql版本:mysql-installer-community-5.6.26.0 (http://pan.baidu.com/s/1lYaJW)

mysql的jdbc驱动版本:mysql-connector-java-5.1.36(http://pan.baidu.com/s/15AQZW)


1、先将上述软件下载,解压,只有mysql需要点击安装


2、运行eclipse,建立Java Project,将hibernate lib/required目录下所有的jar包和mysql的jdbc驱动中的mysql-connector-java-5.1.36-bin.jar 加入lib目录


3、编写hibernate.cfg.xml文件,将该文件放置到src目录下,hibernate.cfg.xml内容如下:


<?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>
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</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="com/example/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


这里数据库的登陆用户名和密码为自己mysql的用户名和密码,本人的用户名root,无密码,在运行程序之前要保证mysql中已经创建了hibernate_first这一数据库


4、在src添加包,本人添加的包名为:com.example.hibernate.(注意与代码应用一致即可)


5、在com.example.hibernate中添加User类,User类代码如下:


package com.example.hibernate;

import java.util.Date;

public class User {
private String id;
private String username;
private String password;
private Date createTime;
private Date expireTime;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String userName) {
this.username = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}


6、编写User.hbm.xml,完成实体类映射,将其放置到com.example.hibernate中,即与User类同一目录下,User.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>
<class name="com.example.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>

其中的property标签是将要生成是数据库表中的字段,在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型,可以要可以注意到,这里User.hbm.xml的位置与第3个步骤中hibernate.cfg.xml文件中的<mapping resource="com/example/hibernate/User.hbm.xml"/>是要保持一致的。


7、生成表:编写工具类ExoprtDB.java,将hbm生成ddl(与User类同一目录),ExoprtDB.java内容如下:


package com.example.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExoprtDB {


public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfr = new Configuration().configure();

SchemaExport export = new SchemaExport(cfr);
export.create(true, true);
}
}


8、到这里就可以生成User表了,但是如果直接运行ExoprtDB.java文件是不能生成User表的。因为在mysql数据中还没有建立数据库Hibernate-first。所以在mysql控制台中通过create database hibernate-first; use hibernate-first;之后再执行ExoprtDB.java文件就可以生成表了。


9、最后是向表中添加数据的例子,编写Client类(与User类同一目录),Client.java文件内容如下:

package com.example.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Client {
public static void main(String[] args) {
//读取配置文件
Configuration cfg = new Configuration().configure();

SessionFactory factory = cfg.buildSessionFactory();

Session session = null;
try{
session = factory.openSession();
//开启事务
session.beginTransaction();

User user = new User();
user.setUsername("username");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());

session.save(user);
//提交事务
session.getTransaction().commit();

}catch(Exception e){
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
}


1 0
原创粉丝点击