hibernate基于单表curd

目录

    • 1. hibernate框架
    • 2. 配置文件实体mapper和hibernate.cfg.xml
    • 3. 操作单表增删改查

1. hibernate框架

数据持久层的框架
功能定位:专门用来访问数据库,对数据库进行增删改查操作
Hibernate是一个ORM框架 MyBatis   MyBatisPlus、JPA(springdata jpa)
ORM:Object Relational Mapping(对象关系映射)特点
1.	上手较难
2.	灵活度低
3.	全自动(不写sql语句操作数据库)

2. 配置文件实体mapper和hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping><!-- 配置stu info 实体类与表的映射关系 --><!-- 类的全路径 和 表名 --><class name="cn.bitqian.entity.StuInfo" table="stu_info"><!-- 主键  --><!-- name 为属性 --><!-- id 为列 --><id name="sid" column="id"><!-- 配置主键生成策略 --><generator class="native"></generator></id><!-- 可以配置列的长度 --><property name="name" column="name"></property><property name="birthday" column="birth_day"></property></class><!--     <class name="cn.bitqian.entity.User" table="users2"><id name="userId" column="user_id">主键生成策略<generator class="native"></generator></id>实体属性与列的映射关系!<property name="userName" column="user_name"></property><property name="userPasswrod" column="user_password"></property><property name="userGender" column="user_gender"></property><property name="userAddress" column="user_address"></property><property name="registerDate" column="register_date"></property></class> --></hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory><!-- mysql 5.x 版本sql 连接配置 --><property name="connection.url">jdbc:mysql://localhost:3306/ssh_study</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property><property name="connection.password">123456</property><!-- 数据库方言 mysql --><property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><!-- 在命令执行时是否显示sql语句 --><property name="show_sql">true</property><property name="format_sql">true</property><!-- 用于更新表的变化 --><property name="hbm2ddl.auto">update</property><!-- 引用实体类 与 数据库表的 映射文件 --><mapping resource="cn/bitqian/entity/stuInfo.hbm.xml"/><!-- 引用user映射文件 --><mapping resource="cn/bitqian/entity/user.hbm.xml"/></session-factory></hibernate-configuration>

驱动包注意

Mysql 5driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mydatabaseMysql 8.0.15driver=com.mysql.cj.jdbc.Driverurl=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&需转译成&amp;

3. 操作单表增删改查

  • 实体类
package cn.bitqian.entity;import java.io.Serializable;
import java.util.Date;public class StuInfo implements Serializable {/*** 唯一 serial id* 用于序列化和返序列化的校验*/private static final long serialVersionUID = -3784274218883718429L;private Integer sid;private String name;private Date birthday;// 省略set/get
}
  • SessionFactory
