hibernate--入门
hibernate概念:
是一个轻量级,企业级,开源的ORM持久层的框架,是可以操作数据库的框架。
轻量级:使用时依赖的资源很少。目前仅依赖log4j和c3p0连接池
企业级:指在企业级应用中比较多的
开源:开放源代码。
ORM的操作方式:建立对象关系映射,实现操作实体类就相当于操作数据库表。
通常情况下,软件工程的持久层解决方案,一个为主,一个为辅,两者并存(写SQL语句和不写SQL语句)。
CRM:
客户关系管理,利用相应的信息技术以及互联网技术来协调企业与客户间在销售,营销和服务上的交互。
包括:客户信息管理,联系人管理,商机管理,统计分析管理,综合查询,系统管理等。
hibernate开发环境的搭建:
MAVEN工程,IDEA工具
1.pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ruikang</groupId> <artifactId>test01_hibernate01</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 添加Hibernate依赖 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <!-- 添加Log4J依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.6.4</version> </dependency> <!-- 添加javassist --> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.0.GA</version> </dependency> <!-- mysql数据库的驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> </project>
2.hibernate全局配置文件
<?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> <!--SessionFactory用来创建Session对象,Session对象时操作数据库的核心--> <!--SessionFactory必要部分:1.连接数据库信息;2.hibernate的可选配置;3.映射文件配置--> <session-factory> <!-- 1.连接数据库的基本信息 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mybatis</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 2.hibernate的可选配置 --> <!-- hibernate里的配置,数据库使用的方言,开发过程中要在控制台显示,sql --> <!--<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- c3p0的配置 --> <!-- hibernate.connection.provider_class使用c3p0数据库连接池的一个配置 --> <!-- <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> <!–当具体连接不够用时,申请连接的个数 –> <property name="hibernate.c3p0.acquire_increment">20</property> <!– 设置多久扫描一次连接是否超时 –> <property name="hibernate.c3p0.idle_test_period">2000</property> <!– 指定连接池最大里最大连接数 –> <property name="hibernate.c3p0.max_size">20</property> <!– 指定连接池最大缓存多少个statements对象 –> <property name="hibernate.c3p0.max_statements">10</property> <!– 指定连接池最大里最小连接数 –> <property name="hibernate.c3p0.min_size">5</property> <!– 指定连接池连接的超时时长 –> <property name="hibernate.c3p0.timeout">2000</property>--> <!--3.映射文件配置--> <!-- ORM映射关系 ,导入相应模型的绝对路径--> <mapping resource="custormer.hbm.xml"/> </session-factory> </hibernate-configuration>
3.实体类映射文件的配置
<?xml version="1.0" encoding="utf-8" ?> <!--导入dbd约束--> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bean"> <class name="Customer" table="customer"> <id name="id" column="id"> <generator class="native"></generator> <!--主键自增--> </id> <property name="name" column="name"></property> <property name="lovel" column="lovel"></property> </class> </hibernate-mapping>
4.获取session工具类
package com.utils; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; public class HibernateUtils { private static SessionFactory factory; static{ try { Configuration configuration=new Configuration(); configuration.configure("hibernate.cmd.xml"); factory = configuration.buildSessionFactory(); }catch (ExceptionInInitializerError e){ throw new ExceptionInInitializerError("初始化SessionFactory失败,请检查配置文件"); } } public static Session getSession(){ return factory.openSession(); } }
5.测试
import com.utils.HibernateUtils; import org.hibernate.SQLQuery; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; import java.util.List; public class CustomerTestDemo02 { @Test public void saveTest(){ Customer customer = new Customer("文丑", "吃喝"); //1.使用工具获取Session Session session = HibernateUtils.getSession(); //2.打开事务 Transaction tx = session.beginTransaction(); //3.操作 session.save(customer); //4.提交事务 tx.commit(); //5.释放资源 session.close(); } @Test public void selectOneTest(){ Customer customer = new Customer("文丑", "吃喝"); //1.使用工具获取Session Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); Object o = session.get(Customer.class, 1); System.out.println(o); session.close(); } @Test public void selectAllTest(){ //1.使用工具获取Session Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); SQLQuery sqlQuery = session.createSQLQuery("select * from customer"); List<Object[]> list = sqlQuery.list(); for (Object[] o : list) { for (Object o1 : o) { System.out.println(o1); } } session.close(); } @Test public void updateTest(){ //1.使用工具获取Session Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); Customer o = (Customer)session.get (Customer.class, 1); o.setLovel("做县官"); session.update(o); tx.commit(); session.close(); } @Test public void deleteTest(){ //1.使用工具获取Session Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); Customer customer = new Customer(); customer.setId(3); session.delete(customer); tx.commit(); session.close(); } }