Hibernate实现简单的增删改查操作


1、创建表
//根据实体类创建表字段

2、创建java project项目

//这个不多说

3、添加对应jar包

//这个也不多说

4、创建数据库表实体类

package com.entity;

/**
 * 数据库实体
 * Created by lvjun on 2018-03-26.
 */
public class UsersEntity {
    int id;//用户id
    String uname;
    String upwd;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
}

5、创建UsersEntity.hb.xml配置文件,用于映射实体类对应的数据库表,跟实体类一个包内






    
        
        
            
            
        
        
        
    


6、创建Hibernate.cfg.xml配置文件,在src目录下




    
        
        
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/newtime?characterEncoding=UTF-8
        root
        1234

        
        
        org.hibernate.dialect.MySQLDialect
        
        thread
        
        true
        
        update
        
        
    

7、测试类

1、SelectTestHabernate

package com.test;

import com.entity.UsersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 根据ID查询信息
 * Created by lvjun on 2018-03-26.
 */
public class SelectTestHabernate {
    public static void main(String[] args) {
        //获取SessionFactory
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        //通过SessionFactory 获取 Session
        Session session = sessionFactory.openSession();
        //在Session基础上开启一个事务
        session.beginTransaction();

        //查询方式get
        UsersEntity usersEntity = (UsersEntity) session.get(UsersEntity.class, 4);
        System.out.println("get:id=4的用户名:" + usersEntity.getUname());

        //查询方式load
        UsersEntity usersEntity2 = (UsersEntity) session.load(UsersEntity.class, 29);
        System.out.println("load:id=29的用户名:" + usersEntity2.getUname());

        //提交事物
        session.getTransaction().commit();
        //关闭事物
        session.close();
        sessionFactory.close();
    }

}

2、InsertTestHibernate

package com.test;

import com.entity.UsersEntity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

/**
 * 测试添加参数
 * Created by lvjun on 2018-03-26.
 */
public class InsertTestHibernate {
    public static void main(String[] args) {
        //获取SessionFactory
        SessionFactory  sessionFactory = new Configuration().configure().buildSessionFactory();
        //通过SessionFactory 获取 Session
        Session session =sessionFactory.openSession();
        //在Session基础上开启一个事务
        session.beginTransaction();


        //通过调用Session的save方法把对象保存到数据库,插入一条数据
        UsersEntity usersEntity =new UsersEntity();
        usersEntity.setUname("lvjunjun");
        usersEntity.setUpwd("lvjun123123");
        System.out.println("瞬时状态");
        session.save(usersEntity);
        System.out.println("持久状态");


        //循环插入N条数据
//        for (int i=0 ; i< 10 ; i ++){
//            UsersEntity usersEntity =new UsersEntity();
//            usersEntity.setUname("lvjun"+i);
//            usersEntity.setUpwd("a"+i);
//            session.save(usersEntity);
//        }


        //提交事物
        session.getTransaction().commit();
        //关闭事物
        session.close();
        System.out.println("脱管状态");
        sessionFactory.close();

    }
}

3、UpdateTestHibernate

package com.test;

import com.entity.UsersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 根据ID修改一条记录
 * Created by lvjun on 2018-03-26.
 */
public class UpdateTestHibernate {
    public static void main(String[] args) {
        //获取SessionFactory
        SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
        //通过SessionFactory获取Session
        Session session=sessionFactory.openSession();
        //Session开启事物
        session.beginTransaction();
        //根据id查询一条记录
        UsersEntity usersEntity = (UsersEntity) session.get(UsersEntity.class, 19);
        System.out.println("id是=4的数据参数:"+usersEntity.getUname());
        //修改密码
        usersEntity.setUpwd("1234567890");
        session.update(usersEntity);
        //提交事物
        session.getTransaction().commit();
        //关闭事物
        session.close();
        sessionFactory.close();
    }
}

4、DeleteTestHibernate

package com.test;

import com.entity.UsersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 根据ID删除一条记录
 * Created by lvjun on 2018-03-26.
 */
public class DeleteTestHibernate {
    public static void main(String[] args) {
        //获取SessionFactory
        SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
        //通过SessionFactory获取Session
        Session session=sessionFactory.openSession();
        //Session开启事物
        session.beginTransaction();
        //根据id查询一条记录
        UsersEntity usersEntity = (UsersEntity) session.get(UsersEntity.class, 4);
        //删除
        session.delete(usersEntity);
        //提交事物
        session.getTransaction().commit();
        //关闭事物
        session.close();
        sessionFactory.close();

    }
}

你可能感兴趣的:(Java)