您好,登錄后才能下訂單哦!
這篇“Hibernate使用的方法是什么”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“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
用于數(shù)據(jù)庫(kù)的連接配置代替了之前的util中的連接類。
創(chuàng)建后引入dtd約束
復(fù)制這一段等會(huì)放入到核心配置文件中
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> <!-- 數(shù)據(jù)庫(kù)相關(guān)配置 --> <!-- connection.username|connection.password|connection.url|connection.driver_class|dialect --> <!-- 連接數(shù)據(jù)庫(kù)賬戶名稱 --> <property name="connection.username">root</property> <!-- 連接數(shù)據(jù)庫(kù)密碼(我的數(shù)據(jù)庫(kù)沒(méi)有登錄密碼,直接不用寫) --> <property name="connection.password">123</property> <!-- 連接的絕對(duì)路徑(使用&需要解譯&) --> <property name="connection.url"> jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&userSSL=false </property> <!-- 驅(qū)動(dòng)的絕對(duì)路徑 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 數(shù)據(jù)庫(kù)方言配置 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 配置本地事務(wù) --> <property name="hibernate.current_session_context_class">thread</property> <!-- 調(diào)試相關(guān)配置 --> <!-- hibernate運(yùn)行過(guò)程是否展示sql命令代碼(自動(dòng)生成) --> <property name="show_sql">true</property> <!-- 是否規(guī)范輸出sql代碼 --> <property name="format_sql">true</property> <!-- 實(shí)體映射相關(guān)配置 --> <mapping resource="com/liyingdong/entity/User.hbm.xml"/> </session-factory></hibernate-configuration>
子配置文件
對(duì)應(yīng)的你是實(shí)體類,有多少個(gè)實(shí)體對(duì)應(yīng)多少個(gè)子配置文件。
注意要在配置文件中引入dtd約束
復(fù)制到配置文件中
<?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"> <!-- 實(shí)體類映射文件 --> <hibernate-mapping> <!-- name:實(shí)體類全路徑名 table:實(shí)體類對(duì)于的數(shù)據(jù)庫(kù)表名稱 --> <class name="com.liyingdong.entity.User" table="t_user_hb"> <!-- id:用于設(shè)置數(shù)據(jù)庫(kù)表結(jié)構(gòu)中主鍵列的生成方式 name:實(shí)體類中屬性名稱 type:Jave的數(shù)據(jù)類型 column:數(shù)據(jù)庫(kù)表字段名稱 --> <id name="id" type="java.lang.Integer" column="id"> <!-- class:定義主鍵列生成的方式:hibernate管理、數(shù)據(jù)庫(kù)管理、開發(fā)者管理 increment,identity,sequcene,native,assgine --> <generator class="increment"></generator> </id> <!-- 與實(shí)體類相匹配 --> <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>
實(shí)體類
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 + "]"; } }
以上就是關(guān)于“Hibernate使用的方法是什么”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。