专注Java教育13年 全国咨询/投诉热线:444-1124-454

首页 hot资讯 Hibernate使用步骤

Hibernate使用步骤

更新时间:2022-06-22 10:59:45 来源:赢咖4 浏览18次

Hibernate使用步骤

1.引入框架依赖

<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.liyingdong</groupId>
  <artifactId>Hibernate</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Hibernate Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  	<servlet.version>4.0.1</servlet.version>
  	<junit.version>3.8.1</junit.version>
  	<mysql.version>5.1.44</mysql.version>
  	<jstl.version>1.2</jstl.version>
  	<taglibs.version>1.1.2</taglibs.version>
  	<tomcat.version>8.5.53</tomcat.version>
  	<hibernate.version>5.2.12.Final</hibernate.version>
  </properties>  
  <dependencies>
   <!-- junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <!-- servlet -->
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>${servlet.version}</version>
	    <scope>provided</scope>
	</dependency>		
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<!-- Mysql -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>${mysql.version}</version>
	</dependency>	
	<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
	<!-- jstl -->
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>jstl</artifactId>
	    <version>${jstl.version}</version>
	</dependency>	
	<!-- https://mvnrepository.com/artifact/taglibs/standard -->
	<!-- taglibs -->
	<dependency>
	    <groupId>taglibs</groupId>
	    <artifactId>standard</artifactId>
	    <version>${taglibs.version}</version>
	</dependency>	
	<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jsp-api -->
	<!-- tomcat -->
	<dependency>
	    <groupId>org.apache.tomcat</groupId>
	    <artifactId>tomcat-jsp-api</artifactId>
	    <version>${tomcat.version}</version>
	</dependency>	
	<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
	<!-- hibernate -->
	<dependency>
	    <groupId>org.hibernate</groupId>
	    <artifactId>hibernate-core</artifactId>
	    <version>${hibernate.version}</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>Hibernate</finalName>
    <plugins>
    		<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>    
    </plugins>
  </build>
</project>

2.主配置文件hibernate.cfg.xml

用于数据库的连接配置代替了之前的util中的连接类。

创建后引入dtd约束

复制这一段等会放入到核心配置文件中

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>
		<!-- 数据库相关配置 -->
		<!-- connection.username|connection.password|connection.url|connection.driver_class|dialect -->
			<!-- 连接数据库账户名称 -->
			<property name="connection.username">root</property>
			<!-- 连接数据库密码(我的数据库没有登录密码,直接不用写) -->
			<property name="connection.password">123</property>
			<!-- 连接的绝对路径(使用&需要解译&amp;) -->
			<property name="connection.url">
				jdbc:mysql://localhost:3306/mysql?useUnicode=true&amp;characterEncoding=UTF-8&amp;userSSL=false
			</property>
			<!-- 驱动的绝对路径 -->
			<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
			<!-- 数据库方言配置 -->
			<property name="dialect">
				org.hibernate.dialect.MySQLDialect
			</property>
			<!-- 配置本地事务 -->
			<property name="hibernate.current_session_context_class">thread</property>
		<!-- 调试相关配置 -->
		<!-- hibernate运行过程是否展示sql命令代码(自动生成) -->
			<property name="show_sql">true</property>
			<!-- 是否规范输出sql代码 -->
			<property name="format_sql">true</property>
		<!-- 实体映射相关配置 -->
		<mapping resource="com/liyingdong/entity/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

子配置文件

对应的你是实体类,有多少个实体对应多少个子配置文件。

注意要在配置文件中引入dtd约束

复制到配置文件中

<?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>
		<!-- 
			name:实体类全路径名
			table:实体类对于的数据库表名称
		 -->
		<class name="com.liyingdong.entity.User" table="t_user_hb">
			<!-- 
				id:用于设置数据库表结构中主键列的生成方式
				name:实体类中属性名称
				type:Jave的数据类型
				column:数据库表字段名称
			 -->
			<id name="id" type="java.lang.Integer" column="id">
				<!-- 
					class:定义主键列生成的方式:hibernate管理、数据库管理、开发者管理
					increment,identity,sequcene,native,assgine
				 -->
				<generator class="increment"></generator>
			</id>
			<!-- 与实体类相匹配 -->
			<property name="userName" type="java.lang.String" column="user_name"/>
			<property name="userPwd" type="java.lang.String" column="user_pwd"/>
			<property name="realName" type="java.lang.String" column="real_name"/>
			<property name="sex" type="" column="sex"/>
			<property name="birthday" type="java.util.Date" column="birthday"/>
			<property name="createdatetime" insert="false" update="false" type="timestamp"  column="create_datetime"/>
			<property name="remark" type="java.lang.String" column="remark"/>
		</class>
	</hibernate-mapping>

实体类

package com.liyingdong.entity;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
public class User implements Serializable{
	private Integer id;
	private String userName;
	private String userPwd;
	private String realName;
	private String sex;
	private Date birthday;
	private Timestamp createdatetime;
	private String remark;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Timestamp getCreatedatetime() {
		return createdatetime;
	}
	public void setCreatedatetime(Timestamp createdatetime) {
		this.createdatetime = createdatetime;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}	
	public User() {		
	}
	public User(Integer id, String userName, String userPwd, String realName, String sex, Date birthday,
			Timestamp createdatetime, String remark) {
		super();
		this.id = id;
		this.userName = userName;
		this.userPwd = userPwd;
		this.realName = realName;
		this.sex = sex;
		this.birthday = birthday;
		this.createdatetime = createdatetime;
		this.remark = remark;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
				+ ", sex=" + sex + ", birthday=" + birthday + ", createdatetime=" + createdatetime + ", remark="
				+ remark + "]";
	}
}

案例

Demo(增加,删除,修改,查询)

package com.liyingdong.test;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.liyingdong.entity.User;
public class Demo {
	public void users() {
		//创建Hibernate核心类
		Configuration cfg=new Configuration();
		//读取核心配置文件
		cfg.configure("hibernate.cfg.xml");
		//创建session工厂
		SessionFactory sf = cfg.buildSessionFactory();
		//获取session
		Session session = sf.openSession();		
		//开启事务
		Transaction ts= session.beginTransaction();		
		System.out.println("-------------增加-------------------");
		//新增
		/*User user=new User();
		user.setUserName("海王大大");
		user.setUserPwd("6661");
		user.setRealName("非常帅");
		user.setSex("男");
		user.setBirthday(new Date());
		user.setRemark("海班");
		//新增操作
		session.save(user);*/		
		System.out.println("-------------修改-------------------");
		//修改(根据id修改)
		/*user.setId(3);
		User user2 = session.get(User.class, user.getId());
		if(null!=user2) {
			user2.setUserName("二狗");
			session.update(user2);
		}	*/			
		System.out.println("-------------删除-------------------");
		//删除
		/*user.setId(1);
		User user2 = session.get(User.class, user.getId());
		if(null!=user2) {
			session.delete(user2);
		}*/
		System.out.println("-------------查询-------------------");
		List<User> list = (List<User>)session.createQuery(" from User").list();
		for (User user : list) {
			System.out.println(user);
		}		
		//提交事务
		ts.commit();
		//释放资源
		session.close();
	}
	public static void main(String[] args) {
		Demo d=new Demo();
		d.users();
	}
}

增加

修改

删除

查询

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>
返回顶部