Hibernate 安装配置

http://download.jboss.org/jbosstools/updates/stable/kepler

 

http://jingyan.baidu.com/article/db55b609959d154ba20a2f5d.html

 

尼玛, 老子装这个 插件累死了 。

 

http://www.cnblogs.com/AlanLee/p/5836823.html

 

 

http://www.cnblogs.com/svennee/p/4078763.html  里面软件包介绍

 

http://blog.csdn.net/wen303614/article/details/17100347

 

http://www.cnblogs.com/leiOOlei/archive/2013/10/08/3357089.html

 

http://blog.csdn.net/undergrowth/article/details/9963529

 

http://blog.csdn.net/c1481118216/article/details/52854449

 

 

 

 http://www.java2s.com/Code/Jar/j/Downloadjboss422GAjar.htm  下载jar

 

http://www.cnblogs.com/leiOOlei/archive/2013/10/08/3357089.html

 

http://blog.csdn.net/jackiehff/article/details/8181945/  Eclipse常用开发插件

 

https://zhidao.baidu.com/question/1498711296646440659.html

 

 

http://wenku.baidu.com/link?url=xZxLD9fUnMAK9D3Ms-Trrv8skOghggY8heZVPhWLPIw1XTLNoXR7UPJmz7miKH0NyGWSJXhL_8IRoI6buc9PNGZQLo0vsOcX7ZZ8bMKvrc3

 

http://blog.csdn.net/peterli_xue/article/details/6958323

 

 

http://macrochen.iteye.com/blog/1343807

 

http://blog.csdn.net/leoking01/article/details/51730924

 jar包的研究:http://www.cnblogs.com/taony/p/5759660.html

http://blog.sina.com.cn/s/blog_6c9bac050100my5d.html

 

http://www.cnblogs.com/0616--ataozhijia/p/4094952.html

 

http://www.cnblogs.com/shellway/p/3935471.html

 

http://blog.csdn.net/zhang_xinxiu?viewmode=contents

 

http://blog.sina.com.cn/s/blog_8af106960101c5xt.html

 

Java框架技术简介:

https://zhidao.baidu.com/question/522454718100137805.html

 

开源代码自动生成工具:

http://blog.csdn.net/zl19861225/article/details/8626679

 

 

 

 

 

 

想要成为java大牛必须知道的25点

http://blog.csdn.net/gemire/article/details/8172259

 

 

http://blog.csdn.net/mahoking/article/details/43083619

http://blog.sina.com.cn/s/blog_5ed73dc00101j1g3.html

 

http://blog.csdn.net/zhang_xinxiu?viewmode=contents

 

http://blog.csdn.net/qq_29829081/article/details/51002599

 

 hibernate4  单向的数据库 一对一关联:

参考文档:

http://blog.csdn.net/aboy123/article/details/10301973

http://zs15932616453.iteye.com/blog/1918226

 

http://blog.csdn.net/zhang_xinxiu?viewmode=contents

 

自己做的案例:

 创建  纯 的  动态网站:


create table user2  ( userid int(11)  not null primary key  auto_increment  default null ,
                        name varchar(20)  null   default null ,
                         password varchar(12) null  default null
                                                                                       
 );


create table address  ( addressid int(11)  not null primary key  auto_increment  default null ,
                        addressinfo varchar(255)  null   default null
                                  
 );

 

-------------------------

 

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="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.username">root</property>
  <property name="connection.password">123</property>
  <property name="connection.url">jdbc:mysql://localhost:3306/db_onlineexam</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
  <property name="hbm2ddl.auto">update</property>
        <mapping resource="org/hibernate/entity/User.hbm.xml"/>
        <mapping resource="org/hibernate/entity/Address.hbm.xml"/>
 
 </session-factory>
</hibernate-configuration>

 

------------------------

 HibernateUtil。java

package org.hibernate.entity;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.ServiceRegistryBuilder;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
//hibernate 4.3

public class HibernateUtil {

 // Configuration cfg = new Configuration().configure();

 private static Configuration cfg = null;

 private static ServiceRegistry serviceRegistry = null;

 private static SessionFactory sessionFactory = null;

 private static Session session = null;

 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

 static {
  cfg = new Configuration().configure();
  serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
  sessionFactory = cfg.buildSessionFactory(serviceRegistry);
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;

 }

 public static Session getSession() {

  session = sessionFactory.openSession();
  threadLocal.set(session);
  return session;
 }

 public static void closeSession() {
  Session session = (Session) threadLocal.get();
  if (session != null)
   session.close();
 }

}

-------------------------------

 

 

package org.hibernate.dao;

import java.util.List;

import org.hibernate.entity.Address;
import org.hibernate.entity.User;


public interface UserDAO {
  void save(User user,Address address );    
  User findById(int id);    
     void delete(User user);    
  void update(User user);    
}

 

-----------------------------------

 

package org.hibernate.dao;

import org.hibernate.*;
import org.hibernate.entity.*;

