映射 Map(基本数据类型)(参考张龙老师的)

映射 Map(基本数据类型)(参考张龙老师的),第1张

概述1.Team.java package org.yang.hibernate; import java.util.HashMap; import java.util.Map; public class Team {     private String id;     private String teamName;     private Map<String, String> students

1.Team.java

package org.yang.hibernate;

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

public class Team
{
private String ID;
private String teamname;
private Map<String,String> students = new HashMap<String,String>();
public String getID() {
return ID;
}
public voID setID(String ID) {
this.ID = ID;
}
public String getTeamname() {
return teamname;
}
public voID setTeamname(String teamname) {
this.teamname = teamname;
}
public Map<String,String> getStudents() {
return students;
}
public voID setStudents(Map<String,String> students) {
this.students = students;
}


}

2.Team.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="org.yang.hibernate.Team" table="team">
<ID name="ID" column="ID" type="string">
<generator ></generator>
</ID>

<property name="teamname" type="string">
<column name="teamname" length="13"></column>
</property>

<map name="students" table="student">
<key column="team_ID"></key>
<!-- 这里的index存放的是KEY -->
<index column="name" type="java.lang.String"></index>
<!-- 这里的element存入的是VALUE -->
<element column="description" type="java.lang.String"></element>
</map>

</class>
</hibernate-mapPing>


3.Createtable.java

package org.yang.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Createtable
{
public static voID main(String[] args)
{
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true,true);
}
}


4. 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">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/myhibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">yang</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgresqlDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<mapPing resource="Team.hbm.xml" />
</session-factory>
</hibernate-configuration>


5.Test.java

package org.yang.hibernate;

import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test
{
private static SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Exception ex)
{
ex.printstacktrace();
}
}

public static voID main(String[] args)
{
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
Team team = new Team();
team.setTeamname("team1");

Map<String,String> map = team.getStudents();
map.put("zhanggsan","helloworld");
map.put("lisi","welcome");

session.save(team);
tx.commit();
}
catch(Exception ex)
{
if (null != tx)
{
tx.rollback();
}

ex.printstacktrace();
}
finally
{
session.close();
}

}

}

总结

以上是内存溢出为你收集整理的映射 Map(基本数据类型)(参考张龙老师的)全部内容,希望文章能够帮你解决映射 Map(基本数据类型)(参考张龙老师的)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1179468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存