Hibernate三种容器(List,Set,Map)映射技术之Map映射

 

 环境: eclipse+myeclipse+mysql.

(1) 建表脚本

--刪除表
drop table if exists phone  ;
drop table if exists person ;


--新建表
create table person
(
 id int not null AUTO_INCREMENT primary key ,
 name  varchar(20) ,
 age int
) ;

create table phone
(
 id int ,
 name varchar(20) not null,
 numbers varchar(11) not null ,
 foreign key (id) references person(id) on delete cascade
) ;

--提交
commit ;

(2) Person.java的编写.

package org.hibernate.mapDemo.vo;

import java.util.Map;

public class Person {
 private int id ;
 private String name ;
 private int age ;
 private Map phone;
 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;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Map getPhone() {
  return phone;
 }
 public void setPhone(Map phone) {
  this.phone = phone;
 }
}


(3) Person.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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.hibernate.mapDemo.vo.Person" table="person" >
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age" />
        </property>
        <map name="phone" table="phone">
         <key column="id"></key>
         <index column="name" type="java.lang.String"></index>
         <element type="java.lang.String">
          <column name="numbers"></column>
         </element>
        </map>
    </class>
</hibernate-mapping>

<4> 測試類的編寫.

package org.hibernate.mapDemo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapDemo.vo.Person;

public class PersonDao {
 private Session session ;
 public PersonDao()
 {
  session = new Configuration().configure()
     .buildSessionFactory().openSession() ;
 }
 
 
 
 public void insert(Person p)
 {
  session.save(p) ;
  session.beginTransaction().commit() ;
  session.close() ;
 }
 
 public Person findbyid(int id)
 {
  String hql = "from Person as p where p.id=?" ;
  Query q = session.createQuery(hql) ;
  q.setInteger(0, id) ;
  List l = q.list() ;
  if(l.size()>0)
   return (Person)l.get(0) ;
  return null ;
 }
 
 public void update(Person p)
 {
  session.update(p) ;
  session.beginTransaction().commit() ;
  session.close() ;
 }
 
 public static void main(String[] args)
 {
  PersonDao pd = new PersonDao() ;
//插入  
//  Person p = new Person() ;
//  p.setName("zhangsan") ;
//  p.setAge(10) ;
//  p.setPhone(new HashMap()) ;
//  Map m = p.getPhone() ;
//  m.put("lisi", "123") ;
//  m.put("lisi", "123") ;
//  m.put("lisi", "124") ;
//  m.put("wangwu", "123") ;
//  m.put("wangwu", "126") ;
//  p.setPhone(m) ;
//  pd.insert(p) ;

//更新  
  Person p = pd.findbyid(1) ;
  p.setName("lisi") ;
  p.setAge(1) ;
  
  p.getPhone().put("lisi", "lis3i") ;
  p.getPhone().put("lisis2", "lisi") ;
  
  pd.update(p) ;
  
 }
 
}

注:此處免去了Hibernate.cfg.xml的編寫.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值