hibernate入门

一、了解什么是hibernate?

hibernate:ORM框架/持久层框架 jdbc的一个框架 属于轻量级
通过管理对象来改变数据库中的数据
通过管理对象来操作数据库
它的优势:跨数据库的无缝移植
可以不用写sql语句,当然它的弊端也是有的,如果出现错误,不知道问题出在哪里

二、hibernate配置

pom.xml

需要手动配置pom.xml依赖

<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.chen</groupId>
  <artifactId>T224_hibernate</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>T224_hibernate Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<junit.version>4.12</junit.version>
		<servlet.version>4.0.0</servlet.version>
		<hibernate.version>5.3.0.Final</hibernate.version>
		<mysql.driver.version>5.1.46</mysql.driver.version>
	</properties>
  
  <dependencies>
  <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.driver.version}</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>T224_hibernate</finalName>
    <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>${maven.compiler.source}</source>
					<target>${maven.compiler.target}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
		</plugins>
    
  </build>
</project>

web.xml

替换web.xml2.3至3.1

<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_1.xsd"
	id="WebApp_ID" version="3.1">
  <display-name>Archetype Created Web Application</display-name>
</web-app>

hibernate主要配置

hibernate.cfg.xml(src/main/resources目录下)

<?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>
		<!-- 1. 数据库相关 -->
		<property name="connection.username">root</property>
		<property name="connection.password">123</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/mysql?useUnicode=true&amp;characterEncoding=UTF-8
		</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

		<!-- 配置本地事务(No CurrentSessionContext configured!-->
		<property name="hibernate.current_session_context_class">thread</property>

		<!-- 2. 调试相关 -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>

		<!-- 3. 添加实体映射文件 -->
	<mapping resource="com/c/entity/User.hbm.xml"/><!--次要配置路径-->
	</session-factory>
</hibernate-configuration>

次要配置 User.hbm.xml (entity目录下)
注释如下

<?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>
 <!-- class标签:
	    		name:对应的是需要映射的实体类的全路径名
	    		table:实体类对应的数据库中的表
    		
    		id标签:配置的表中的主键
	    		name:对应的实体类属性名
	    		type:指的是实体类数据类型
	    		column:数据表对应的列段
    		
    		property:配置出去主键以外列段对应的类属性映射关系
    			name:对应的实体类属性名
	    		type:指的是实体类数据类型
	    		column:数据表对应的列段
	    		nsert="false" update="false"
	    		上面的表示的含义是:该类段或者说该属性性制作查查询用,不做更新
	    		
     -->
	<class name="com.c.entity.User" table="tb_stu">
		<id name="sid" type="java.lang.Integer" column="sid">
			<generator class="increment" />
		</id>
		<property name="sname" type="java.lang.String" column="sname">
		</property>
		<property name="shobby" type="java.lang.String" column="shobby">
		</property>
		<property name="tid" type="java.lang.Integer" column="tid">
		</property>
		<property name="cid" type="java.lang.Integer" column="cid">
		</property>

	</class>

</hibernate-mapping>

三、hibernate增删改查测试

User实体类

package com.chen.one.entity;

public class User {
	private int sid;
	
	private String sname;
	private String shobby;
	private int tid;
	private int cid;
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getShobby() {
		return shobby;
	}
	public void setShobby(String shobby) {
		this.shobby = shobby;
	}
	public Integer getTid() {
		return tid;
	}
	public void setTid(Integer tid) {
		this.tid = tid;
	}
	public Integer getCid() {
		return cid;
	}
	public void setCid(Integer cid) {
		this.cid = cid;
	}
	
	public User() {
		super();
	}
	
	public User(Integer sid, String sname, String shobby, Integer tid, Integer cid) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.shobby = shobby;
		this.tid = tid;
		this.cid = cid;
	}
	
	
	
	
}

QueryDemo

查询所有

package com.c.Demo;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class QueryDemo {
	public static void main(String[] args) {
		Configuration configure = new 	Configuration().configure("/hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		
		List list = session.createQuery("from User").list();
		for (Object object : list) {
			System.out.println(object);
		}
		
		session.close();
	
		
	}

}

InsertDemo

增加

package com.c.Demo;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.c.entity.User;

public class InsertDemo {
	public static void main(String[] args) {
		Configuration configure = new 	Configuration().configure("/hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
		session.save(new User(null, "太太", "篮球", 1, 2));
		
		transaction.commit();
		session.close();
	
		
	}

}

UpdateDemo

修改

package com.c.Demo;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.c.entity.User;

public class UpdateDemo {
	public static void main(String[] args) {
		Configuration configure = new 	Configuration().configure("/hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
//		session.save(new User(null, "太太", "篮球", 1, 2));
		User user = session.get(User.class, 7);
		user.setSname("鸡你太美");
		System.err.println(user);
		transaction.commit();
		session.close();
	
		
	}

}

DeleteDemo

删除

package com.c.Demo;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.c.entity.User;

public class DeleteDemo {
	public static void main(String[] args) {
		Configuration configure = new 	Configuration().configure("/hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
		User user = new User();
		user.setSid(7);
		session.delete(user);
		
		transaction.commit();
		session.close();
	
		
	}

}

欧啦,今天的hibernate入门就到这里啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值