ORM框架入门实例5

基于Hibernate的crud

1.导入依赖

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.创建实体类

public class User {

    private Integer id;

    private String name;

    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(Integer id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

3.实现Hibernate配置

<hibernate-configuration>
    <!-- 通常,一个session-factory节点代表一个数据库 -->
    <session-factory>

        <!-- 1. 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_test?serverTimezone=GMT%2B8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">******</property>
        <!--
            数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>


        <!-- 2. 其他相关配置 -->
        <!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 2.2 格式化sql -->
        <property name="hibernate.format_sql">true</property>
        <!-- 2.3 自动建表  -->
<!--        <property name="hibernate.hbm2ddl.auto">create</property>-->
        <!-- 2.4 保证线程有唯一session实例 -->
        <property name="hibernate.current_session_context_class">thread</property>
        <!--3. 加载所有映射-->
        <mapping resource="User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

<!--User.hbm.xml-->
<?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>

    <class name="com.q.pojo.User" lazy="true" table="user">
        <id name="id" column="id">
        </id>
        <property name="password" column="password"/>
        <property name="name" column="name"/>
        <!--        <many-to-one name="group" column="groupid" cascade="save-update"/>-->
        <!--        <one-to-one name="idCard" constrained="true" cascade="save-update"/>-->
    </class>
</hibernate-mapping>

4.实现dao层

public interface UserDao {
    public boolean add(User user);
    public boolean delete(Integer id);
    public boolean update(User user);
    public User get(Integer id);
}
//dao层实现类
public class UserDaoImpl implements UserDao {


    SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = null;


    @Override
    public boolean add(User user) {
        try {
            tx = session.beginTransaction();// 4、开始一个事务
            session.save(user);
            tx.commit();// 6、 提交事务
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
            return false;
        } finally {
            session.close();
        }
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        try {
            tx = session.beginTransaction();// 4、开始一个事务
            User user = session.get(User.class, id);
            if (user == null) {
                return false;
            }
            session.delete(user);
            tx.commit();// 6、 提交事务
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
            return false;
        } finally {
            session.close();
        }
        return true;
    }

    @Override
    public boolean update(User user) {
        try {
            tx = session.beginTransaction();// 4、开始一个事务
            if (user == null) {
                return false;
            }
            session.update(user);
            tx.commit();// 6、 提交事务
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
            return false;
        } finally {
            session.close();
        }
        return true;
    }

    @Override
    public User get(Integer id) {
        User user = null;
        try {
            tx = session.beginTransaction();// 4、开始一个事务
            user = session.get(User.class, id);
            if (user == null) {
                return null;
            }
            tx.commit();// 6、 提交事务
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
            return null;
        } finally {
            session.close();
        }
        return user;
    }
}

5.测试

private UserDaoImpl userDao = new UserDaoImpl();

    @org.junit.Test
    public void addTest() {
        User user = new User();
        user.setId(1);
        user.setName("1");
        user.setPassword("110");
        boolean add = userDao.add(user);
        if(add) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    @org.junit.Test
    public void updateTest() {
        User user = new User();
        user.setId(1);
        user.setName("2");
        user.setPassword("119");
        boolean update = userDao.update(user);
        if(update) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }

    @org.junit.Test
    public void getTest() {
        User user = userDao.get(1);
        System.out.println(user.toString());
    }

    @org.junit.Test
    public void deleteTest() {
        boolean delete = userDao.delete(1);
        if(delete) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值