Hibernate连接Mysql数据库的基础配置和操作,实现小型demo完成增删改查功能
1.打开MyEclipse创建一个web Project,导入相应的jar包
2.在src目录下创建hibernate.cfg.xml配置文件信息
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配制文件的dtd信息 -->
<!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.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--数据库驱动 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--连接数据库的url -->
<property name="hibernate.connection.url">
jdbc:mysql:///hibernate?characterEncoding=utf8
</property>
<!--数据库的用户名 -->
<property name="hibernate.connection.username">
root
</property>
<!--数据库的密码 -->
<property name="hibernate.connection.password">
root
</property>
<!-- C3P0连接池设定 -->
<!-- 使用c3po连接池 配置连接池提供的供应商 -->
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以毫秒为单位,
如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,
就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位 -->
<property name="c3p0.idle_test_period">3000</property>
<!--其它配置 -->
<!-- 显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="format_sql">true</property>
<!--自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 用来关联hbm配置文件 -->
<mapping resource="com/ruanyuan/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
其中c3po连接池可以不用配置,其他配置也可以不用写。其他配置主要是在控制台输出的时候编码更规范,看起来更方便。用来关联hbm配置文件路径是一会对象-关系映射文件的地址。
3.创建一个包,在包中创建一个实体类,并为这个实体类编写映射文件Customer.hbm.xml
4.编写测试类,创建Source Folder包
/**
* 1.添加操作
*/
@Test
public void insertTest() {
// 1.加载hibernate.cfg.xml配置
//查找指定位置的配制文件
//Configuration config = new Configuration().configure("/config/hibernate.cfg.xml");
//默认去类路径的根目录下查找名称为hibernate.cfg.xml的文件
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction t = session.beginTransaction();
//session.beginTransaction();
// 5.操作
// 5.1创建一个对象
Customer c = new Customer();
c.setName("王五");
c.setAge(20);
c.setCity("上海");
c.setSex("男");
// 5.2将数据存储到表中
session.save(c);
// 6.提交事务
t.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
System.out.println("success");
}
/**
* 2.修改操作
*/
@Test
public void updateTest() {
// 1.加载hibernate.cfg.xml配置
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction t = session.beginTransaction();
// 5.操作
// 5.1创建一个对象
Customer c = new Customer();
c.setId(1);
c.setName("李四");
c.setAge(20);
c.setSex("男");
c.setCity("广州");
// 5.2将数据存储到表中
session.update(c);
// 6.提交事务
t.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
/**
* 3.查询操作--根据id查找
*/
@Test
public void findByIdTest() {
// 1.加载hibernate.cfg.xml配置
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction t = session.beginTransaction();
// 5.操作
// 对于hibernate中根据id查找操作,它可以使用两个方法 get load
//get加载数据时,如果指定的记录不存在,则返回null,而load则会报ObjectNotFoundException异常
Customer c=(Customer) session.get(Customer.class, 1);
//Customer c = (Customer) session.load(Customer.class, 1);
System.out.println("姓名:"+c.getName());
System.out.println("年龄:"+c.getAge());
System.out.println("性别:"+c.getSex());
System.out.println("所在城市:"+c.getCity());
// 6.提交事务
t.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
/**
* 4.删除操作
*/
@Test
public void deleteByIdTest() {
// 1.加载hibernate.cfg.xml配置
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction t = session.beginTransaction();
// 5.操作
Customer c = (Customer) session.get(Customer.class, 1); // 先查询
session.delete(c);// 删除
// 6.提交事务
t.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
5.打开Mysql创建数据库
CREATE DATABASE hibernate
DEFAULT CHARSET utf8
COLLATE utf8_general_ci;
以上就是hibernate的基本操作。