1.在classpath(resources)下配置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">123456</property><!--密码-->
<property name="connection.url">jdbc:mysql://localhost:3306/shop?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>
<!-- 3. 添加实体映射文件 -->
<mapping resource="/pojo/customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.在pojo/entity下配置customer.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>
<!--
clas标签下的属性:
name:数据库中的表映射的类的全路径名
table:要映射的表名
id标签:用于标识主键的
name:数据库表的列段映射到实体类中的属性名
type:属性的类型
column:数据库表中的列段
property标签:
name:数据库表的列段映射到实体类中的属性名
type:属性的类型
column:数据库表中的列段
-->
<class name="pojo.Customer" table="customer">
<id name="cid" type="java.lang.Integer" column="cid">
<generator class="increment" />
</id>
<property name="username" type="java.lang.String" column="username">
</property>
<property name="password" type="java.lang.String" column="password">
</property>
<property name="sex" type="java.lang.String" column="sex">
</property>
<property name="addr" type="java.lang.String" column="addr">
</property>
</class>
</hibernate-mapping>
3.创建实体类Customer
package pojo;
public class Customer {
/*
cid varchar(10) NOT NULL
username varchar(15) NULL
password varchar(20) NULL
sex varchar(10) NULL
addr varchar(20) NULL*/
private Integer cid;
private String username;
private String password;
private String sex;
private String addr;
@Override
public String toString() {
return "Customer{" +
"cid='" + cid + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", addr='" + addr + '\'' +
'}';
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
整体结构如图
4.crud操作
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import pojo.Customer;
import java.util.List;
public class TestCustomer {
@Test
public void addCustomer(){ //添加客户
/*
* 1,对hibernate.cfg.xml核心配置文件进行建模
* 2,建模后的对象获取sessionfactory对象,sessionfactory对象中包含了数据库的相关信息
* 3,通过sessionfactory对象获取session会话
* 4,session开启事务
* 5,操作数据库
* 6,提交事务(查询中不需要)
* 7,释放资源
*/
Configuration configure=new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory=configure.buildSessionFactory();
Session session=sessionFactory .openSession();
Transaction transaction=session.beginTransaction();
// User user=new User(16, "a1", "123123", "xxxx", "男",new Date(System.currentTimeMillis()) ,
// new Timestamp(System.currentTimeMillis()), "lalalal");
Customer customer = new Customer();
customer.setCid(4);
customer.setUsername("环境");
customer.setPassword("1656");
customer.setSex("男");
customer.setAddr("回到家阿江苏省");
System.out.println(customer);
session.save(customer);
transaction.commit();
session.close();
}
@Test
public void deleteCustomer(){ //删除
/*/*
* 1,对hibernate.cfg.xml核心配置文件进行建模
* 2,建模后的对象获取sessionfactory对象,sessionfactory对象中包含了数据库的相关信息
* 3,通过sessionfactory对象获取session会话
* 4,session开启事务
* 5,操作数据库
* 6,提交事务(查询中不需要)
* 7,释放资源
*/
Configuration configure=new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory=configure.buildSessionFactory();
Session session=sessionFactory .openSession();
Transaction transaction = session.beginTransaction();
//操作数据库
//Customer customer = new Customer();
Customer customer = session.get(Customer.class, 5);
//System.out.println(customer);
session.delete(customer);
transaction.commit();
session.close();
}
@Test
public void updateCustomer(){ //修改
/*/*
* 1,对hibernate.cfg.xml核心配置文件进行建模
* 2,建模后的对象获取sessionfactory对象,sessionfactory对象中包含了数据库的相关信息
* 3,通过sessionfactory对象获取session会话
* 4,session开启事务
* 5,操作数据库
* 6,提交事务(查询中不需要)
* 7,释放资源
*/
Configuration configure=new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory=configure.buildSessionFactory();
Session session=sessionFactory .openSession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class,4);
customer.setUsername("斯柯达");
session.update(customer);
transaction.commit();
session.close();
}
@Test
public void selectCustomer(){
/*/*
* 1,对hibernate.cfg.xml核心配置文件进行建模
* 2,建模后的对象获取sessionfactory对象,sessionfactory对象中包含了数据库的相关信息
* 3,通过sessionfactory对象获取session会话
* 4,session开启事务
* 5,操作数据库
* 6,提交事务(查询中不需要)
* 7,释放资源
*/
Configuration configure=new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory=configure.buildSessionFactory();
Session session=sessionFactory .openSession();
//查询不需提交事务
// Transaction transaction = session.beginTransaction();
List list = session.createQuery("from Customer ").list();
for (Object c:list){
System.out.println(c);
}
session.close();
}
}