一 创建dao层
public class User { 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; } @Override public String toString() { return "[User:id="+id+",name"+name+"]"; } } |
二导入jar包

三配置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> <!-- 一、数据库信息:数据库方言(是一个类的全名)与数据库连接信息 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="connection.url">jdbc:mysql://192.168.10.227:3309/hibernate_20160815</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">000000</property>
<!-- 二、其他配置 --> <property name="show_sql">true</property> <property name="format_sql">false</property> <!-- create: 先删表,再建表。 create-drop: 启动时建表,退出前删表。 update: 如果表结构不一致,就创建或更新。 validate: 启动时验证表结构,如果不致就抛异常。 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 三、导入映射配置文件 --> <mapping resource="cn/itcast/a_helloword/User.hbm.xml"/> </session-factory> </hibernate-configuration> |
四映射配置文件
<?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="cn.itcast.a_helloword.User" table="user"> <id name="id" type="int" column="id"> <generator class="native"></generator> </id> <property name="name" type="string" column="name"></property> </class>
</hibernate-mapping> |
五 应用层
Java Code
package cn.itcast.a_helloword;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test;
public class App { public static SessionFactory sessionFactory; static { // 读取配置文件并生成Session工厂对象 Configuration cfg = new Configuration(); // cfg.configure("hibernate.cfg.xml"); // 加载指定的配置文件 // cfg.configure(); // 读取默认的配置文件(hibernate.cfg.xml) // cfg.addResource("cn/itcast/a_helloworld/User.hbm.xml"); // cfg.addClass(User.class); // // sessionFactory = cfg.buildSessionFactory(); sessionFactory = new Configuration()// .configure()// .addClass(User.class)// 加载指定类对应的映射文件(以类名为前缀,后缀为.hbm.xml的同一个包下的文件) .buildSessionFactory(); } //保存对象到数据库 @Test public void testSave() throws Exception { //准备对象 User user = new User(); user.setName("张三"); //保存到数据库中 Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(user);//保存 tx.commit(); session.close(); } //从数据库中获取一条数据 @Test public void testGet() throws Exception { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = (User) session.get(User.class,1);//保存 tx.commit(); session.close(); System.out.println(user);//显示信息 } } |
转载于:https://my.oschina.net/u/2427561/blog/733779