我的Hibernate学习日志(1)

首先做一些准备工作,第一步启动mysql服务,将MySQL准备好。


找到服务并启动Mysql,至此数据库准备完毕。

第二步 创建项目并导入JAR包

在Myeclipse中创建一个web项目,命名为learn1


将所需JAR包复制到WEB-INF/lib目录中,并将所有JAR包添加到类路径中。


如图所示,添加成功

第三部 创建数据库及表

在MySQL中创建一个名为Hibernate的数据库,创建一个名为customer的表,添加五个字段,分别为id、name、age、sex、city,将id设为主键。


第四步 编写实体类

在项目src目录下,创建包,并在包中创建实体类,与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 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+"]";
	}
}

第五步 编写映射文件

在实体类中,创建一个映射文件


其中代码如下:

<?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>
	<class name="cn.itcast.domain.Customer" table="customer">
	<id name="id" column="id">
		<generator class="native"/>
	</id>
	<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>

第六步 编写核心配置文件

创建一个xml文件,并编写


其中代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!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="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql:///hibernate_20160926</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">yezi</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="cn/itcast/a_helloworld/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>

第七步 编写测试类进行测试

在项目中新建一个源文件夹,并在此文件夹中创建一个包,在包中建立一个test文件,并新建测试类

package cn.itcast.test;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
import org.hibernate.cfg.Configuration;  
import org.hibernate.service.ServiceRegistry;    
  
public class Test {  
  @Test
    SessionFactory sessionFactory;  
    Transaction transaction;  
    Session session;  

   Customer c=new Customer();
   c.setName("wwz");
   c.setAge("22");
   c.setCity("hrb");
   c.setSex("male");
   session.save.(c);
   t.commit();
   session.close();
   sessionFactory.close();
  }
}  

测试结果:


猜你喜欢

转载自blog.csdn.net/qq_39758738/article/details/80232858
今日推荐