Hibernate框架对mysql数据库进行CRUD(增改改删)
来源:互联网 发布:js对象添加属性的方法 编辑:程序博客网 时间:2024/06/05 05:55
使用hibernate框架对mysql数据库进行增删改查
1、创建一个java工程,并且创建一个数据库hibernate
2、创建一个lib文件夹,拷贝jar包,并且build path(圈起的三个是测试jar包,编辑器有的话可以不考)
3、创建hibernate配置文件hibernate.cfg.xml,这里创建在根目录下
<?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"><hibernate-configuration> <!-- 一个sessionFactory代表数据库的一个连接 --> <session-factory> <!-- 链接数据库的用户名 --> <property name="connection.username">root</property> <!-- 链接数据库的密码 --> <property name="connection.password">newstar</property> <!-- 链接数据库的驱动 --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 链接数据库的url --> <property name="connection.url"> <![CDATA[jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf8]]> </property> <!-- 方言 告诉hibernate使用哪个数据库,hibernate就会在底层拼接相应的sql语句 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 根据持久化类生成表的策略 validate 通过映射文件检查持久化泪idea匹配 update hibernate每次启动的时候,检查表是否存在,弱国不存在,则创建,如果存在,则说明不进行任何操作 create hibernate每次启动的时候,根据持久化类和映射文件生成表 create-drop hibernate每次启动的时候,根据持久化类和映射文件生成表,之后再删除 --> <property name="hbm2ddl.auto">update</property> <!-- 显示sql语句(开发中写上,方便开发观察) --> <property name="show_sql">true</property> <!-- 映射文件, --> <mapping resource="com/itstar/domain/Person.hbm.xml" /> </session-factory></hibernate-configuration>
4、创建持久化类
package com.itstar.domain;import java.io.Serializable;public class Person implements Serializable { private Long pid; private String name; private String password; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
5、创建映射文Person.hbm.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"><hibernate-mapping> <!-- hibernate映射文件 --> <class name="com.itstar.domain.Person" table="person"> <!-- id 描述主键 name 属性的名称 column 属性的名称对应的表中的字段 (不写时,默认与属性名称一致) length 属性的名称对应的表的字段长度 (不写时,默认为最大值 --> <id name="pid" column="pid" length="5"> <generator class="increment"></generator> </id> <property name="name" length="20" type="java.lang.String"></property> <property name="password" length="20" type="java.lang.String"></property> </class></hibernate-mapping>
6、创建测试类开始测试
package com.itstar.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.itstar.domain.Person;public class TestHibernateCRUD { //保存用户 @Test public void testSavePerson(){ Configuration configuration = new Configuration(); //在跟根目录,不需要添加参数 configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = new Person(); person.setPid(1L); person.setName("张三"); person.setPassword("123456"); session.save(person); transaction.commit(); session.close(); } //查询用户 @Test public void testGetPerson(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Person person = (Person)session.get(Person.class, 2L); System.out.println(person.getName()); session.close(); } //更改用户 @Test public void testChangePerson(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = (Person)session.get(Person.class, 2L); person.setName("李四"); session.update(person); transaction.commit(); session.close(); } //删除用户 @Test public void testDeletePerson(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = (Person)session.get(Person.class, 3L); session.delete(person); transaction.commit(); session.close(); }}
阅读全文
0 0
- Hibernate框架对mysql数据库进行CRUD(增改改删)
- hibernate对数据库进行CRUD操作
- 对数据库进行CRUD
- hibernate 逆向工程对数据库进行CRUD操作
- 使用JDBC对数据库进行CRUD(二)
- 使用JDBC对数据库进行CRUD(转)
- MyBatis(二)--对MySQL数据表进行CRUD操作
- 使用hibernate框架对mysql进行操作时数据库乱码问题
- 运用Hibernate机制,对mysql数据库中的表进行增,删,改,查
- 使用JDBC对数据库进行CRUD
- Android中对数据库进行CRUD操作
- 使用JDBC对数据库进行CRUD
- 使用JDBC对数据库进行CRUD
- 在Hibernate中用DAO和spring对db进行CRUD
- 使用hibernate进行CRUD
- hibernate增、删、改、查(CRUD)
- 【hibernate框架】一对多(多对一)双向CRUD-Cascade1
- 【hibernate框架】一对多(多对一)双向CRUD-Cascade2
- 用百度AI的OCR文字识别结合PHP实现了图片的文字识别功能
- 树的统计Count HYSBZ
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 Problem C: 勤劳的ACgirls (隔板问题)
- 数据结构实验之栈六:下一较大值(二)
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Hibernate框架对mysql数据库进行CRUD(增改改删)
- 【DFS】UVA
- 实现两个超长正整数相加
- C#钩子类 几乎捕获键盘鼠标所有事件
- 119. Pascal's Triangle II
- java 利用Runtime.getRuntime().exec()调用python脚本并传参
- jdk5新特性
- struts2的结果处理方式
- 数据结构实验之二叉树六:哈夫曼编码(最优二叉树)