Hibernate:用户注册、查询用户信息、用户删除、用户修改功能


```xml

```xml
1、Hibernate.cfg.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="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate03?useUnicode=true&amp;serverTimezone=GMT&amp;characterEncoding=UTF-8&amp;useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
	<!-- 基本配置 -->
	<!-- 数据库的方言 -->
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<!-- 是否显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
	<!-- 是否格式化SQL语句 -->
	<property name="hibernate.format_sql">true</property>
	<!-- 是否让hibernate根据表结构的变化来生成DDL语句
	DDL:数据定义语言
	hibernate可以根据映射文件来为我们生成数据库的表结构。
	但是他不能生成数据库。
	hbm2ddl.auto的取值
	* none:不用Hibernate自动生成表.
	* create:每次都会创建一个新的表.(测试)
	* create-drop:每次都会创建一个新的表,执行程序结束后删除这个表.(测试)
	* update:如果数据库中有表,使用原来的表,如果没有表,创建新表.可以更新表结构。
	* validate:只会使用原有的表.对映射关系进行校验.-->
	<property name="hibernate.hbm2ddl.auto">update</property>
	<!-- 配置映射文件 -->	
	  <mapping resource="com/iot/entity/User.hbm.xml"/>
		</session-factory>
	</hibernate-configuration>

2User.java
package com.iot.entity;
import java.util.Date;
public class User {
  private int id;
  private String username;
  private String password;
  private String gender;
  private String email;
  private String telephone;
  private String introduce;
  private int state;
  private String role;
  private Date registTime;
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;
}
public String getGender() {
	return gender;
}
public void setGender(String gender) {
	this.gender = gender;
}
public String getEmail() {
	return email;
}
public void setEmail(String email) {
	this.email = email;
}
public String getTelephone() {
	return telephone;
}
public void setTelephone(String telephone) {
	this.telephone = telephone;
}
public String getIntroduce() {
	return introduce;
}
public void setIntroduce(String introduce) {
	this.introduce = introduce;
}
public int getState() {
	return state;
}
public void setState(int state) {
	this.state = state;
}
public String getRole() {
	return role;
}
public void setRole(String role) {
	this.role = role;
}
public Date getRegistTime() {
	return registTime;
}
public void setRegistTime(Date registTime) {
	this.registTime = registTime;
}
@Override
public String toString() {
return "User [t_id=" +t_ id + ", username=" + username + ", password=" + password + ", gender=" + gender + ", email="+ email + ", telephone=" + telephone + ", introduce=" + introduce + ", state=" + state + ", role=" + role+ ", registTime=" + registTime + "]";
}
}
3、User.hbm.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">
<hibernate-mapping>
<!--table:名字 name:全路径 -->
   <class table="t_user" name="com.iot.entity.User">
<!--id:主键  name:字段名 column:名字 generator class="native:自增 property:其他建 -->   
   <id name="id" column="t_id">
   <generator class="native"></generator>
   </id>
   <property name="username" column="username" ></property>
   <property name="password" column="password" ></property>
   <property name="gender" column="gender" ></property>
   <property name="email" column="email" ></property>
   <property name="telephone" column="telephone" ></property> 
   <property name="introduce" column="introduce" ></property>
   <property name="state" column="state" ></property>
   <property name="role" column="role" ></property>
   <property name="registTime" column="registTime" ></property>
   </class>
</hibernate-mapping>
4UserDao.java
package com.iot.dao;
import com.iot.entity.User;
public interface UserDao {
public void registerUser(User user);
public User getUserInfo(int id);
public User updateUserInfo(int id);
public User deleteUserInfo(int id);		
}  

                        
5UserDaoImpl.java
package com.iot.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.iot.dao.UserDao;
import com.iot.entity.User;
import com.iot.utils.SessionFactoryUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public void registerUser(User user) {
	//构建configuration实例,加载主配置文件
//		Configuration cfg=new Configuration().configure("/config/hibernate.cfg.xml");
	//构建SessionFactory	
//		SessionFactory sf=cfg.buildSessionFactory();
		SessionFactory sf=SessionFactoryUtil.getSessionFactory();
	//使用SessionFactory生产一个Session
		Session session=sf.openSession();
	//开启事务
		Transaction tx=session.beginTransaction();
	//进行数据库的操作,用户注册,保存用户
		session.save(user);
	//提交事务
		tx.commit();
	//释放资源
		session.close();
		sf.close();
	}
	@Override
	public User getUserInfo(int id) {
		//构建configuration实例,加载主配置文件
//Configuration cfg=new Configuration().configure("/config/hibernate.cfg.xml");
	   //构建SessionFactory	
//SessionFactory sf=cfg.buildSessionFactory();
		SessionFactory sf=SessionFactoryUtil.getSessionFactory();
	//使用SessionFactory生产一个Session
		Session session=sf.openSession();
	//开启事务
		Transaction tx=session.beginTransaction();
	//进行数据库的操作,用户注册,保存用户。
		User user =(User) session.get(User.class, id);
	//提交事务
		tx.commit();
	//释放资源
		session.close();
		sf.close();
		return user;
	}
	@Override
	public User updateUserInfo(int id) {
		//构建configuration实例,加载主配置文件
//		Configuration cfg=new Configuration().configure("/config/hibernate.cfg.xml");
	//构建SessionFactory	
//		SessionFactory sf=cfg.buildSessionFactory();
		SessionFactory sf=SessionFactoryUtil.getSessionFactory();
	//使用SessionFactory生产一个Session
		Session session=sf.openSession();
	//开启事务
		Transaction tx=session.beginTransaction();
	//进行数据库的操作,用户注册,保存用户。
		User user =(User) session.get(User.class, id);
		user.setUsername("黄老邪");
		session.update(user);
	//提交事务
		tx.commit();
	//释放资源
		session.close();
		sf.close();
		return user;
	}
	@Override
	public User deleteUserInfo(int id) {
		//构建configuration实例,加载主配置文件
//		Configuration cfg=new Configuration().configure("/config/hibernate.cfg.xml");
	//构建SessionFactory	
//		SessionFactory sf=cfg.buildSessionFactory();
		SessionFactory sf=SessionFactoryUtil.getSessionFactory();
	//使用SessionFactory生产一个Session
		Session session=sf.openSession();
	//开启事务
		Transaction tx=session.beginTransaction();
	//进行数据库的操作,用户注册,保存用户。
		User user =(User) session.get(User.class, id);
		session.delete(user);
	//提交事务
		tx.commit();
	//释放资源
		session.close();
		sf.close();
		return user;
	}
}
6UserTest.java
package com.iot.test;
import java.util.Date;
import org.junit.Test;
import com.iot.dao.UserDao;
import com.iot.dao.impl.UserDaoImpl;
import com.iot.entity.User;
public class UserTest {
	@Test
	public void testRegisterUser(){
	User user=new User();
	user.setUsername("黄椿桐");
	user.setPassword("123");
	user.setGender("男");
	user.setRegistTime(new Date());
	UserDao userDao = new UserDaoImpl();
	userDao.registerUser(user);
	}
	@Test
	public void testGetUserInfo(){
		UserDao userDao=new UserDaoImpl();
		User user=userDao.getUserInfo(1);
		System.out.println(user);
	}
	@Test
	public void testUpdateUserInfo(){
		UserDao userDao=new UserDaoImpl();
		User user=userDao.updateUserInfo(1);
		System.out.println(user);
	}
	@Test
	public void testDeleteUserInfo(){
		UserDao userDao=new UserDaoImpl();
		User user=userDao.deleteUserInfo(3);
		System.out.println(user);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩辕椿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值