Hibernate-操作数据库-类对象插入-1
Posted PushyTao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate-操作数据库-类对象插入-1相关的知识,希望对你有一定的参考价值。
-1为非带注解版本
基于hibernate的小项目(小的不能再小了,还有很多功能没有加上)带完善版本
工程框架:
Student类:
package cn.edu.ldu.entity;
public class Student {
private int id;
private String name;
//必须要有一个无参的构造方法
//如果写了一个有参构造,必须要有一个午餐的构造方法写出来
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="url">
jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
</property>
<!-- ?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC-->
<property name="driverClassName"> com.mysql.cj.jdbc.Driver </property>
<property name="username">root</property><!--connection.-->
<property name="password">123456</property>
<!-- DB schema will be updated if needed -->
<property name="connection.provider_class">
com.alibaba.druid.support.hibernate.DruidConnectionProvider </property>
<property name="filters">stat</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.hibernate.hbm2ddl.auto">update</property>
<!-- 进行修改-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- 加入映射-->
<mapping resource="Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
log4j.properties:
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
## 2021-05-10取消注释
log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
## log4j.logger.org.hibernate.type=info
log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
Student.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="cn.edu.ldu.entity.Student" table="student" catalog="hibernate">
<id name="id"> <generator class="native"/> </id><!--代表主键-->
<property name="name" type="string" length="255"/>
<!-- <property name="" type="" length=""></property>-->
</class>
</hibernate-mapping>
StudentTest类:
package cn.edu.ldu.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
public class StudentTest {
public static void main(String[] args) {
Configuration config = new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Student student = new Student();
student.setName("PushyTao");
session.beginTransaction(); //完整性一致性
session.save(student);
session.getTransaction().commit(); //提交
session.close();
sessionFactory.close();
}
/*JUnit调试
@Before
public void testJunit(){
System.out.println("before");
}
@Test
public void testInsert(){
System.out.println("test the insert");
}
@After
public void testUpdate(){
System.out.println("after");
}*/
}
数据库结构以及表的设计
主键为ID并且自动递增
以上是关于Hibernate-操作数据库-类对象插入-1的主要内容,如果未能解决你的问题,请参考以下文章