【myeclipse逆向工程生成domain对象】

本文介绍如何使用MyEclipse通过Hibernate进行数据库映射,包括创建Java工程、生成实体类及其配置文件,并详细展示了Student、Subject及Sc三个实体类的Java代码与对应的XML映射文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值