hibernate实体类操作(一)添加与根据Id查询

Hibernate实体类操作

hibernate核心配置文件hibernate.cfg.xml与映射文件User.hbm.xml的配置不做多余赘述,若要了解,请看本人之前的一篇博文(点击即可链接至目标文章):[hibernate配置文件详解](http://blog.csdn.net/qq_33349750/article/details/75810267)

一、 添加操作:
1、 调用session里面的save方法来进行添加,代码示例如下:

User2 user = new User2();
user.setId(4);
user.setUname("lalala");
user.setPassword("lalala");
user.setAddress("CQ");
//调用session的方法进行添加
session.save(user);

2、 根据Id(主键)查询
实际上是根据session里面get方法实现。相关代码为:
1) 工具类:


package cn.lzb.utils;

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

public class SessionFactoryUtils {

    static SessionFactory seFactory = null;
    static Configuration cfg = null;

    static {
        //1、加载核心配置文件
        cfg = new Configuration();
        cfg.configure();

        //2、创建sessionFactory
        seFactory = cfg.buildSessionFactory();
    }

    //构造方法返回sessionFactory
    public static SessionFactory getSessionFactory() {
        return seFactory;
    }
}

2) 实体类

package cn.lzb.entity;

public class User {
    private Integer id;
    private String uname;
    private String password;
    private String Address;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", uname=" + uname + ", password=" + password + ", Address=" + Address + "]";
    }
}

3) 测试类

package cn.lzb.hibernatetest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.lzb.entity.User;
import cn.lzb.utils.SessionFactoryUtils;

public class BaseIdQueryDemo {

    @Test
    public void BaseIdQuery() {
        //1、调用SessionFactory工具类得到SessionFactory
        SessionFactory sessionFactory = SessionFactoryUtils.getSessionFactory();

        //2、获取Session
        Session session = sessionFactory.openSession();

        //3、开启事务
        Transaction tx = session.beginTransaction();

        //4、根据Id查询,调用get方法
        User user = session.get(User.class, 3);//第一个参数是实体类的class,第二个是id值
        System.out.println(user.toString());//需要实体类中有有一个toString方法。

        //5、提交事务

        //6、关闭
        session.close();
        sessionFactory.close();

    }
}

4) 输出结果:

--这一部分是本人设定的日志输出
Hibernate: 
    select
        user0_.ID as ID1_0_0_,
        user0_.UNAME as UNAME2_0_0_,
        user0_.PASSWORD as PASSWORD3_0_0_,
        user0_.ADDRESS as ADDRESS4_0_0_ 
    from
        USER user0_ 
    where
        user0_.ID=?

-- 这一部分是代码示例输出(根据Id查询)
User [id=3, uname=lalala, password=loveHPH, Address=LL]

你可能感兴趣的:(hibernate)