Hibernate Annotation _List/Map
生活随笔
收集整理的這篇文章主要介紹了
Hibernate Annotation _List/Map
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// Student.java 實(shí)體類(lèi)package com.tao.pojo;import java.util.List;public class Student {private int id;private String name;private String gender;private int age;//list集合private List<String> hobby; //Map 集合//private Map<Integer,String> hobby=new HashMap<Integer,String>();public Student() {super();}public Student(int id, String name, String gender, int age) {super();this.id = id;this.name = name;this.gender = gender;this.age = age;}public Student(String name, String gender, int age, List<String> hobby) {super();this.name = name;this.gender = gender;this.age = age;this.hobby = hobby;}public Student(int id, String name, String gender, int age, List<String> hobby) {super();this.id = id;this.name = name;this.gender = gender;this.age = age;this.hobby = hobby;}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 String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public List<String> getHobby() {return hobby;}public void setHobby(List<String> hobby) {this.hobby = hobby;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";} }//Student.hbm.xml 映射文件<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.schemaupdate"><class name="com.tao.pojo.Student" table="student"><id name="id" column="id"></id><property name="name" column="name" type="string"/> <property name="age" column="age"/> <list name="hobby" table="stu_hobby"><!-- 外鍵,自己起名 --><key column="sid"></key><!--索引列 --><list-index column="stu_index"></list-index><!--其他普通列 --><element column="descx" type="string"></element></list>
<!--map集合 -->
<!-- <map name="hobby" table="bobby"><key column="sid"></key><index column="stu_id" type="int"></index><element column="descx" type="string"></element></map> --></class></hibernate-mapping>// hibernate.cfg.xml 配置文件<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><!-- 數(shù)據(jù)庫(kù)連接部分 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/test0111_list?characterEncoding=utf-8</property><property name="connection.username">root</property><property name="connection.password">root</property><!--顯示SQl語(yǔ)句 --><property name="show_sql">true</property><property name="format_sql">true</property><!--MySQL數(shù)據(jù)庫(kù)方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- 自動(dòng)創(chuàng)建或生成表--><property name="hbm2ddl.auto">update</property><!--映射文件 --><mapping resource="com/tao/pojo/Student.hbm.xml"/> </session-factory></hibernate-configuration>// Test.java 測(cè)試類(lèi)package com.tao.test;import java.util.ArrayList;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.MySQL5Dialect;import com.tao.pojo.Student;public class Test {public static void main(String[] args) {Configuration configure = new Configuration().configure();SessionFactory factory = configure.buildSessionFactory();Session session = factory.openSession();session.beginTransaction();//添加數(shù)據(jù)(在添加數(shù)據(jù)之前先——生成表,把添加數(shù)據(jù)部分先注釋)Student stu = new Student(1, "aa", "nn", 25);ArrayList<String> hobby = new ArrayList<String>();hobby.add("旅游");hobby.add("散步");hobby.add("學(xué)習(xí)");hobby.add("跳舞");stu.setHobby(hobby);session.save(stu);/*//Map添加數(shù)據(jù)Student stu1 = new Student(2, "吖吖", "男", 26);stu.getHobby().put(1, "讀書(shū)");stu1.getHobby().put(2, "旅游");stu2.getHobby().put(3, "跳舞");stu2.getHobby().put(4, "唱歌");session.save(stu);*/session.getTransaction().commit();;session.close();factory.close();}
}
轉(zhuǎn)載于:https://www.cnblogs.com/jili6254/p/8268093.html
總結(jié)
以上是生活随笔為你收集整理的Hibernate Annotation _List/Map的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ubuntu网卡问题
- 下一篇: 表空间管理