首先建立一个ormdemo命名为ormtest。然后新建lib文件夹导入相关jar包。
首先导入hibernate的jar包:
然后导入日志类包:
以及连接mysql的驱动jar包:
最终使用如下jar包:
然后创建一个Visitor类:
public class Visitor {
private Integer id;
private String name;
private Integer age;
private String sex;
private String message;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getmessage() {
return message;
}
public void setmessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Visitor [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", message=" + message + "]";
}
}
编写映射文件Visitor.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hck.entity.Visitor" table="Visitor">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="integer"/>
<property name="sex" column="sex" type="string"/>
<property name="message" column="message" type="string"/>
</class>
</hibernate-mapping>
编写核心配置文件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>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_blog</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">001122</property>
<property name="format_sql">true</property>
<mapping resource="com/hck/entity/visitor.hbm.xml"/>
</session-factory>
</hibernate-configuration>
最后编写单元测试类:
public class HibernateTestDemo {
Configuration config;
SessionFactory sessionFactory;
Session session;
Transaction transaction;
@Before
public void setUp()
{
config=new Configuration().configure();
sessionFactory=config.buildSessionFactory();
session=sessionFactory.openSession();
transaction=session.beginTransaction();
}
@Test
public void insert()
{
Visitor visitor=new Visitor();
visitor.setId(1);
visitor.setName("david");
visitor.setAge(18);
visitor.setSex("m");
visitor.setmessage("112233@163.com");
session.save(visitor);
}
@Test
public void delete()
{
Visitor visitor=(Visitor)session.get(Visitor.class, 1);
session.delete(visitor);
}
@Test
public void select()
{
Visitor visitor=(Visitor)session.get(Visitor.class, 1);
System.out.println(visitor);
}
@Test
public void update()
{
Visitor visitor=new Visitor();
visitor.setId(1);
visitor.setName("david");
visitor.setAge(18);
visitor.setSex("m");
visitor.setmessage("112233@163.com");
session.saveOrUpdate(visitor);
}
@After
public void closeTransaction()
{
transaction.commit();
session.close();
sessionFactory.close();
}
}
执行mysql操作如下: