ORM框架/持久层框架
dao dao
jdbc hibernate
jdbc
ORM(Object Relational Mapping):对象关系映射。
对象与关系型数据库之间的映射管理框架
优势:跨数据库的无缝移植(SqlServer、Oracle、MySql)
首先把pom.xml所需要的jia包写上并下载
<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.zhoujun</groupId>
<artifactId>hibrenate</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hibrenate 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.49</MySQL.version>
<jstl.version>1.2</jstl.version>
<hibernate.version>5.2.12.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${MySQL.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.5.53</version>
</dependency>
</dependencies>
<build>
<finalName>struts</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>
web xml配置 把原有的改成3.0的
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
</web-app>
在resource目录下添加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>
<!--数据库相关配置 -->
<!--连接账户名称 -->
<property name="connection.username">root</property>
<!-- 链接账户密码 -->
<property name="connection.password">123456</property>
<!-- 连接的绝对路径 -->
<property name="connection.url">
jdbc:mysql://localhost:3306/ddd?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="show_sql">true</property>
<!--是否规范化输出sql代码 -->
<property name="format_sql">true</property>
<!--实体映射相关配置 -->
<mapping resource="com/zhoujun/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
在开发阶段再创建实体类和实体映射文件(*.hbm.xml)
实体必须实现Serializable接口
<?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.zhoujun.entity.User" table="t_user_hb">
<!--
id : 用于设置数据库表结构中主键列的生成方式
name : 实体类中属性类型
type : java中数据类型
cliumn : 数据库表字段名称
-->
<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>
<property name="userPwd" type="java.lang.String" column="user_pwd"></property>
<property name="realName" type="java.lang.String" column="real_name"></property>
<property name="sex" type="java.lang.String" column="sex"></property>
<property name="birthday" type="java.util.Date" column="birthday"></property>
<property name="createDatetime" insert="false" update="false" type="timestamp" column="create_datetime"></property>
<property name="remark" type="java.lang.String" column="remark"></property>
</class>
</hibernate-mapping>
小结:hibernate.cfg.xml(1)/*.hbm.xml(N)
实体映射文件一定要加到核心配置文件
创建一个实体类:与数据库相连
package com.zhoujun.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 Character 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 Character getSex() {
return sex;
}
public void setSex(Character 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;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
+ remark + "]";
}
public User() {
}
public User(Integer id, String userName, String userPwd, String realName, Character sex, Date birthday,
Timestamp createDatetime, String remark) {
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.realName = realName;
this.sex = sex;
this.birthday = birthday;
this.createDatetime = createDatetime;
this.remark = remark;
}
}
测试类:
package com.zhoujun.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zhoujun.entity.User;
public class Demo {
public static void main(String[] args) {
Demo demo=new Demo();
demo.users();
}
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();
// 新增
User user=new User();
// user.setUserName("张三");
// user.setUserPwd("123456");
// user.setRealName("砖石王老五");
// user.setSex("男");
// user.setBirthday(new Date());
user.setRemark("有钱人111");
session.save(user);
// 修改
// user.setId(3);
// User u=(User)session.get(User.class, user.getId());
// if(null!=u) {
// u.setRemark("有钱人111");
// session.update(user);
// }
// session.get(user.getId().toString(), user);
// session.update(user);
// 删除
user.setId(2);
User u=(User)session.get(User.class, user.getId());
if(null!=u) {
session.delete(u);
}
ts.commit();
session.close();
}
}