Hibernate 菜鸟教程 3 单表操作

工程截图

Hibernate 菜鸟教程 3 单表操作_第1张图片
这里写图片描述

配置文件hibernate.cfg.xml



    
        
        
        com.mysql.jdbc.Driver
        jdbc:mysql:///hibernate
        root
        admin
        
        org.hibernate.dialect.MySQLDialect

        
        
        create
        
        true
        
        

        
        
    

映射文件User.hbm.xml





    
    
        
        
            
            
            
            
            
        
        
        
        
        
    

模型对象User.java

package com.jege.hibernate.single.table;

/**
 * @author JE哥
 * @email [email protected]
 * @description: 模型对象
 */
public class User {
  private Long id;
  private String username;
  private String password;

  public User() {

  }

  public User(Long id) {
    this.id = id;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long 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;
  }

  @Override
  public String toString() {
    return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
  }

}

测试类CrudTest.java

package com.jege.hibernate.single.table;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test;

import com.jege.hibernate.util.HibernateUtils;

/**
 * @author JE哥
 * @email [email protected]
 * @description: crud测试类
 */
public class CrudTest {

  // 在每个@Test之前自动执行,先保存数据
  @Before
  public void before() throws Exception {
    User user = new User();
    user.setUsername("jege");
    user.setPassword("1272434821");

    Session session = HibernateUtils.INSTANCE.getSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();
  }

  @Test
  public void save() throws Exception {

  }

  @Test
  public void update() throws Exception {
    Session session = HibernateUtils.INSTANCE.getSession();
    session.beginTransaction();

    User user = (User) session.get(User.class, 1L);
    user.setUsername("JavaEEGe");
    user.setPassword("[email protected]");

    session.update(user);
    session.getTransaction().commit();
    session.close();
  }

  @Test
  public void delete() throws Exception {

    Session session = HibernateUtils.INSTANCE.getSession();
    session.beginTransaction();

    User user = (User) session.get(User.class, 1L);
    if (user != null) {
      session.delete(user);
    }

    session.getTransaction().commit();
    session.close();
  }

  @Test
  public void get() throws Exception {
    Session session = HibernateUtils.INSTANCE.getSession();
    User user = (User) session.get(User.class, 1L);
    System.out.println(user);
    session.close();
  }

  @Test
  public void getAll() throws Exception {
    Session session = HibernateUtils.INSTANCE.getSession();
    Query query = session.createQuery("select u from User u");
    List list = query.list();
    for (User user : list) {
      System.out.println(user);
    }
    session.close();
  }

}

源码地址

https://github.com/je-ge/hibernate

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
**您的支持将鼓励我继续创作!谢谢! **

微信打赏
微信打赏

支付宝打赏
支付宝打赏

你可能感兴趣的:(Hibernate 菜鸟教程 3 单表操作)