在MySQL中创建数据库hibernate和表customers
create database hibernate;
use hibernate;
create table customers(
id int primary key,
name varchar(20),
age int ,
birthday date,
married int,
photo longblob,
description text
)
用hibernate.properties文件进行配置
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect #方言
hibernate.show_sql=true #是否显示sql语句
Customer类
package com.xxc.domain;
import java.sql.Date;
public class Customer {
private Integer id;
private String name;
private Integer age;
private Date birthday;
private boolean married;
private byte[] photo;
private String description;
private String fName;
private String lName;
/*
* 首先要明白一点:这个get和set方法是由hibernate调用的,给sql赋值时候调用get方法从domain中取值
* 从数据库查询出结果后,给domain设值时调用set方法
* 需求:如果数据库中只有name字段,而数据库对应的domain中有两个字段fName和lName。fName+","+lName=name
* 前台传递过来fName和Lname,数据回显的时候也要分别显示fName和lName,所以在此domain里光是设置name属性是满足不了需求的
* 这时我们可以在getName和setName的方法中进行业务逻辑处理
*
* 注:由于Customer.hbm.xml中字段的默认访问级别为property(方法级),所以在此domain中可以不需要设置name属性,只需设置getName和setName即可。且权限为private,不需要程序员调用。
*/
//因为在往数据库添加数据的时候会调用此方法,那么就在此方法中添加将fName+lName操作
private String getName() {
if((fName!=null && !"".equals(fName)) && (lName!=null && !"".equals(lName))){
return fName+","+lName;
}
return null;
}
//因为此操作是从数据库查询出结果并往对象中设值的时候用的,所以可以在此方法中加入分割name操作,分割成fName和lName
private void setName(String name) {
if(name!=null && !"".equals(name)){
String[] temp = name.split(",");
if(temp!=null && temp.length>1){
this.fName = temp[0];
this.lName = temp[1];
}
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
配置domain和表的映射文件,一般这个映射文件和domain放在一个包下
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 此文件要和映射的类在一起 -->
<hibernate-mapping>
<class name="com.xxc.domain.Customer" table="customers" lazy="false"><!-- lazy是否进行懒加载 -->
<!-- type:映射类型,不是java也不是sql类型,是java类型到sql类型的转换 org.hibernate.type包下可以看出此结论
name表示类中的属性名(区分大小写) column表示表中列名(不区分大小写) -->
<id name="id" column="id" type="integer">
<!-- 指定主键生成策略 -->
<generator class="increment" />
</id>
<property name="name" column="name" type="string" access="property"></property><!-- type必须小写 access表示属性访问级别,默认是property方法级,field是属性级-->
<property name="age" column="age" type="integer"></property>
<property name="birthday" column="birthday" type="date"></property>
<property name="married" column="married" type="boolean"></property>
<property name="photo" column="photo" type="binary"></property>
<property name="description" column="description" type="text"></property>
</class>
</hibernate-mapping>
测试类(这里测试的时候不要再setName了,因为前边为了演示在get和set中可以进行业务逻辑的操作,将name的get和set都private了)
package com.xxc.app;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.xxc.domain.Customer;
public class Test {
private static SessionFactory sf= null;
static{
//加载src/hibernate.properties文件
Configuration config = new Configuration();
//当类的配置文件和类不在一个包下时,这时config.addClass(Customer.class);也不需要了
//config.addResource("com/xxc/app/Customer.hbm.xml");
config.addClass(Customer.class);
//创建Session工厂。(将配置文件信息加载到SessionFactory缓存中(sessionFactory缓存既不是一级要不是二级,映射对象组成的sql语句,事先生成好了还有元数据))
sf = config.buildSessionFactory();
}
public static void addCustomer(Customer c){
Session session = sf.openSession();
Transaction t = session.beginTransaction();
session.save(c);
t.commit();
session.close();
}
/*
* 查询并不是用find方法,Hibernate3.0也没有提供这样的方法,2.0的时候有3.0去掉了,因为功能没有Query接口强大
* 查询一个
*/
public static Customer loadCustomer(Integer id){
Session session = sf.openSession();
Transaction t = session.beginTransaction();
Customer c = (Customer)session.load(Customer.class, id);
t.commit();
session.close();
return c;
}
/*
* 更新,按照id更新
*/
public static void updateCustomer(Customer c){
Session session = sf.openSession();
Transaction t = session.beginTransaction();
session.update(c);
t.commit();
session.close();
}
/*
* 查询所有
*/
public static List findAll(){
Session session = sf.openSession();
Transaction t = session.beginTransaction();
Query q = session.createQuery("from Customer");
List<Customer> list = q.list();
t.commit();
session.close();
return list;
}
/*
* 删除客户,只删除指定某一个
*/
public static void deleteCustomer(Customer c){
Session session = sf.openSession();
Transaction t = session.beginTransaction();
session.delete(c);
t.commit();
session.close();
}
/*
* 批量删除
*/
public static void deleteAllCustomer(){
Session session = sf.openSession();
Transaction t = session.beginTransaction();
Query q = session.createQuery("delete from Customer c where c.id>1");
q.executeUpdate();
t.commit();
session.close();
}
}