提前有jdk、mysql、hibernate必须jar包、mysql连接jar包

mysql中的表

Java中的bean,User类

 package com.xiaostudy.demo;

 public class User {

     private int id;
private String username;
private String password; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

JavaBean的对应xml

 <?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"> <!-- 导入bean所在的包路径 -->
<hibernate-mapping package="com.xiaostudy.demo">
<!-- 把bean中的类名和mysql中的表名对应 -->
<class name="User" table="user">
<!-- bean中id和mysql中主键对应 -->
<id name="id" column="id">
<!-- generator:主键生成策略
1.increment 数据库自己生成主键. 先从数据库中查询最大的ID值,将ID值加1作为新的主键
2.identity 依赖于数据的主键自增功能
3.sequence 序列,依赖于数据中的序列功能(Oracle).
4.hilo(纯了解,永远用不到) : Hibernate自己实现序列的算法,自己生成主键. (hilo算法 )
5.native 自动根据数据库判断,三选一. identity|sequence|hilo
6.uuid 生成32位的不重复随机字符串当做主键
7.assigned 自己指定主键值. 表的主键是自然主键时使用. -->
<generator class="native"></generator>
</id>
<!-- bean中的属性和mysql中的字段对应 -->
<property name="username" column="username"></property>
<property name="password" column="password"></property>
</class>
</hibernate-mapping>

hibernate的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.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql的用户名 -->
<property name="connection.username">root</property>
<!-- mysql的用户密码 -->
<property name="connection.password">123456</property>
<!-- 连接mysql的某库 -->
<property name="connection.url">jdbc:mysql://localhost:3306/user</property>
<!-- 控制台输出sql -->
<property name="show_sql">true</property>
<!-- 格式化输出的sql -->
<property name="format_sql">true</property>
<!-- 自动提交事务 -->
<property name="connection.autocommit">true</property>
<!-- 导入映射文件 -->
<mapping resource="com/xiaostudy/demo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

测试Java类

 package com.xiaostudy.demo;

 import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session; public class Test1 { public static void main(String[] args) {
//加载hibernate.cfg.xml文件,前提是默认放在src目录下
Configuration conf = new Configuration().configure();
//根据配置,创建Factory
SessionFactory sf = conf.buildSessionFactory();
//通过SessionFactory来获取Session,
//getCurrentSession()方法是,获取当前的Session,一般在事务中使用
//但是需要在hibernate.cfg.xml中配置才可以使用
//<property name="current_session_context.class">thread</property>
Session session = sf.getCurrentSession();
User user = new User();
user.setUsername("demo3");
user.setPassword("123456");
session.save(user);
//关闭资源
session.close();
sf.close();
} }


hibernate——第一次简单的使用的更多相关文章

  1. Hibernate框架简单应用

    Hibernate框架简单应用 Hibernate的核心组件在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.proper ...

  2. ssh架构之hibernate(一)简单使用hibernate完成CRUD

    1.Hibernate简介   Hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,h ...

  3. Maven+Struts+Hibernate+Spring简单项目搭建

    这段时间学习如何使用Maven+Struts+Hibernate+Spring注解方式建立项目,在这里感谢孙宇老师.    第一次写博客,主要是方便自己查看,同时分享给大家,希望对大家有所帮助,我也是 ...

  4. 新秀学习Hibernate——一个简单的例子

    一个.Hibernate开发. 上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类. 为了可以使用Hibernate高速上手,我们先解说一个简单的Hibernate应用实例hibernat ...

  5. Spring-boot+Spring-batch+hibernate+Quartz简单批量读文件写数据用例

    本文程序集成了Spring-boot.Spring-batch.Spring-data-jpa.hibernate.Quartz.H2等.完整代码在Github上共享,地址https://github ...

  6. 【SSH三大框架】Hibernate基础第五篇:利用Hibernate完毕简单的CRUD操作

    这里利用Hibernate操作数据库完毕简单的CRUD操作. 首先,我们须要先写一个javabean: package cn.itcast.domain; import java.util.Date; ...

  7. 【SSH】——Hibernate实现简单的自动建表

    [与ORM] Object Relational Mapping,对象关系映射,将对象和关系联系了起来.面向对象是从耦合.聚合.封装等的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论 ...

  8. SSH 项目中 用Hibernate底层 简单的封装DAO层

    废话不多少了,主要是使用hibernate的查询方法,自己封装了DAO层,供service来方便使用. 首先:必须要继承的 public class CommonDao extends Hiberna ...

  9. hibernate框架简单步骤

    Demo.java package com.itheima.test; import org.hibernate.Session; import org.hibernate.SessionFactor ...

随机推荐

  1. poj3414

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13545   Accepted: 5717   Special J ...

  2. spring定时器的cronexpression表达式

    转自:https://www.cnblogs.com/yaowen/p/3779284.html 相关配置: import com.alibaba.dubbo.config.annotation.Se ...

  3. [LeetCode] Factorial Trailing Zeros

    Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...

  4. 与Mysqli相关的四种数据库取值

    <!--取值方案一:通过数字数组 fetch_row()--><meta http-equiv="Content-Type" content="text ...

  5. python macos scrapy ,gevent module

    easy_install pip pip install scrapy pip install ipython ImportError: No module named items https://g ...

  6. unix_timestamp 和 from_unixtime 时间戳函数 区别

    1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...

  7. Spring 框架整合Struts2 框架和 Hibernate 框架

    1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...

  8. Spring 框架的事务管理

    1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...

  9. MySQL数据库的设计和表创建

    首先,我们使用Navicat Premium编辑器创建一个用户,同时设置用户权限,MySQL默认有一个root用户,拥有最高权限 下面,我们先创建一个用户: ①CREATE USER  'aaa'@' ...

  10. 汇编学习笔记(AT&T语法)

    一个最基本的汇编程序如下所示: .section .data .section .text .globl _start _start: movl $, %eax # the number 1 is t ...