作业01:第一次作业

重要!本博文非自愿编写,详见:关于本博客目前情况下,所有内容的声明。

准备工作:

1.下载 Java SE JDK 并安装,同时,配置 "path"、"JAVA_HOME" 等环境变量。我使用的是 Oracle Java SE JDK 8u172

下载地址:http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html

2.下载 Eclipse 并安装,我使用的是 Eclipse Oxygen.3a (4.7.3a)。

下载地址:http://www.eclipse.org/downloads/eclipse-packages/

3.下载 MySQL 并安装,我使用的是 MySQL Community Server 5.7.22。

下载地址:https://dev.mysql.com/downloads/installer/

4.下载 Hibernate ORM 并作为 Eclipse 的用户库,我使用的是 Hibernate ORM 5.2.17。

下载地址:http://hibernate.org/orm/

创建项目并导入JAR包


编写实体类

public class Customer {
	private Integer id; //主键id
	private String name; //姓名
	private Integer age;  //年龄
	private String sex;  //性别
	private String city; //所在城市

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", age=" + age
				+ ", sex=" + sex + ", city=" + city + "]";
	}

}

编写映射文件 Customer.hbm.xml

<?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">
<hibernate-mapping>
    <!-- name代表的是实体类名   talbe代表的是表名 -->
  <class name="Customer" table="customer">
	<!-- name=id 代表的是customer类中属性  
	     column=id 代表的是table表中的字段 -->
	<id name="id" column="id"> 
		<generator class="native"/><!-- 主键生成策略 -->
	</id>
   	<!-- 其它属性使用property标签来映射 -->
	<property name="name" column="name" type="string" />
	<property name="age" column="age" type="integer" />  
	<property name="sex" column="sex" type="string"/>
	<property name="city" column="city" type="string"/>
   </class>
</hibernate-mapping> 

编写核心配置文件 hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配制文件的dtd信息 -->
<!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>
	<!-- 指定方言 -->
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!--数据库驱动 MySQL 5.7 -->
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<!--数据库驱动 MySQL 8.0 -->
	<!--
	<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
	-->
    <!--连接数据库的url -->
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
	<!--数据库的用户名 -->
	<property name="hibernate.connection.username">root</property>
	<!--数据库的密码 -->
	<property name="hibernate.connection.password">19961107</property>
    <!--其它配置 -->
    <!-- 显示sql语句 -->
	<property name="hibernate.show_sql">true</property>
	<!-- 格式化sql语句 -->
	<property name="format_sql">true</property>
	<!-- 用来关联hbm配置文件 -->
	<mapping resource="Customer.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

猜你喜欢

转载自blog.csdn.net/qq853419196/article/details/80231688