hibernate
来源:互联网 发布:阿兹特克文明 知乎 编辑:程序博客网 时间:2024/05/29 12:32
版本:4.2.2
配置文件: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"> <!-- Generated by eclipse Hibernate Tools. --><hibernate-configuration> <session-factory> <!--显示执行的SQL语句--> <property name="show_sql">true</property> <!--连接字符串--> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!--连接数据库的用户名--> <property name="connection.username">root</property> <!--数据库用户密码--> <property name="connection.password"></property> <!--数据库驱动--> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!--JDBC连接池(使用内置的连接池)--> <!-- <property name="connection.pool_size">1</property> --> <!--设置Hibernate自动管理上下文的策略--> <!-- <property name="current_session_context_class">thread</property> --> <!--选择使用的方言--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--在启动时删除并重新创建数据库 hibernate mapping data defination language(create/update/validate/create-drop)--> <property name="hbm2ddl.auto">create</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- 配置文件mapping --><!-- <mapping resource="events/Event.hbm.xml"/> <mapping resource="events/Person.hbm.xml"/> --> <!-- 注解mapping --> <mapping class="com.basic.hibernate.model.Student"></mapping> </session-factory></hibernate-configuration>
实体类:
annotation,注解一般加在get方法上,不加在成员变量上
@Entity@Table(name="student")public class Student { private Integer id; private String name; private Integer age; private String t; private Date birthDate; private Title title; @Transient //该属性不存入数据库 public String getT() { return t; } public void setT(String t) { this.t = t; } @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic //没写时 public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Temporal(TemporalType.TIME) //时间精度 value=TemporalType.TIME public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } @Enumerated(EnumType.STRING) //枚举类型 public Title getTitle() { return title; } public void setTitle(Title title) { this.title = title; }}
Junit测试类
加junit jar包, 对象保存
public class StudentTest { private static SessionFactory sf = null; @SuppressWarnings("deprecation") @BeforeClass public static void beforeClass(){ sf = new Configuration().configure().buildSessionFactory(); } @Test public void testStudentSave() { Student s = new Student(); s.setId(1); s.setName("s1"); s.setAge(1); s.setBirthDate(new Date()); s.setTitle(Title.high); Session session = sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); //fail("Not yet implemented"); } @AfterClass public static void afterClass(){ sf.close(); }}
- hibernate
- Hibernate
- Hibernate
- Hibernate
- hibernate
- hibernate
- Hibernate
- Hibernate
- Hibernate
- hibernate
- Hibernate
- Hibernate
- hibernate
- hibernate
- hibernate
- Hibernate
- Hibernate
- hibernate
- ios 之网络知识(转)
- Android 一些不为人知的东西
- 5位循环黑洞数,4位黑洞数在注解中
- HTTP协议详解2
- string 与 string
- hibernate
- 数据库的一个基础问题
- 算法导论c++实现第一章
- Android应用程序(APK)的编译打包过程
- CentOS添加一个新的硬盘分区到挂载的过程
- 嵌入式软件技术的缺陷查找方法
- postgresql 安装状况百出啊。解决方法
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- acm算法基础1 素数环与DFS