我们所要了解的是ORM的使用,所以我们需要了解一下ORM的环境构建以及一个基本的Demo的实现。
首先我们需要了解一下ORM的原理,所谓的ORM就是利用描述对象和数据库表之间映射的元数据,自动把Java应用程序中的对象,持久化到关系型数据库的表中。通过操作Java对象,就可以完成对数据库表的操作。
然后我们要了解一下ORM的环境构建:首先需要一个软件开发工具,eclipse/myeclipse都可以,在开发工具中要有一个jdk1.8的开发环境,然后需要一些有关Hibernate相关的Jar包进行程序驱动,还需要一个数据库(MYSQL或ORACLE,我所用的是MYSQL数据库)以便我们进行数据访问,而在程序编写时不仅需要一个实例化的具体类,还需要一个与该实体类所契合的*.hbm.xml文件.
以下是代码
Customer
package cn.itcast.domain;
public class Customer {
private Integer 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;
}
public String toString(){
return "Customer [id="+id+ ", name="+name+", age= "+age+", sex="+sex+", city="+city+"]";
}
}
CustomerTest
package cn.itcast.domain;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class CustomerTest {
@Test
public void insertTest(){
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction t=session.beginTransaction();
Customer c=new Customer();
c.setName("王五");
c.setAge(20);
c.setCity("上海");
c.setSex("男");
session.save(c);
System.out.println(c.getAge());
t.commit();
session.close();
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hiberate-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>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 链接数据库的url -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<!-- 数据库的名-->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 其他配置 -->
<!-- 显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式SQL语句 -->
<property name="format_sql">true</property>
<!-- 用来关联hbm配置文件 -->
<mapping resource="cn/itcast/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTO 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- name代表的是实体类名,table代表的是表名 -->
<class name="cn.itcast.domain.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>
实验结果