下面写的东西是博主学习的时候的笔记,如果有错漏或者看不懂,请留言!能互相进步是件不错的事情,谢谢!
************************************************************
1、导入我们的jar包
window->perferences->java->build path->user libraries
在这里new新建一个library
如下图
2、新建java project
2.1、导入library
导入相应的数据库驱动程序
2.2、写配置文件
在src目录下面新建hibernate.cfg.xml文件
<?xml version='1.0' encoding='utf-8'?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 这里是数据库的驱动程序 -->
<property name="connection.url">jdbc:mysql://localhost/hibernate</property> <!-- 这里是数据库的链接 hibernate填响应的数据库名
<property name="connection.username">root</property> <!--这里是数据库的用户名 -->
<property name="connection.password">password</property> <!--这里是数据库的密码 -->
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库方言 在文档可以找到响应的参数 -->
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- 勾上了 -->
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping class="bat.test.model.Student" /> <!-- 使用annotation 这里直接写上类名即可-->
</session-factory>
</hibernate-configuration>
这个文件一定要记住在文档里面拷贝
3、新建类
这个项目有两个类
一个是model类
model代码
package bat.test.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
//@Table(name="Student", catalog="hibernate")
public class Student {
private int id;
private String name;
private int age;
@Id
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
另一个用于测试的
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import bat.test.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
//s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration();
cfg.configure();//读取配置文件
ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().
applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);
Session session = factory.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
factory.close();
}
}
建相应的数据库和数据表
CREATE TABLE `Student` (
`id` int(11) ,
`name` varchar(20) ,
`age` int(11)
)
现在可以运行测试了