标签:
1.新建项目
2.新建hibernate.xml框架 右击项目名->MyEclipse->add hibernate
3.新建一个对数据库表进行操作的 名字.hbm.xml模块 右击src->new->XML(Basic templates)->(改名字)Next->(create XMLfile from a DTD file)Next->(Select XML Catalog entry)Key中找到Hibernate Mapping DTD 3.0 Next->Finish
4.配置对数据库表进行操作的名字.hbm.xml模块 视图操作
(1).右击模块名字->new->Class 添加 Name和Table(数据库表名)属性
(2).右击Class 分别new ID和property 注:ID分别new column和generator ID要改为手动输入: <generator class="assigned"/>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="com.abc.Person" table="T_person"> 6 <id name="ID" type="java.lang.Integer"> 7 <column name="person_id"/> 8 <generator class="assigned"/> 9 </id> 10 <property generated="never" lazy="false" name="Name" type="java.lang.String"/> 11 <property generated="never" lazy="false" name="age" type="java.lang.Integer"/> 12 <list name="schools" table="school"> 13 <key column="person_id" not-null="true"/> 14 <list-index base="0" column="list_order"/> 15 <element column="schoos_name" type="java.lang.String"/> 16 </list> 17 </class> 18 </hibernate-mapping>
5.配置hibernate.xml框架
创建一个DB连接数据库
在hibernate.xml选择视图操作
在DB Driver里选择创建好的DB数据库连接,最后在Mappings里Add添加Person.hbm.xml
6.对与数据库表相对应的实体类操作
主要是与数据库表的属性,字段一致,右击空白处->Source->Generate Getters and Setters...添加Get和Set
1 package com.abc; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Person { 7 private Integer ID; 8 private String Name; 9 private int age; 10 private List<String> schools=new ArrayList<String>(); 11 public Integer getID() { 12 return ID; 13 } 14 public void setID(Integer iD) { 15 ID = iD; 16 } 17 public String getName() { 18 return Name; 19 } 20 public void setName(String name) { 21 Name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 public List<String> getSchools() { 30 return schools; 31 } 32 public void setSchools(List<String> schools) { 33 this.schools = schools; 34 } 35 36 }
7.调用使用
1 package com.abc; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 11 public class Test { 12 13 public static void a(){ 14 15 Configuration configuration=new Configuration().configure(); 16 //configuration.configure("/hibernate.cfg.xml"); 17 18 SessionFactory sessionFactory=configuration.buildSessionFactory(); 19 Session session=sessionFactory.openSession(); 20 Transaction trans=session.beginTransaction(); 21 Person b=new Person(); 22 b.setID(123); 23 b.setAge(0); 24 b.setName("张三"); 25 List<String> schools=new ArrayList<String>(); 26 schools.add("高中"); 27 schools.add("学校"); 28 b.setSchools(schools); 29 session.save(b); 30 trans.commit(); 31 session.close(); 32 } 33 34 }
8.index.jsp调用方法
9.单击添加Tomcat服务器运行
标签:
原文地址:http://www.cnblogs.com/ZSK991656110/p/4478240.html