public class UserDAOImpl implements UserDAO {
 
public void save(User user ,Address address){
 Session session= HibernateUtil.getSession(); //����Sessionʵ��
Transaction tx = session.beginTransaction(); //����Transactionʵ��
try{
 session.save(address);
 session.save(user); 
 
 tx.commit();
} catch(Exception e){
e.printStackTrace();
tx.rollback();        //�ع�����
}finally{
     HibernateUtil.closeSession();    //�ر�Sessionʵ��
}
}
//�����û���ʶ����ָ���û� 
public User findById(int id){ 
 User user=null;
Session session= HibernateUtil.getSession(); //����Sessionʵ��
Transaction tx = session.beginTransaction(); //����Transactionʵ��
try{
 user=(User)session.get(User.class,id);   //ʹ��Session��get������ȡָ��id���û����ڴ���
 tx.commit();        //�ύ����
} catch(Exception e){
e.printStackTrace();
tx.rollback();        //�ع�����
}finally{
     HibernateUtil.closeSession();    //�ر�Sessionʵ��
}
return user;
}
//ɾ���û� 
public void delete(User user){
Session session= HibernateUtil.getSession(); //����Sessionʵ��
Transaction tx = session.beginTransaction(); //����Transactionʵ��
try{
 session.delete(user);      //ʹ��Session��delete�������־û�����ɾ��
 tx.commit();        //�ύ����
} catch(Exception e){
e.printStackTrace();
tx.rollback();        //�ع�����
}finally{
     HibernateUtil. closeSession();    //�ر�Sessionʵ��
}
}     
//�޸��û���Ϣ
public void update(User user){
Session session= HibernateUtil.getSession(); //����Sessionʵ��
Transaction tx = session.beginTransaction(); //����Transactionʵ��
try{
session.update(user);      //ʹ��Session��update�������³־û����� 
tx.commit();        //�ύ����
} catch(Exception e){
e.printStackTrace();
tx.rollback();        //�ع�����
}finally{
     HibernateUtil. closeSession();    //�ر�Sessionʵ��
}
}     
}

 

 

------------------------------


package org.hibernate.entity;
public class User {
 private int userid;     // User��ı�ʶ����
 private String name;    //name����
 private String password;   //password����
 private Address address;    // User��Ĺ���ʵ������
 //�޲ι��췽��
 public User() {                                                                  
 }
 //��ʼ������User���ԵĹ��췽��
 public User(int userid, String name, String password, String type, Address address) {
  this.userid = userid;
  this.name = name;
  this.password = password;
  this.address = address;
 }
 // userid���Ե�getter��setter����
 public int getUserid() {
  return this.userid;
 }
 public void setUserid(int userid) {
  this.userid = userid;
 }
 //name��getter��setter����
 public String getName() {
  return this.name;
 }
 public void setName(String name) {
  this.name = name;
 }
 //password���Ե�getter��setter����
 public String getPassword() {
  return this.password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 //address��getter��setter����
 public Address getAddress() {
  return this.address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
}
----------------------------

 

User.hbm.xml

 

<?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 2012-10-31 20:36:01 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="org.hibernate.entity.User" table="user2">
        <id name="userid" type="int" access="field">
            <column name="userid" />
            <generator class="foreign">
             <param name="property">address</param>
            </generator>
           
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" />
        </property>
        <one-to-one name="address" class="org.hibernate.entity.Address" constrained="true">
        </one-to-one>
    </class>
</hibernate-mapping>

 

-----------------------------------------

package org.hibernate.entity;

public class Address {

 private int addressid;
    private String addressinfo;
 //�޲ι��췽��
 public Address () {
 }
 //��ʼ������Address���ԵĹ��췽��
 public Address (int addressid, String addressinfo) {
  this.addressid = addressid;
  this.addressinfo = addressinfo;
 }
 //Address���Ե�getter��setter����
 public int getAddressid () {
  return this.addressid;
 }
 public void setUseid(int addressid) {
  this.addressid = addressid;
 }
 //addressinfo��getter��setter����
 public String getAddressinfo () {
  return this.addressinfo;
 }
 public void setAddressinfo (String addressinfo) {
  this.addressinfo = addressinfo;
 }
}

 

----------------------------------

 

Address.hbm.xml

<?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 2012-10-31 20:36:01 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="org.hibernate.entity.Address" table="address">
        <id name="addressid" type="int" access="field">
            <column name="addressid" />
            <generator class="identity" />
        </id>
        <property name="addressinfo" type="java.lang.String">
            <column name="addressinfo" />
        </property>
    </class>
</hibernate-mapping>

----------------------------

package org.hibernate.test;
import org.hibernate.dao.*;
import org.hibernate.entity.Address;
import org.hibernate.entity.User;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;


public class UserTest {
  
 @Before
  public void setUp() throws Exception {      }
     
 @Test
 public void testSave( ){ 
  
  UserDAO userdao=new UserDAOImpl(); 
  
  User u=new User();
  u.setName("tom5");
  u.setPassword("111");
 // u.setUserid(4);
  
  Address a=new Address();  
  a.setAddressinfo("America5");  
  u.setAddress(a);
  
  userdao.save(u ,a);
  
  
 
 }
}

================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 ==========================================================

 C3P0 连接池:

 

http://blog.csdn.net/mic_hero/article/details/7699414

 http://blog.csdn.net/hanjiancanxue_liu/article/details/9966423

 

 http://blog.csdn.net/feier7501/article/details/18822607

 

 http://blog.knowsky.com/194421.htm

 

 http://www.cnblogs.com/caoyc/p/5607051.html

 

 http://blog.csdn.net/yipanbo/article/details/43986829

 http://www.iteye.com/problems/87600

 

 http://blog.csdn.net/icecityman/article/details/5500438

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值