package cn.bitqian.dao;import java.io.Serializable;
import java.util.Date;import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
//import org.hibernate.dialect.MySQL5InnoDBDialect;import cn.bitqian.entity.StuInfo;/*** get / load distinct* single table curd * @author echo lovely**/
public class Test1 {public static void main(String[] args) {test1();}/*** stuInfo 表测试*/static void test1() {// 1. 读取配置文件Configuration config = new Configuration().configure();// 2. 通过配置文件 获取 sessionFactorySessionFactory sessionFactory = config.buildSessionFactory();// 3. 通过session工厂获取sessionSession session = sessionFactory.openSession();// 4. 开启事务session.beginTransaction();Object stuObj = null;// 数据库操作try {// 新增/*StuInfo stu = new StuInfo();stu.setName("jack");stu.setBirthday(new Date());// 返回新增主键值Serializable primaryValue = session.save(stu);System.out.println(primaryValue);*/// 修改/*StuInfo stu = new StuInfo();stu.setSid(1);stu.setName("rose");stu.setBirthday(new Date());session.update(stu);System.out.println("修改成功...");*/// 删除/*StuInfo stu = new StuInfo();stu.setSid(1);session.delete(stu);*/// 单个查询// load 懒加载 和 get的区别// load 查询 延时加载System.out.println("load 查询前");stuObj = session.load(StuInfo.class, 1);System.out.println("load 查询后");if (stuObj != null) {System.out.println("使用对象");StuInfo stu = (StuInfo) stuObj;System.out.println(stuObj + "\n转型后的对象: \n" + stu);}System.out.println("null stu by load:");// load查询不存在的// org.hibernate.ObjectNotFoundException: No row with the given identifier existsSystem.out.println(session.load(StuInfo.class, 2));// get 查询 即时加载/*System.out.println("get 查询前:");stuObj = session.get(StuInfo.class, 1);System.out.println("get 查询后:");*//*** 延时加载和即时加载的区别:* 	相同点:* 		1. 都能查询单条数据,在Session关闭后不能再查询*      2. 都存在缓存*  不同点:*  	1. 即时加载不管数据是否存在,都会返回结果,返回null或者数据。*  		而延时load加载查不到数据时报错。*      2. 延时load加载只能在 你使用数据时,发送查询请求*  	*/// 提交事务session.getTransaction().commit();} catch (Exception e) {session.getTransaction().rollback();e.printStackTrace();} finally {// 关闭sessionsession.close();// 关闭session工厂sessionFactory.close();}// hibernate中的缓存System.out.println(stuObj);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420649.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1189):事件基本使用

<!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><!-- v-cloak用法 -->&…

Selenium - IWebDriver 控制scroll bar到底部

有时候我们需要控制页面滚动条上的滚动条&#xff0c;但滚动条并非页面上的元素&#xff0c;这个时候就需要借助js是来进行操作。一般用到操作滚动条的会两个场景&#xff1a; 注册时的法律条文需要阅读&#xff0c;判断用户是否阅读的标准是&#xff1a;滚动条是否拉到最下方。…

Angular常用命令行和指令

命令行: 命令行含义简写ng new 包名生成项目包ng n 包名ng serve启动项目, 端口号4200ng sng serve --open启动项目 并 在默认浏览器自动打开ng s -ong generate component 组件名生成组件ng g c 组件名ng generate directive 指令名生成指令ng g d 指令名ng generate pipe 管…

前端学习(1190):事件修饰符

传统方式 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><!-- v-cloak用…

mybatis --入门 单表增删改查-curd

目录1. mybatis 环境搭建2. 实体类映射文件配置&#xff08;写sql&#xff09;3. mybatis核心配置文件 &#xff08;环境配置&#xff09;4. 测试mybatis document https://mybatis.org/mybatis-3/zh/ mybatis in github https://github.com/mybatis/mybatis-3 1. mybatis 环…

ios开发之.pch文件的使用

案例&#xff1a;开源中国iOS客户端 当我们新建一个工程的时候&#xff0c;在Supporting FIles文件下会看到一个以 -Prefix.pch结尾文件的文件&#xff0c;pch全称是“precompiled header”&#xff0c;也就是预编译头文件&#xff0c;该文件里存放的工程中一些不常被修…

循环给对象创建属性名和属性值

4.7号笔记&#xff1a; ​ ① 循环给对象创建属性名和属性值&#xff1a; data.forEach(item > {item.identity identity;})console.log(data);

CWnd与HWND的区别与转换

一、区别HWND是句柄&#xff0c;CWnd是MFC窗体类,CWnd中包含HWND句柄成员对象是m_hWnd.HWND是Windows系统中对所有窗口的一种标识&#xff0c;即窗口句柄。这是一个SDK概念。 CWnd是MFC类库中所有窗口类的基类。微软在MFC中将所有窗口的通用操作都封装到了这个类中&#xff0…

字符串截取后两位,字符串转成数组,再转换位字符串

4.11号笔记 字符串去掉所有空格&#xff0c;转成数组&#xff0c;再转成字符串 var str 你好&#xff01; 世界 * * var arr str.replace(/\s/g, "").split("");//去掉所有空格并转成数组arr.splice(-2, 2); // 从最后面截取两位str arr.join(&q…

mybatis dao实现 || 接口代理方式实现

目录1、mybatis环境搭建2、mybatis dao接口实现3、动态代理方式&#xff0c;只实现Mapper接口mybatis入门单表操作demo mybatis dao层实现1. 实现dao层接口 2. 接口代理方式实现dao代理开发要求&#xff1a;​ Mapper接口开发方法只需要编写Mapper接口&#xff08;相当于dao接…

lvs-dr模式原理详解和可能存在的“假负载均衡”

原文地址&#xff1a; http://blog.csdn.net/lengzijian/article/details/8089661 lvs-dr模式原理 转载注明出处&#xff1a;http://blog.csdn.net/lengzijian/article/details/8089661 先附上一张原理图&#xff1a; 为了更清晰的表述lvs-dr原理&#xff0c;我们用tcpdump工具…

rem.js常用代码

rem.js (function flexible(window, document) {var docEl document.documentElement;var dpr window.devicePixelRatio || 1;// adjust body font size// 设置 em 默认字体所对应的大小function setBodyFontSize() {if (document.body) {document.body.style.fontSize 12 …

mybatis3 类型别名

目录问题描述解决方案方案二&#xff0c;直接扫描包问题描述 <!-- 插入操作 需要user参数 --><insert id"insertUser" parameterType"cn.bitqian.entity.User">insert into users1 values (#{userId}, #{userName}, #{userPassword})</inse…

vue-获取某个组件渲染的Dom根元素

function getComponentRootDom(comp, props){const vm new Vue({render: h > h(comp, {props})})vm.$mount();return vm.$el;}

动态sql (sql-if,sql-foreach)

目录1. UserMapper接口1. sql-if2. sql-foreach3. 多条件查询和根据多个id查询测试say sth.. 每当我回顾以前写jdbc分页&#xff0c;多条件查询时&#xff0c;我那是有多痛苦。 特别是当我拼接条件时&#xff0c;对象.getName 又是判断null&#xff0c;又是判断空字符串的。 还…

把一张合成图分拆出各个小图

反编译一个程序&#xff0c;看到一张合成图&#xff0c;想分拆出里面的每个小图&#xff0c;知道了各个图的坐标大小后&#xff0c;写了一个小方法&#xff0c;希望对大家有用哈 package com.bitimage;import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStr…

6_js数组常用函数进阶与String

1 数组常用函数的应用 1.1 数组常用方法解析进阶 文档&#xff1a;const - JavaScript | MDN 课堂案例&#xff1a;01.find&Some方法的应用.html find() 从数组中找到满足条件的第一个元素并且并返回它。否则返回 undefined。 findIndex()*方法返回数组中满足提供的测试…

vue-快速原型开发

官方地址&#xff1a; https://cli.vuejs.org/zh/guide/prototyping.html

mybatis TypeHandler 类型处理器

目录1. 自定义日期类型处理器2. 配置自定义日期处理器3. 新增&#xff0c;查询1. 自定义日期类型处理器 继承mybatis提供的BaseTypeHandler覆写方法&#xff0c; 来转换Java和数据库中的字段package cn.bitqian.config;import org.apache.ibatis.type.BaseTypeHandler; import…

使用map递归树形结构

mapTree (data) {const reg /^e/;const regu /^u/;data.map(items > {if(items.children.length < 1){if(reg.test(items.userid)){items.disabled true //遍历树 拼入相应的disabled}else if(regu.test(items.userid)){items.children undefined }}else{this.mapTr…