【myeclipse中连数据库】【mysql事例】2018年04月03日 22:49:12
首先连数据库(上面这篇文章有介绍)->创建一个java工程-创建包(选择src鼠标右键->new->package)->创建一个lib文件夹(在java工程下,选择src鼠标右键->new->File)->选中工程,然后鼠标右键->myeclipse->Add Hibernate Annotation..->然后进行数据库的逆向工程生成domain(选中student->鼠标右键->Hibernate Engineering-如图
代码显示
1、Sc.java
package com.lw.domain;
/**
* Sc entity. @author MyEclipse Persistence Tools
*/
public class Sc implements java.io.Serializable {
// Fields
private Integer id;
private Subject subject;
private Student student;
// Constructors
/** default constructor */
public Sc() {
}
/** full constructor */
public Sc(Subject subject, Student student) {
this.subject = subject;
this.student = student;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Subject getSubject() {
return this.subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
}
2、Sc.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="com.lw.domain.Sc" table="sc" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="sequence" />
</id>
<many-to-one name="subject" class="com.lw.domain.Subject" update="false" insert="false" fetch="select">
<column name="id" not-null="true" unique="true" />
</many-to-one>
<many-to-one name="student" class="com.lw.domain.Student" update="false" insert="false" fetch="select">
<column name="id" not-null="true" unique="true" />
</many-to-one>
</class>
</hibernate-mapping>
3、Student.java
package com.lw.domain;
import java.util.HashSet;
import java.util.Set;
/**
* Student entity. @author MyEclipse Persistence Tools
*/
public class Student implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private Set scs = new HashSet(0);
// Constructors
/** default constructor */
public Student() {
}
/** minimal constructor */
public Student(Integer id) {
this.id = id;
}
/** full constructor */
public Student(Integer id, String name, Set scs) {
this.id = id;
this.name = name;
this.scs = scs;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getScs() {
return this.scs;
}
public void setScs(Set scs) {
this.scs = scs;
}
}
4、Student.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="com.lw.domain.Student" table="student" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="10" />
</property>
<set name="scs" inverse="true">
<key>
<column name="id" not-null="true" unique="true" />
</key>
<one-to-many class="com.lw.domain.Sc" />
</set>
</class>
</hibernate-mapping>
5、Subject.java
package com.lw.domain;
import java.util.HashSet;
import java.util.Set;
/**
* Subject entity. @author MyEclipse Persistence Tools
*/
public class Subject implements java.io.Serializable {
// Fields
private Integer id;
private String subject;
private Set scs = new HashSet(0);
// Constructors
/** default constructor */
public Subject() {
}
/** minimal constructor */
public Subject(Integer id) {
this.id = id;
}
/** full constructor */
public Subject(Integer id, String subject, Set scs) {
this.id = id;
this.subject = subject;
this.scs = scs;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSubject() {
return this.subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Set getScs() {
return this.scs;
}
public void setScs(Set scs) {
this.scs = scs;
}
}
6、Subject.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="com.lw.domain.Subject" table="subject" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="subject" type="java.lang.String">
<column name="subject" />
</property>
<set name="scs" inverse="true">
<key>
<column name="id" not-null="true" unique="true" />
</key>
<one-to-many class="com.lw.domain.Sc" />
</set>
</class>
</hibernate-mapping>
7、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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">test</property>
<mapping resource="com/lw/domain/Student.hbm.xml" />
<mapping resource="com/lw/domain/Subject.hbm.xml" />
<mapping resource="com/lw/domain/Sc.hbm.xml" />
</session-factory>
</hibernate-configuration>