您的位置:首页 > 其它

hibernate_day02_04_实现类操作(添加和根据id查询)

2017-11-24 15:46 435 查看
项目目录结构:



1:实体类com.hlg.entity.User

package com.hlg.entity;

public class User {

private int uid;
private String username;
private String password;
private String address;

public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}


2:User.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.hlg.entity.User" table="t_user">
<id name="uid" column="uid">
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>


3:Hibernate核心配置文件 /hibernate_day02/src/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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://*.*.36.138:3306/hibernate_day02?userUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">*</property>

<!-- 第二部分:配置hibernate信息  可选的 -->
<!-- 输出底层 sql语句  -->
<property name="hibernate.show_sql">true</property>
<!-- 输出底层 sql语句的格式  -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate 创建表,需要配置之后
update:如果已经有表,就更新;如果没有,创建
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 第三部分 :把映射文件放到核心配置文件中  必须 -->
<mapping resource="com/hlg/entity/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>


4:工具类:com.hlg.utils.HibernateUtils

package com.hlg.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

static Configuration cfg = null;
static SessionFactory sessionFactory = null;

//静态代码块实现
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();

}

public static SessionFactory getSessionFactory(){
return sessionFactory;
}

public static void main(String[] args){}
}


5:测试类

package com.hlg.hibernatetest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import com.hlg.entity.User;
import com.hlg.utils.HibernateUtils;

public class HibernateDemo {

@Test
public void testGet(){
//1 调用工具类得到sessionFactory
SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
//2 获取session
Session session = sessionFactory.openSession();
//3 开启事务
Transaction tx = session.beginTransaction();

//4 根据Id查询
User user = session.get(User.class, 1);
System.out.println(user);
//5 提交事务
tx.commit();
//6 关闭资源
session.close();
sessionFactory.close();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