更新時間:2022-06-22 10:59:45 來源:動力節點 瀏覽1008次
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>
<!-- 連接的絕對路徑(使用&需要解譯&) -->
<property name="connection.url">
jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&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();
}
}
增加
修改
刪除
查詢
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習