hibernate之初学增删改查
2021-07-10 13:05
                         标签:hiberna   app   current   project   catch   style   数据   接收   nsa      项目搭建啥的看我的上一篇文章,我就不多逼逼了,接下来就贴代码了 工具类:   对象序列化是为了反序列化用的,比如将一个对象写入到文件,或者作为流的形式传给第三方,那么这个类必须实现Serializable接口,并且定义一个私有的常量SerializableID,不然就不能从文件中读取对象了,接收方也没法还原这个对象   实际上就是一个ID用到了  接下就是上面方法的代码实现:   接下来是User的实体类   接下来是对impl中的所有方法进行测试: 接下来是文件配置文件名固定用UserEntity.hbm.xml 接下来是Hibernate的配置文件:   hibernate之初学增删改查 标签:hiberna   app   current   project   catch   style   数据   接收   nsa    原文地址:http://www.cnblogs.com/ShaoXin/p/7090254.htmlpackage com.xinzhi.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
    public static SessionFactory sf;
    static {
        sf = new Configuration().configure().buildSessionFactory();
    }
    public static Session getSession() {
        return sf.openSession();
    }
}
package com.xinzhi.dao;
import java.io.Serializable;
import java.util.List;
import com.xinzhi.entity.UserEntity;
public interface UserDao {
    // 增加一个用户
    public void saveUser(UserEntity userEntity);
    // 删除一个用户
    public void deleteUser(Serializable id);
    // 修改一个用户
    public void updateUser(UserEntity userEntity);
    // 查询所有的用户
    public List
package com.xinzhi.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.xinzhi.dao.UserDao;
import com.xinzhi.entity.UserEntity;
import com.xinzhi.utils.HibernateUtil;
public class UserDaoImpl implements UserDao {
    @Override
    public void deleteUser(Serializable id) {
        Session session = null;
        Transaction beginTransaction = null;
        try {
            session = HibernateUtil.getSession();
            beginTransaction = session.beginTransaction();
            Object object = session.get(UserEntity.class, id);
            if (object != null) {
                session.delete(object);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            beginTransaction.commit();
            session.close();
        }
    }
    @Override
    public List
package com.xinzhi.entity;
public class UserEntity {
    private int id;
    private String username;
    private String pwd;
    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 getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String toString() {
        return "UserEntity [id=" + id + ", pwd=" + pwd + ", username="
                + username + "]";
    }
}
package com.xinzhi.test;
import java.util.List;
import org.junit.Test;
import com.xinzhi.dao.impl.UserDaoImpl;
import com.xinzhi.entity.UserEntity;
public class TestUser {
    @Test
    public void testDelete() throws Exception {
        UserDaoImpl userDaoImpl = new UserDaoImpl();
        userDaoImpl.deleteUser(2);
    }
    @Test
    public void testSelectAll() throws Exception {
        UserDaoImpl userDaoImpl = new UserDaoImpl();
        List
xml version="1.0"?>
DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
hibernate-mapping 
    package="com.xinzhi.entity">
    
    class name="UserEntity"  table="user">id name="id" column="id">
            generator class="native"/>
        id>property name="username" column="username">property>
        property name="pwd" column="pwd">property>
    class>
hibernate-mapping>
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.jdbc.Driverproperty>
        property name="hibernate.connection.username">rootproperty>
        property name="hibernate.connection.password">123456property>
        property name="hibernate.connection.url">jdbc:mysql://localhost:3306/webprojectproperty>property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>
        property name="hibernate.show_sql">trueproperty>
        property name="">property>
        mapping resource="com/xinzhi/entity/UserEntity.hbm.xml"/>
    session-factory>
hibernate-configuration>
上一篇:js 调用后台接口