由于好长时间不用hibernate,故抽时间将这部分知识温习一下。在此记录一下自己的学习进度。好了,直接上代码
首先,先准备hibernate开发要用到的jar包,本次学习中使用的hibernate版本是3.4.0,上图:
准备好要用到的jar包,下面就开始真正的开发,我的开发环境是Eclipse helios 版本。
下一步,让我新建一个java project ,命名为spring_hibernate_day01,上图:
其中,model包下是实体类,还有hibernate映射文件,默认约定映射文件和实体类放在一起。
hibernate.cfg.xml 是hibernate的核心配置文件
test包下用来进行相应的测试。
好,直接上代码
model 类 Teacher:
import java.util.Date; public class Teacher { private int id; private String name; private String address; private Date year; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getYear() { return year; } public void setYear(Date year) { this.year = year; } }
映射文件teacher.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.**.hibernate.model"> <class name="Teacher"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="address"/> <property name="year"/> </class> </hibernate-mapping>
hiberante的核心配置文件,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"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) --> <!--<property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!--<property name="current_session_context_class">thread</property>--> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="cn/***/hibernate/model/teacher.hbm.xml"/> </session-factory> </hibernate-configuration>
好,到此所有的代码都已经完成,下面让我们进行一下测试,看数据是否能保存到数据库里,上代码
TeacherTest:
public class TeacherTest { /** * @param args */ public static void main(String[] args) { Teacher t = new Teacher(); t.setName("name"); t.setAddress("beijing"); t.setYear(new Date()); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); sf.close(); } }
让我们查一下数据库,看数据是否保存到数据库中,上图:
到此,我们就完成了hibernate第一次的学习记录,有需要讨论的童鞋,请给我留言。。。