1.简单应用
1.1需要的库文件:
这些文件直接下载吧,如果找不到,运行的时候就会报错,再一条条添加也可以
2.配置文件
文件结构,里面的resource没用 这里的blog数据库很简单
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<!-- 持久化映射文件(将java对象映射到数据库表)-->
<hibernate-mapping>
<!--
name 指定需要持久化的类 (包 + 类名)
table 指定需要映射的表名
-->
<class name="Blog" table="blog">
<!--
id 必须指定
name 指定持久化类的持久化标识符的名称
-->
<id name="id">
<!-- generator指定id生成策略 -->
<generator class="native"/>
</id>
<!--
property其他属性映射配置
name 指定持久化中需要映射的属性名
column 指定表名的列名(不写标示默认使用属性名)
length 指定列的储蓄数据长度
type 指定列字段的类型
<property name="password" length="12"/>
-->
<property name="name" column="name"/>
</class>
</hibernate-mapping>
有个小坑,就是mysql
中的id要设置成自动增长的,否则可能报错: BUG:java.sql.SQLException: Field 'id' doesn't have a default value
另外一个配置文件 hibernate.cfg.xml
<!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>
<!-- 指定数据库方言,更好的操作目标数据库,因为数据库都有一些区别通过方言就可以对应使用目标数据库的特点
org.hibernate.dialect.MySQLInnoDBDialect MySQL数据库5.5之前使用
org.hibernate.dialect.MySQL5InnoDBDialect MySQL数据库5.5之后使用
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 数据库驱动 -->
<property name="hibernate.connection.driver.class">com.mysql.jdbc.Driver</property> <!-- 数据库url
jdbc:mysql://192.168.10.222:3306/test 指定连接的网络数据库
jdbc:mysql://localhost:3306/test 表示连接的是本机数据库
-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <!-- 数据库登录用户名的密码 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">0</property>
<!-- 数据库自动创建表操作
create-drop 在程序启动的时候创建数据库表,程序退出的时候删除之前创建的表(设计阶段)
create 在程序启动的时候删除上一次创建的数据库表,然后再创建新的表结构(设计阶段)
update 在程序启动的时候如果没有表就创建数据库表,有就检查有没有更新(推荐使用)
validate 在程序启动的时候检查表结构,不会创建
<property name="hibernate.hbm2ddl.auto">update</property>
-->
<!-- 显示执行的SQL语句 -->
<property name="show_sql">true</property>
<!-- 格式这行的SQL语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 引入需要持久化的持久化配置文件 -->
<!-- 这是在unix下的文件目录路径,windows系统是使用"\",应改为resource="com\person\Model\Person.hbm.xml" -->
<!--
<mapping resource="com/person/Model/Person.hbm.xml"/>
<mapping class="com.person.Model.Person"/>
-->
<mapping resource="Blog.hbm.xml"/>
<mapping class="Blog"/>
</session-factory>
</hibernate-configuration>
3.简单代码
Blog.java
import javax.persistence.*;
@Entity
@Table(name="blog")
public class Blog{
@Id
@GeneratedValue
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "id:"+id+" name:"+name;
}
}
BlogDao.java
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class BlogDao {
static Configuration config;
static SessionFactory factory;
static Session session;
static Transaction tx;
static Query query;
public static void addBlog(String name){
Blog person=new Blog();
person.setName(name);
openSession();
//将对象person持久化到数据库
session.save(person);
//提交事务
tx.commit();
closeSession();
}
public static void openSession(){
//获得hibernate.cfg.xml中的配置
config=new Configuration().configure();
//获得连接工厂
//对应一个数据库,通常一个应用程序只有一个,在启动的时候创建就可以了
factory=config.buildSessionFactory();
//获取session,session是Hibernate的核心API,其他的增删改查都是通过session完成的
//session相当于一次数据库表的操作
//非线程安全
session=factory.openSession();
//hibernate 中增删改查需要事务的支持
tx=session.beginTransaction();
}
public static void closeSession(){
//关闭session
session.close();
//关闭sessionFactory.通常不用手动关闭
factory.close();
}
}
测试:
public class BlogJdbcDemo {
public static void main(String[] args) {
BlogDao dao = new BlogDao();
dao.addBlog("xian");
}
}
hibernate.cfg.xml
默认放在src目录下,编译器会复制到生成的class下面