这篇“Hibernate使用的方法是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“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> <!-- 连接的绝对路径(使用&需要解译&) --> <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 + "]"; } }
以上就是关于“Hibernate使用的方法是什么”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。