Java笔记之hibernate(一):构建项目
1.Eclipse上新建Java Project项目hibernate_demo
2.src同级目录下新建lib文件夹,导入jar包,并Build Path
3.src目录下新建hibernate.cfg.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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/keeper?characterEncoding=utf-8</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- MySQL方言配置 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/hibernate/demo/bean/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
4.新建com.hibernate.demo.bean包,并在其中新建Employee类和Employee.hbm.xml文件
Employee类
package com.hibernate.demo.bean; public class Employee { private Integer empId; private String empName; public Employee() { } public Employee(Integer empId, String empName) { super(); this.empId = empId; this.empName = empName; } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } @Override public String toString() { return "Employee [empId=" + empId + ", empName=" + empName + "]"; } }
Employee.hbm.xml
<?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 package="com.hibernate.demo.bean"> <!-- 映射Employee类与t_employee表 --> <class name="Employee" table="t_employee"> <id name="empId" column="emp_id"> <!-- 声明主键生成策略为自增 --> <generator class="native"></generator> </id> <property name="empName" column="emp_name"></property> </class> </hibernate-mapping>
5.新建com.hibernate.demo.test包,并在其中新建Test1类
package com.hibernate.demo.test; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.hibernate.demo.bean.Employee; public class Test1 { public static void main(String[] args) { //加载配置文件,构建会话工厂对象 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //获取会话对象 Session session = sessionFactory.openSession(); //根据类名和主键值获取对象 Employee employee = (Employee) session.get(Employee.class, 1); System.out.println(employee.getEmpName()); } }
6.运行Test1类
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from t_employee employee0_ where employee0_.emp_id=? 张三