码迷,mamicode.com
首页 > 编程语言 > 详细

(28)java web的hibernate使用

时间:2018-08-14 11:38:53      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:数据   图片   setter   对象   mysql   java程序员   pre   tac   www.   

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。

ORM: Object Realtion Mapping

 

一,导入jar包

  技术分享图片

二,创建数据库,建表

  技术分享图片

三,创建class,和表的对应关系映射文件

  Account.class

public class Account {

	/* 一定要生成 setter getter 方法,否则包异常 */
	private int accountId;
	private String name;
	private int age;
	private String nickName;

	public int getAccountId() {
		return accountId;
	}

	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

}

   Account.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="gy.hibernate">

	<class name="Account" table="accounts">

		<!-- 主键 ,映射 -->
		<id name="accountId" column="id">
			<generator class="native" />
		</id>

		<!-- 非主键,映射 -->
		<property name="name" column="name"></property>
		<property name="age" column="age"></property>
		<property name="nickName" column="nick_name"></property>
	</class>

</hibernate-mapping>

 

四,配置hibernate 配置文件

  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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 配置数据库url -->
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property>
		<!-- mysql账号 -->
		<property name="hibernate.connection.username">root</property>
		<!-- mysql密码 -->
		<property name="hibernate.connection.password"></property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		
		<!-- 调试打印日志 -->
		<property name="hibernate.show_sql">true</property>
		
		<!-- 加载所有映射 -->
		<mapping resource="gy/hibernate/Account.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 

五,代码调用

  public String register() {
		
    System.out.println("开始注册-------------------------");

    Account account = new Account();
    // account.setAccountId(1);
    account.setName("xiao gang");
    account.setAge(15);
    account.setNickName("xg");

    // 获取加载配置文件的管理类对象
    Configuration config = new Configuration();
    config.configure(); // 默认加载src/hibenrate.cfg.xml文件
    // 创建session的工厂对象
    SessionFactory sf = config.buildSessionFactory();
    // 创建session (代表一个会话,与数据库连接的会话)
    Session session = sf.openSession();
    // 开启事务
    Transaction tx = session.beginTransaction();
    // 保存-数据库
    session.save(account);
    // 提交事务
    tx.commit();
    // 关闭
    session.close();
    sf.close();

    return SUCCESS;
  }

 

(28)java web的hibernate使用

标签:数据   图片   setter   对象   mysql   java程序员   pre   tac   www.   

原文地址:https://www.cnblogs.com/yangzigege/p/9472300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!