hibernate入门1
来源:互联网 发布:韩国民族主义知乎 编辑:程序博客网 时间:2024/06/06 18:22
1创建java工程
2导包并add build path
3实体类 User
package com.itcast.a_1;public class User {private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer 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;}}
4建数据库表
use zn ;DROP TABLE IF EXISTS `STU`; CREATE TABLE `STU` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `password` varchar(30), PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; desc stuselect * from stu
5核心配置文件
<?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><property name="hibernate.connection.url">jdbc:mysql:///zn</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><property name="show_sql">true</property><property name=""></property><mapping resource="com/itcast/a_1/User.hbm.xml"/></session-factory></hibernate-configuration>
6mapping配置文件
<?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="com.itcast.a_1.User" table="stu"><!-- 主键 --><id name="id"><!-- 固定值:主键生成策略 --><generator class="native"></generator></id><!-- 普通属性 --><property name="username"></property><property name="password"></property></class></hibernate-mapping>
7测试
package com.itcast.a_1;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class UserTest {@Testpublic void demo1(){User user = new User();user.setUsername("li");user.setPassword("123124");//1 加载配置文件获得核心配置对象Configuration config = new Configuration().configure();//2 获得工厂 SessionFactory,相当于连接池SessionFactory factory = config.buildSessionFactory();//3获得会话session,相当于链接ConnectionSession session = factory.openSession();//4 开启事务Transaction transaction = session.beginTransaction();//操作session.save(user);//5 提交事务 | 回滚事务transaction.commit();//6 释放资源--关闭sessionsession.close();//7 释放资源--关闭工厂factoryfactory.close();}}
8 查看数据库更改成功
阅读全文
1 0
- hibernate教程:1、hibernate入门
- 1、hibernate入门1
- hibernate入门笔记(1)
- Hibernate 入门(1)
- Hibernate入门(1)
- 心得1-hibernate入门
- 1、Hibernate入门
- Hibernate初入门1
- Hibernate 学习入门1
- 【1】Hibernate的入门
- hibernate入门1
- hibernate入门1
- hibernate入门(1)
- Hibernate 入门(1)
- Hibernate——(1)Hibernate入门
- Hibernate——(1)Hibernate入门
- Hibernate入门笔记 1, 2
- Hibernate入门学习笔记1
- SQL中EXISTS的用法
- mysql基本命令
- Scala正则表达式模式
- 对于池化的一些看法
- SQL Server Profiler:使用方法和指标说明
- hibernate入门1
- CSS属性之浮动以及解决浮动塌陷
- [论文阅读笔记] Massive Exploration of Neural Machine Translation Architectures
- 微信公众号支付前端部分流程
- 1056. 组合数的和(15)
- Codeforces Round #218 (Div. 2) D. Vessels(思维 并查集)
- 经典论文整理
- 10_二进制中1的个数
- Git SSH Key 生成步骤