创建hibernate架构:
注意:需要将所需的架包导进去:
二:Java工程的具体结构:
具体代码如下:hibernate.cfg.xml
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 控制台打印sql语句 测试 --> <property name="show_sql">true</property> <!-- 引入映射文件,这是domain中绑定的employee.hbm.xml路径 --> <mapping resource="demo/test/domain/employee.hbm.xml"/> </session-factory> </hibernate-configuration>
模板方法的接口:
package demo.test.dao; import java.util.List; import demo.test.domain.Employee; public interface IEmployee { public void save(Employee emp); public void delete(Employee employ); public void update(Employee emp); List<Employee> list(); public Employee get(Employee emp); }
实现类:
package demo.test.dao.impl; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import demo.test.dao.IEmployee; import demo.test.domain.Employee; import demo.test.utiltool.UtilTool; public class ImplEmployee implements IEmployee { @SuppressWarnings("deprecation") @Override public void save(Employee emp) { Session session = UtilTool.getSession(); // Configuration configuration = new // Configuration().configure();//配置对象Configuration主要靠config加载解析配置文件 // Session session = configuration.buildSessionFactory().openSession(); session.beginTransaction();//加了事物,是出于安全性考虑. session.save(emp); session.getTransaction().commit();//加了事物,是出于安全性考虑.
session.close(); } @Override public void delete(Employee emp) { Session session = UtilTool.getSession(); session.beginTransaction(); session.delete(emp); session.getTransaction().commit(); session.close(); } @Override public void update(Employee emp) { Session session = UtilTool.getSession(); session.beginTransaction(); session.update(emp); session.getTransaction().commit(); session.close(); } @Override public List<Employee> list() { Session session = UtilTool.getSession(); Query query = session.createQuery("SELECT emp FROM Employee emp"); @SuppressWarnings("unchecked") List<Employee> list = query.list(); return list; } @Override public Employee get(Employee emp) { Session session = UtilTool.getSession(); session.beginTransaction(); Employee emplo = (Employee) session.get(emp.getClass(), emp.getId()); session.close(); return emplo; } }
Javabean规范:
package demo.test.domain; //javabean规范:①必须是一个公共类②类变量都为private私有③空参构造方法④getter和setter方法 public class Employee { private Integer id; private String name; private Integer age; public Employee() {} public Employee(String name, Integer age, Integer id) { this.name = name; this.age = age; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Employee(Integer id) { this.id = id; } public Employee(String name, Integer age) { this.name = name; this.age = age; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
javabean规范中的类所对应的.xml文件:跟随Employee
最终要将此文件部署到hibernate.cfg.xml的mapping resource中
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="demo.test.domain"> <class name="Employee" table="t_employee"> <!-- 对象模型名 和 表名 --> <id name="id" column="e_id"> <generator class="native" /> <!-- 根据本地设置主键 --> </id> <property name="name" column="e_name"/> <property name="age" column="e_age"/> </class> </hibernate-mapping>
工具类:将冗余部分抽出,作为公共部分供其他方法调用
package demo.test.utiltool; import org.hibernate.Session; import org.hibernate.cfg.Configuration; public class UtilTool { public static Session getSession(){ //配置对象Configuration主要靠config加载解析配置文件 Session session = new Configuration().configure().buildSessionFactory().openSession(); return session; } }
测试类:
package demo.test.utiltest; import org.junit.Test; import demo.test.dao.IEmployee; import demo.test.dao.impl.ImplEmployee; import demo.test.domain.Employee; //测试类 public class TestClass { IEmployee employ; public TestClass() { employ = new ImplEmployee(); } @Test public void testSave() { IEmployee employee = new ImplEmployee(); Employee employ = new Employee("sdsa", 112); employee.save(employ); } @Test public void testdelete() { IEmployee employee = new ImplEmployee(); Employee employ = new Employee(5); employee.delete(employ); } @Test public void testupdate() { IEmployee employee = new ImplEmployee(); Employee employ = new Employee("張三",12,6); employee.update(employ); } @Test public void testlist() { System.out.println(employ.list()); } @Test public void testget() { IEmployee employee = new ImplEmployee(); Employee employ = new Employee(6); Employee empl = employee.get(employ); System.out.println(empl); } }