博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate mysql demo_hibernate简单的demo
阅读量:4318 次
发布时间:2019-06-06

本文共 1694 字,大约阅读时间需要 5 分钟。

mysql表 CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `pwd` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10113 DEFAULT CHARSET=utf8; hibernate.cfg.xml ?xml version=

mysql表格

CREATE TABLE `t_user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(45) NOT NULL,

`pwd` varchar(45) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=10113 DEFAULT CHARSET=utf8;

hibernate.cfg.xml

mysql

jdbc:mysql://localhost:3306/test

root

123456

com.mysql.jdbc.Driver

org.hiberna【本文来自鸿网互联 (http://www.68idc.cn)】te.dialect.MySQLDialect

User.java

package com.domain;

public class User {

Integer id;

String name;

String pwd;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

}

Test.java

package com.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.domain.*;

public class Test {

public static void main(String[] args) {

Configuration cfg = new Configuration().configure();

SessionFactory factory = cfg.buildSessionFactory();

Session session = null;

try{

session = factory.openSession();

session.beginTransaction();

User user = new User();

user.setName("大狼哥");

user.setPwd("123345");

session.save(user);

session.getTransaction().commit();

}catch(Exception e){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

//关闭session

if(session != null && session.isOpen())

session.close();

}

}

}

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

你可能感兴趣的文章
楼主错题:解析
查看>>
RabbitMQ 启用页面管理功能并设置权限
查看>>
C#获取本机局域网ip和公网ip
查看>>
asp.net如何删除文件夹及文件内容操作
查看>>
Problem Best Time to Buy and Sell Stock I
查看>>
LeetCode(79): 单词搜索
查看>>
PHP7三元运算符 ?? 和 ?: 的区别
查看>>
解决win7 Windows USB无法驱动/驱动错误/该设备无法启动。(代码10)
查看>>
代理模式
查看>>
form表单发送请求实例
查看>>
微软正式提供Visual Studio 2013正式版下载(附直接链接汇总)
查看>>
symbian系统开发教程(一)
查看>>
SpringBoot配置属性之Server
查看>>
MySQL 8.0 Docker使用注解
查看>>
题解报告:hdu 2093 考试排名
查看>>
U - Relatives(欧拉函数)
查看>>
【原创】Lucene.Net+盘古分词器(详细介绍)
查看>>
【转】EntityFramework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句
查看>>
xtu summer individual 5 D - Subsequence
查看>>
树状数组
查看>>