一:Maven1
1. 如何使用hibernate完成CRUD操作
1.1 CRUD操作步骤
1.1.1 读取配置 new confignation().confige(“hibernata.hgb.xml”);
1.1.2 创建SessionFactory cfg.buildSession();
1.1.3 打开Session SessionFactory.opensess();
1.1.4 开启事务 session.begintrsn();
1.1.5 CURD
1.1.6 提交事务/回滚事务 tran.commit();
1.1.7 关闭Session session.close();
1.2 注意事项
1.2.1 hibernate默认使用的是手动事务,因此必须显示的开启和提交事务
1.2.2 删除操作时,必须先查再删
1、hibernate配置文件(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>
<!-- 1. 数据库相关 -->
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置本地事务(No CurrentSessionContext configured!) -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 2. 调试相关 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- hibernate需要管理的数据库表对应的实体类映射文件 -->
<mapping resource="com/zking/test/entity/Clazz.hbm.xml"></mapping>
<mapping resource="com/zking/test/entity/Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
2.映射文件:(Student.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>
<class table="t_struts_student" name="com.zking.test.entity.Student">
<!--
name:实体类的属性
type:实体类的属性类型
column:数据库表的主键列
-->
<id name="sid" type="java.lang.Integer" column="sid">
</id>
<!--
id标签打头,代表配置的是数据库主键与实体类的关系,其他property配置的是数据库表的普通列
-->
<property name="cid" type="java.lang.Integer" column="cid"></property>
<property name="sname" type="java.lang.String" column="sname"></property>
<property name="spin" type="java.lang.String" column="spin"></property>
<property name="sex" type="java.lang.String" column="sex"></property>
<property name="filename" type="java.lang.String" column="filename"></property>
<property name="filetype" type="java.lang.String" column="filetype"></property>
<property name="mark" type="java.lang.String" column="mark"></property>
</class>
</hibernate-mapping>
3.student.java类
package com.zking.test.entity;
import java.io.Serializable;
import com.zking.test.util.PinYinUtil;
public class Student {
private Integer sid;
private String sname;
private String spin;
// radio,checkbox,select
private String sex;
private String mark;
private Integer cid;
private String filetype;
private String filename;
private String cname;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
this.spin = PinYinUtil.toPinyin(this.sname);
}
public String getSpin() {
return spin;
}
public void setSpin(String spin) {
this.spin = spin;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getFiletype() {
return filetype;
}
public void setFiletype(String filetype) {
this.filetype = filetype;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid) {
super();
this.sid = sid;
this.sname = sname;
this.spin = spin;
this.sex = sex;
this.mark = mark;
this.cid = cid;
}
public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid, String filetype,
String filename, String cname) {
super();
this.sid = sid;
this.sname = sname;
this.spin = spin;
this.sex = sex;
this.mark = mark;
this.cid = cid;
this.filetype = filetype;
this.filename = filename;
this.cname = cname;
}
public Student() {
super();
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid, String cname) {
super();
this.sid = sid;
this.sname = sname;
this.spin = spin;
this.sex = sex;
this.mark = mark;
this.cid = cid;
this.cname = cname;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", spin=" + spin + ", sex=" + sex + ", mark=" + mark
+ ", cid=" + cid + ", filetype=" + filetype + ", filename=" + filename + ", cname=" + cname + "]";
}
}
注: 为避免以后可能遇到的问题和麻烦,强烈要求实体类属性和类型与数据库表中字段名称和类型保持一致。实体类中的类型用包装类的类型
4.dao方法
package com.zking.test.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zking.test.entity.Student;
import com.zking.test.util.EntityBaseDao;
import com.zking.test.util.PageBean;
import com.zking.test.util.StringUtils;
public class StudentDAO extends EntityBaseDao<Student> {
/**
* 增加
* @param student
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
*
* @throws SecurityException
* @throws SQLException
*/
public void add(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(student);
transaction.commit();
session.close();
}
/**
* 修改
* @param student
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws SQLException
*/
public void edit(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.update(student);
transaction.commit();
session.close();
}
/**
* 删除
* @param student
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws SQLException
*/
public void del(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.delete(student);
transaction.commit();
session.close();
}
/**
* 单个查
* @param student
* @return
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public Student load(Student student) throws InstantiationException, IllegalAccessException, SQLException {
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.get(Student.class, null);
transaction.commit();
session.close();
return student;
}
/**
* 查所有
* @param student
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Student> list(Student student, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select s.*, c.cname from t_struts_student s inner join t_struts_class c on c.cid = s.cid where true ";
if (StringUtils.isNotBlank(student.getSname())) {
sql += " and (s.sname like '%" + student.getSname().trim()
+ "%' or s.spin like '%" + student.getSname().trim() + "%')";
}
if (null != student.getCid() && !new Integer(-1).equals(student.getCid())) {
sql += " and s.cid = " + student.getCid();
}
System.out.println(sql);
return super.executeQuery(sql, pageBean, Student.class);
}
}