2022年8月9日——hibernate的入门案例(1)

网友投稿 594 2022-10-22

2022年8月9日——hibernate的入门案例(1)

2022年8月9日——hibernate的入门案例(1)

start

描述:

使用hibernate实现增加一条数据。。。

效果演示:

步骤说明:

实现的步骤如下,导入jar或者书写pom.xml文件,导入依赖。。。

导入依赖编写hibernate的主配置文件编写实体类编写实体类对应的xml文件编写测试类

代码展示:

描述:

每一步的使用的代码

导入依赖

mysql mysql-connector-java 8.0.29 org.projectlombok lombok 1.18.24 org.hibernate hibernate-core 3.6.5.Final

数据库驱动

hibernate

lombok

编写hibernate的主配置文件

root root com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/db_hibernate?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 10 10000 5000 30 5 10 org.hibernate.dialect.MySQLDialect true true

编写实体类

package test.day_06.entity;import lombok.Data;@Datapublic class People { private Integer id; private String name; private Double money; public People() { } public People(Integer id, String name, Double money) { this.id = id; this.name = name; this.money = money; }}

编写实体类对应的xml文件

编写测试类

package test.day_06;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.Session;import test.day_06.entity.People;public class Demo1 { public static void main(String[] args) { Configuration configuration = new Configuration();// 可以名字为:hibernate.cfg.xml也可以为其它的名字,如果为其它的名字则重写一下即可// 方式一:非重写// Configuration configure = configuration.configure();// 方式二:重写 Configuration configure = configuration.configure("hibernate.cfg.xml"); SessionFactory sessionFactory = configure.buildSessionFactory(); Session session = sessionFactory.openSession(); People people = new People(1,"张三",23.2); session.save(people); session.beginTransaction().commit(); session.close(); }}

以上内容为,经过测试,可以运行的。

end

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:poj 2828 Buy Tickets(动态队列·线段树单点更新)
下一篇:JPA @Query时,无法使用limit函数的问题及解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~