hibernate创建构架
创建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); }
}
hibernate创建构架的更多相关文章
- hibernate 创建session
//1. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibe ...
- hibernate 创建工厂类
package cn.hibernate; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; / ...
- Hibernate创建hqll时报错
Hibernate 问题,在执行Query session.createQuery(hql) 报错误 出错截图: 这条语句在java运行环境下,直接连数据库不出错,如果在hiberante,strut ...
- idea用hibernate创建一个表两个主键时遇到的问题
1>> idea功能简单,最大化的实现傻瓜式操作,不需要像eclipse一样手敲代码,尤其是在创建主键多个或者主键映射时. 2>> (1).首先,idea创建复合主键映射时,需 ...
- Hibernate创建SessionFactory实例
private static SessionFactory sessionFactory = null; static { Configuration configuration =new Con ...
- Hibernate入门之创建数据库表
前言 Hibernate 5.1和更早版本至少需要Java 1.6和JDBC 4.0,Hibernate 5.2和更高版本至少需要Java 1.8和JDBC 4.2,从本节开始我们正式进入Hibern ...
- Hibernate —— 检索策略
一.Hibernate 的检索策略本质上是为了优化 Hibernate 性能. 二.Hibernate 检索策略包括类级别的检索策略.和关联级别的检索策略(<set> 元素) 三.类级别的 ...
- Hibernate入门与简谈
Hibernate jdbc Java Databases Connectivity, 他是提供了一组Java API来访问关系数据库的Java程序.这些Java API 可以使Java应用程序执行S ...
- Hibernate 开发流程
Hibernate内部分装的技术:JDBC(Java Data Base Connectivity), JTA(Java Transaction API) , JNDI(Java Naming and ...
随机推荐
- python调用虹软2.0
第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中 第三版已发出 https://www.cnblogs.com/wxt51/p/10125460. ...
- linux c/c++ 获取文件大小
linux c/c++ 获取文件大小 #include <sys/stat.h> int FileSize(const char* fname) { struct stat statbuf ...
- Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...
- boke练习: @PreAuthorize authentication.name.equals的数据绑定与验证
boke练习: @PreAuthorize authentication.name.equals的数据绑定与验证 先看2个简单的例子: 一 @PostMapping("/{username} ...
- android-------Android Studio使用MAT分析工具遇到的错误
今天主要介绍一下我使用MAT工具分析文件时遇到的一个错误 Error opening heap dump 'a.hprof'. Check the error log for further deta ...
- 非常可乐 HDU - 1495
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但see ...
- linux安装curl扩展
1.获得安装包,从网上直接下载或者其他途径,这里直接wget wget http://curl.haxx.se/download/curl-7.20.0.tar.gz 2.解压到当前目录(或者 htt ...
- [contest 782] 9.7
[contest 782] 9.7 - XJOI 个人觉得温暖题啊,,,可是卡毛空间呀!!! T1 传送
- Humble Numbers HDU - 1058
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...
- ForkJoin
Java Fork/Join 框架 jdk1.8-ForkJoin框架剖析 Java的Fork/Join任务,你写对了吗? 概述 从JDK1.7开始,Java提供Fork/Join框架用于并行执行任务 ...