hibernate学习一
一.项目配置文件
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>cn.hzq.test</groupId>
<artifactId>hibernate</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
2.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>
<!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect-->
<!-- #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect-->
<!-- #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect-->
<!-- #hibernate.connection.driver_class com.mysql.jdbc.Driver-->
<!-- #hibernate.connection.url jdbc:mysql:///test-->
<!-- #hibernate.connection.username gavin-->
<!-- #hibernate.connection.password-->
<!-- mysql驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql地址-->
<property name="connection.url">jdbc:mysql:///demo</property>
<!-- 用户名和密码-->
<property name="connection.username">root</property>
<property name="connection.password">960828</property>
<!-- 方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否将hibernate执行的sql语句打印到控制台#hibernate.show_sql true-->
<property name="show_sql">true</property>
<!-- 是否格式化打印-->
<property name="format_sql">true</property>
<!-- #hibernate.hbm2ddl.auto create-drop-->
<!-- #hibernate.hbm2ddl.auto create-->
<!-- #hibernate.hbm2ddl.auto update-->
<!-- #hibernate.hbm2ddl.auto validate-->
<!-- create-drop和create都只能在测试阶段使用,因为他会先删除表,再创建,会丢失数据-->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource=""></mapping>
</session-factory>
</hibernate-configuration>
3.hibernate-mapping 映射配置
<?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 package="domain">
<class name="User" table="user">
<id name="uid">
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<!-- private String password;-->
<!-- private String name;-->
<!-- private String email;-->
<!-- private Date birthday;-->
<!-- private String sex;-->
<!-- private String remark;//扩展字段-->
<property name="password"></property>
<property name="name"></property>
<property name="email"></property>
<property name="birthday"></property>
<property name="sex"></property>
<property name="remark"></property>
</class>
</hibernate-mapping>
二.API
1.Configuration 对象
-
简介
Configuration 对象是用来加载配置文件的,通过方法
configure()
进行加载,无参的configure方法会自动寻找项目目录中的hibernate.cfg.xml文件,所以名字不可以写错,不然会出错.加载配置文件后,调用buildSessionFactory()
方法依据配置文件创建SessionFactory对象.
-
对象创建及使用
Configuration con = new Configuration(); //对象的创建 con.configure(); //加载配置文件,不传参数会自动寻找项目目录下的hibernate.cfg.xml文件. SessionFactory sessionFactory = Con.buildSessionFactory(); //创建SessionFactory对象
2.SessionFactory 对象
-
简介
该对象顾名思义,是用来创建Session对象的,与J2EE中的Session是不同的.该对象全程只能创建一个,并且该对象消耗资源大,类似于数据库连接池,推荐将其封装成Util.该对象有两个常用方法,都是用来创建Session的,一个为
openSession()
,一个为getCurrentSession()
,区别为在一个线程中,前者多次调用创建的对象不是同一个,而后者多次调用获得的Session对象是同一个. -
对象的创建及使用
SessionFactory sessionFactory = Con.buildSessionFactory(); //创建SessionFactory对象 Session session = sessionFactory.openSession();//创建Session对象 Session session = sessionFactory.getCurrentSession();//创建与线程绑定的Session对象
3.Session 对象
-
简介
Session对象是用来操作数据库的直接对象,用来进行数据库的增删改查.
-
对象的创建及使用
-
通过ID主键查询
Session session = HibernateUtil.openSession();//获得Session对象 Transaction tx = session.beginTransaction();//获得并开启事务 User user = session.get(User.class, "DAE4C350C6B74D11B6EA87C1B1FF897D");//通过ID查询用户 System.out.println(user); tx.commit();//提交事务 session.close();//关闭session
-
插入操作
Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setName("李四"); session.save(user);//插入数据库 tx.commit(); session.close();
-
更新操作
Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setId(1); user.setName("张三"); session.update(user); tx.commit(); session.close();
-
删除操作
Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setId(2); session.delete(user); tx.commit(); session.close();
-
多数据查询
Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); String sql = "select * from user"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(User.class);//放入实体类的Class对象 List<User> list = query.list();//将查询结果转换成list集合 tx.commit(); session.close();
-
条件查询
Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); String sql = "select * from user limit ?,? "; NativeQuery query = session.createSQLQuery(sql); query.setParameter(0,0);//设置第一个?为0 query.setParameter(1,2);//设置第二个?为2 query.addEntity(User.class); List<User> list = query.list(); tx.commit(); session.close();
-