Hibernate 安装配置

来源:互联网 发布:龙献文天下数据 编辑:程序博客网 时间:2024/06/07 02:16

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刑政复议通知书被邮政延误了怎么办 高考听力报名注册了两个用户怎么办 左腿神经损伤夏天脚冰凉怎么办 给区组织部的介绍信给到社区怎么办 被丈夫和儿子强送精神病院怎么办 练车穿短袖晒的胳膊特别黑怎么办 车子卖了对方迟迟不过户怎么办 成都华西医院就诊卡密码忘了怎么办 资阳办健康证怎么办要预约吗 头发出油厉害怎么办民间小偏方 你帮助别人别人却想着害你怎么办 怀孕接触有辐射的东西回怎么办 苹果手机用久了有点卡怎么办 4s店把我车撞了怎么办 长安之星2代大灯高不聚光怎么办 被电动车撞了人跑了怎么办 车被电动车撞了对方跑了怎么办 房子卖了户口没地方迁怎么办 酷派大神f2开不开机怎么办 酷派手机玩游戏竖屏怎么办 身上起红疙瘩很痒怎么办越挠越多 苹果6s指纹解锁坏了怎么办 案子结了网上追逃的怎么办 贷款买的手机不还了会怎么办 支付宝手机号没用了登陆不了怎么办 支付宝绑定的手机号注销了怎么办 考勤机进水了不能识别指纹怎么办? 网商银行人脸识别失败怎么办 电脑网页上的字变小了怎么办 把光驱换成固态硬盘后不识别怎么办 相机内存卡电脑读不出来怎么办 sd卡在电脑上无法格式化怎么办 内存卡突然读不出来了怎么办 怀孕两个月胎儿死在腹中怎么办 香港公司在大陆卖地皮资金怎么办 结婚证上的身份证号码错了怎么办 身份证快过期了人在外地怎么办 邮政电话银行登录密码忘记了怎么办 如果欠了3w不敢和家里说怎么办 大四学生欠了3w该怎么办 房子付了首付贷款贷不下来怎么办