(1)配置hebernate.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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/heiniao</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- SQL 方言 不同的数据库及版本 使用不同的方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- 是否打印sql -->
<property name="show_sql">true</property>
<!-- 自动生成数据表策略 update为根据实体类生成数据库表-->
<property name="hbm2ddl.auto">update</property>
<!--指定实体类的配置文件 根据项目中实际路径配置-->
<mapping resource="com/heiniao/Model/UserModel.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2)对应数据库表的实体类 (get set 方法省掉)
public class UserModel {
private int id;
private int userCode ;
private String userName ;
private String password ;
private String userType ;
private Date inputDate ;
private String valid ;
}
(3)配置实体类的映射文件 dynamic-update="true" 属性 为更新时只更新值修改的字段 <generator class="native" /> 为id生成策略为令数据库自己生成
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-5-16 23:16:22 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.heiniao.Model.UserModel" table="USER" dynamic-update="true">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="userCode" type="int">
<column name="USERCODE" />
</property>
<property name="userName" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="userType" type="java.lang.String">
<column name="USERTYPE" />
</property>
<property name="inputDate" type="java.util.Date">
<column name="INPUTDATE" />
</property>
<property name="valid" type="java.lang.String">
<column name="VALID" />
</property>
</class>
</hibernate-mapping>
(4) 执行操作
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session =sessionFactory.openSession();
session.beginTransaction();
UserModel user = new UserModel();
user.setUserCode(1803062220);
user.setUserName("DennisHu");
user.setPassword("22");
user.setUserType("1");
user.setInputDate(null);
user.setValid("1");
session.save(user);
session.getTransaction().commit();
sessionFactory.close();
}
(完成)
查询为 UserModel user = (UserModel)session.get(UserModel.class,1); 1为数据的id
更改实体类 user.setUserCode(9999);
session.save(user); hibernate自动发送Update语句
session.getTransaction().commit();