其实去年就开始用Hibernate,但是一直感觉没有系统的开始学过Hibernate,于是准备花一个月的功夫准备学一下Hibernate。
1.使用maven管理依赖的jar包。
maven的pom文件大概就是这样的
<properties> <junit.version>4.10</junit.version> <spring.version>4.2.0.RELEASE</spring.version> <hibernate.version>5.0.0.Final</hibernate.version> <fileupload.version>1.3</fileupload.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.20.0-GA</version> </dependency> </dependencies>
2.在src的根目录下设置好hibernate的配置文件hibernate.cfg.xml
配置方式大概如下
<session-factory> <!-- Database connection settings 用的Mysql数据库 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://数据库地址:数据库端口号/数据库名</property> <property name="connection.username">root</property> <property name="connection.password">数据库登陆密码</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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="实体类路径.类名"/> </session-factory>
3.写一个demo,测试环境是否有配置好。
先建一个实体类
@Entity(name = "DOG")
public class Dog {
private static final long serialVersionUID = 7219216851705947642L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "seq_table")
@TableGenerator(name = "seq_table",
table = "seq_table",
pkColumnName = "PK_NAME",
pkColumnValue = "DOG",
valueColumnName = "TABLE_ID", allocationSize = 1,initialValue=1000)
private Long id;
@Column(name = "DOG_NAME", nullable = false, unique = true, length = 1000)
private String name;
@Column(name = "DOG_AGE", length = 2)
private int age;
public Long getId() {
return id;
}
public void setId(Long 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) {
if (age > 100 || age < 1) {
this.age = 1;
} else {
this.age = age;
}
}
public void say() {
// TODO Auto-generated method stub
System.out.println("汪 汪 汪");
}
@Override
public String toString() {
return "Dog [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
写一个验证的demo
public class HibernateUtil {
private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
//采用单例模式获得session
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return SESSION_FACTORY;
}
}
public class EventManager {
public static void main(String[] args) {
EventManager manager = new EventManager();
Dog dog = new Dog();
dog.setAge(10);
dog.setName("heshi");
manager.save(dog);
HibernateUtil.getSessionFactory().close();
}
public void save(Object object){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
if(null != object){
session.save(object);
}
session.getTransaction().commit();
}
}
数据库里面成功的创建的DOG表,且有一条记录才算成功啦。