Hibernate简单配置

1.User.java文件
package com.po;
public class User{
 private Integer id;
 private String name;
 private Integer age;
 
 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;
 }
}
 
2.sql角本
create table user(
 id int not null primary key,
 name varchar(50) not null default '',
 age int
)

3.User.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">
<hibernate-mapping>
    <class name="com.po.User" table="user" schema="dbo" catalog="pubs">
        <id name="id" column="id">
   <generator class="native">
        </id>
        <property name="name" column="name">        
        <property name="age" column="age"> 
    </class>
</hibernate-mapping>

4.IUserDAO.java文件
package com.po;
public interface IUserDAO
{
 public void insert(User user);
 public User find(Integer id);
}

5.UserDAO.java文件
package com.po;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibername.Transaction;

public class UserDAO implements IUserDAO
{
 private SessionFactory sessionFactory;
 
 public UserDAO(){
 }
 public UserDAO(SessionFactory sessionFactory){
  this.setSessionFactory(sessionFactory);
 }
 public void setSessionFactory(SessionFactory sessionfactory){
  this.sessionFactory=sessionFactory;
 }

 public void insert(User user){
  Session session = sessionFactory.openSession();
  Transaction tx = session.beginTransaction();
  session.save(user);
  tx.commit();
  session.close();
 }

 public User find(Integer id){
  Session session = sessionFactory.openSession();
  User user = (User)session.get(User.class,id);
  Hibernate.initialize(user);
  session.close();
  return user;
 }
}

6.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">
<hibernate-configuration>
 <session-factory>
  <property name="show_sql">
   true
  </property>

  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>

  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>

  <property name="connection.url">
   jdbc:mysql://localhost/demo
  </property>

  <property name="connection.username">
   op
  </property>

  <property name="connection.password">
   11111
  </property>

  <mapping resource="com/po/User.hbm.xml">
 </session-factory>
</hibernate-configuration>

7.HibernateDemo.java文件
package com.po;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateDemo
{
 public static void main(String[] args){
  Configuration config = new Configuration().configure();
  SessionFactory sessionFactory = config.buildSessionFactory();

  IUserDAO userDAO = new UserDAO(sessionFactory);
  User user = new User();
  user.setName("koko");
  user.setAge(new Integer(30));

  userDAo.insert(user);

  user = userDAO.find(new Integer(1));
  System.out.println("name:"+user.getName());
 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值