星期二, 十二月 29, 2015 21:20:00
使用注解的方式进行操作hibernate定义实体
步骤:
1.引入hibernate-annotations.jar
"hibernate-commons-annotations.jar"
"ejb3-persistence.jar"
2.建一个Teacher类
3.在数据库中建teacher表
create table teacher(id int primary key,name varchar(20),title varchar(20));
4.在Teacher加入注解:@Entity //主键@Id
/*1.实体类(使用annotation进行实现,告诉hibernate是实体类,
* 不需要再建立Teacher.hbm.xml文件进行映射)
* 2.hibernate.cfg.xml进行配置即可
* 3.建立一个TeacherTest
*/
5.在hibernate.cfg.xml进行配置
<mapping class="com.zhuhw.hibernate.Teacher"/>
6.建立TeacherTest类
//因为使用的annotation,所以Configuration要使用AnnotationConfiguration
Configuration cf = new AnnotationConfiguration();
运行即可
代码案例:
package com.zhuhw.hibernate; import javax.persistence.Entity; import javax.persistence.Id; /*1.实体类(使用annotation进行实现,告诉hibernate是实体类, * 不需要再建立Teacher.hbm.xml文件进行映射) * 2.hibernate.cfg.xml进行配置即可 * 3.建立一个TeacherTest */ @Entity public class Teacher { private int id; private String name; private String title; //主键 @Id 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 getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
<?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/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</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>--> <!-- hibernate去哪里找这个配置文件 --> <mapping resource="com/zhuhw/hibernate/Student.hbm.xml"/> <mapping class="com.zhuhw.hibernate.Teacher"/> </session-factory> </hibernate-configuration>
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import com.zhuhw.hibernate.Student; import com.zhuhw.hibernate.Teacher; public class TeacherTest { public static void main(String[] args){ Teacher t = new Teacher(); t.setId(2); t.setName("yuzhou"); t.setTitle("aaaa"); //因为使用的annotation,所以Configuration要使用AnnotationConfiguration Configuration cf = new AnnotationConfiguration(); SessionFactory sf = cf.configure().buildSessionFactory(); Session session = sf.openSession(); //在hibernate中执行操作要在一个事务里面 session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); sf.close(); } }
运行结果:
Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)
总结:
1.请务必建立自己动手查一手文档的能力
2.重要的是
1.要建立了自己动手查一手文档的信心
2.还有建立自己动手查一手文档的习惯
3.主动学习,放弃被动接受灌输的习惯
星期二, 十二月 29, 2015 22:10:48