导入jar包就不说了,但不可不做。
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">
mysql-hibernate-demo
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/uf_demo_db
</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping resource="Book.hbm.xml" />
</session-factory>
</hibernate-configuration>
Book.hbm.xml的配置:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Book" table="book" catalog="uf_demo_db">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true"
unique="true" />
</property>
<property name="author" type="java.lang.String">
<column name="author" length="50" />
</property>
<property name="press" type="java.lang.String">
<column name="press" length="50" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="15" />
</property>
</class>
</hibernate-mapping>
Book.java
public class Book implements java.io.Serializable {
private Integer id;
private String name;
private String author;
private String press;
private Double price;
public Book() {
}
public Book(String name) {
this.name = name;
}
public Book(String name, String author, String press, Double price) {
this.name = name;
this.author = author;
this.press = press;
this.price = price;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return this.press;
}
public void setPress(String press) {
this.press = press;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
}
TestOperateDb.java(使用)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestOperateDb {
private SessionFactory factory;
public TestOperateDb() {
Configuration config = new Configuration().configure();
factory = config.buildSessionFactory();
}
public void add() {
Book book = new Book();
book.setName("java Basic");
book.setAuthor("liming");
book.setPress("Press");
book.setPrice(30.00);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
session.save(book);
tx.commit();
session.close();
System.out.println("Add successfully.");
}
public static void main(String[] args) {
TestOperateDb test = new TestOperateDb();
test.add();
}
}
数据库字段:id name author press price