dljd_(007-010)hibernate_第一个程序
一、定义实体类
1.1新建一个java project项目
1.2新建lib文件夹并导入hibernate项目所必须的外部jar文件
1.3实体类编写
package edu.aeon.beans; /** * [说明]:学生实体(bean)类 * @author aeon * */ public class Student { /**用户id*/ private Integer stuId; /**用户名*/ private String stuName; /**用户年龄*/ private int stuAge; /**用户分数*/ private double stuScore; /**空构造器*/ public Student() { super(); } /**带惨构造、没有stuId是因为最后这个用户id我们自动生成*/ public Student(String stuName, int stuAge, double stuScore) { super(); this.stuName = stuName; this.stuAge = stuAge; this.stuScore = stuScore; } /**setter/getter*/ public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public double getStuScore() { return stuScore; } public void setStuScore(double stuScore) { this.stuScore = stuScore; } /**重写toString方法*/ @Override public String toString() { return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + ", stuScore=" + stuScore + "]"; } }
1.4映射实体类到数据库表
dtd头文件可以直接从导入的包中找!
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 映射文件的作用: 1.映射实体类到数据库表 2.映射实体类属性到数据库表字段 --> <hibernate-mapping package="edu.aeon.beans.Student"> <!-- 1.映射实体类到数据库表 --> <class table="t_student"> <!-- 2.映射实体类属性到数据库表字段 --> <id name="stuId"> <generator class="native"/> </id> <property name="stuName" length="16" column="stu_name" /> <property name="stuAge" column="stu_age" /> <property name="stuScore" length="6" column="t_score" /> </class> </hibernate-mapping>