标签:16px cee char his provided 默认 pom username war
今天分享的是hibernate关系映射框架的入门用法
一:Hibernate简介
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的JaveEE架构中取代CMP,完成数据持久化的重任。
二:Hibernate特点
三:在项目中添加hibernate支持
3.1 添加相关依赖
pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ht</groupId> <artifactId>T224_hidbernate</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>T224_hidbernate Maven Webapp</name> <url>http://maven.apache.org</url> <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> <junit.version>4.12</junit.version> <servlet.version>4.0.0</servlet.version> <hibernate.version>5.3.0.Final</hibernate.version> <mysql.driver.version>5.1.46</mysql.driver.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.driver.version}</version> </dependency> </dependencies> <build> <finalName>T224_hidbernate</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> </project>
3.2 在resource目录下添加hibernate.ht.xml(核心配置文件)
3.3 添加DTD支持、Hibernate的配置(数据库连接有关)
首先我们要找到hibernate-configuration-3.0.dtd文件
=====》》
打开文件找到这一串代码赋值进xml中
hibernate.ht.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">root</property> <!-- 数据库密码 --> <property name="connection.url">jdbc:mysql://localhost:3306/ht?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="com/ht/one/entity/User.ht.xml" /> </session-factory> </hibernate-configuration>
四:使用hibernate完成CRUD操作
创建一个实体类User类
User类
package com.ht.one.entity; /** * 用户实体类 * @author Administrator * */ import java.sql.Timestamp; public class User { private int user_id; private String user_name; private String user_pwd; private int user_type; public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_pwd() { return user_pwd; } public void setUser_pwd(String user_pwd) { this.user_pwd = user_pwd; } public int getUser_type() { return user_type; } public void setUser_type(int user_type) { this.user_type = user_type; } public User(int user_id, String user_name, String user_pwd, int user_type) { this.user_id = user_id; this.user_name = user_name; this.user_pwd = user_pwd; this.user_type = user_type; } public User() { } public User(String user_name, String user_pwd, int user_type) { this.user_name = user_name; this.user_pwd = user_pwd; this.user_type = user_type; } }
创建一个User.ht.xml文件去映射对应的实体类
跟上面一样的找法,给xml配置hibernate-mapping-3.0.dtd文件
======》》》
User.ht.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 name:对应的是同目录下的实体类路径 table:对应的数据库中你要操作的表名 Id+property name:对应的是实体类中的列段名 type:对应的列段名的属性类型 --> <class name="com.ht.one.entity.User" table="t_xm_user"> <id name="id" type="java.lang.Integer" column="id"> <generator class="increment" /> </id> <property name="user_id" type="java.lang.Integer" column="user_id"> </property> <property name="user_name" type="java.lang.String" column="user_name"> </property> <property name="user_pwd" type="java.lang.String" column="user_pwd"> </property> <property name="user_type" type="java.lang.Integer" column="user_type"> </property> </class> </hibernate-mapping>
单独创建一个包去写相关的测试类
QueryDemo类 查询
package com.ht.one.demo; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * hibernate查询演示 * @author Administrator * */ public class QueryDemo { public static void main(String[] args) { Configuration configur =new Configuration().configure("/hibernate.ht.xml"); SessionFactory sessionFactory =configur.buildSessionFactory(); //会话,而这里的会话指的是操作数据库的连接 Session session =sessionFactory.openSession(); List list =session.createCriteria("form User").list(); for (Object object : list) { System.out.println(object); } session.close(); } }
InsertDemo类 新增
package com.ht.one.demo; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.ht.one.entity.User; /** * hibernate新增演示 * @author Administrator * */ public class InsertDemo { public static void main(String[] args) { Configuration configur =new Configuration().configure("/hibernate.ht.xml"); SessionFactory sessionFactory =configur.buildSessionFactory(); //会话,而这里的会话指的是操作数据库的连接 Session session =sessionFactory.openSession(); Transaction transaction=session.beginTransaction(); session.save(new User("哈哈哈", "123456", 3)); transaction.commit(); session.close(); } }
数据库中效果图:
UpdateDemo类 修改
package com.ht.one.demo; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.ht.one.entity.User; /** * hibernate修改演示 * @author Administrator * */ public class UpdateDemo { public static void main(String[] args) { Configuration configur =new Configuration().configure("/hibernate.ht.xml"); SessionFactory sessionFactory =configur.buildSessionFactory(); //会话,而这里的会话指的是操作数据库的连接 Session session =sessionFactory.openSession(); Transaction transaction=session.beginTransaction(); User user=session.get(User.class,3); //设置修改的值 user.setUser_name("青灯"); user.setUser_type(4); transaction.commit(); session.close(); } }
数据库效果图:
DeleteDemo类 删除
package com.ht.one.demo; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.ht.one.entity.User; /** * hibernate删除演示 * @author Administrator * */ public class DeleteDemo { public static void main(String[] args) { Configuration configur =new Configuration().configure("/hibernate.ht.xml"); SessionFactory sessionFactory =configur.buildSessionFactory(); //会话,而这里的会话指的是操作数据库的连接 Session session =sessionFactory.openSession(); Transaction transaction=session.beginTransaction(); User user=new User(); user.setUser_id(4); session.delete(user); transaction.commit(); session.close(); } }
效果图:
注意事项
hibernate默认使用的是手动事务,因此必须显示的开启和提交事务
删除操作时,必须先查再删
Hibernate的管理对象的三种状态运行图解
今日的分享到此结束!!谢谢观看!!
标签:16px cee char his provided 默认 pom username war
原文地址:https://www.cnblogs.com/huangting/p/11181804.html