最近全注解的SSH框架整合比较流行,springMvc+hibernate,令我们的代码精简不少。附上我最近学习hibernate注解的demo。
首先是hibernate.cfg.xml文件:(默认直接放在src文件夹下)
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- xml配置文件生效方式 -->
<!--<mapping resource="com/weiwei/learnDemo/student.hbm.xml" />-->
<!-- 注解生效方式生效方式 -->
<mapping class="com.weiwei.learnDemo.Student" />
</session-factory>
</hibernate-configuration>
实体类(新建一个学生信息类)
package com.test.learnDemo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity//将一个类声明为一个实体bean(即一个持久化POJO类)
@Table(name="t_student",schema="test")//注解声明了该实体bean映射指定的表(table)schema
public class Student
{
public Student(int id,String name,String password,int age)
{
this.id = id;
this.name = name;
this.password = password;
this.age = age;
}
public Student()
{
}
@Id//用来注册主属性,@GeneratedValue用来注册主属性的生成策略,@Column用来注册属性,@Version用来注册乐观锁
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="t_id",nullable = false,unique = true)
private int id;
@Column(name="t_name")
private String name;
@Column(name="t_pwd")
private String password;
@Column(name="age")
private int age;
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 String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String toString()
{
return "[id:"+id+",name:"+name+",age:"+age+"]";
}
}
根据注解生成表的代码
package com.test.learnDemo;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class MainTest
{
public static void main(String[] args)
{
Configuration config = new AnnotationConfiguration().configure();
SchemaExport export = new SchemaExport(config);
export.create(true, true);
}
}
测试代码
package com.test.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tarena.entity.Student;
import junit.framework.TestCase;
public class HibernateTest1 extends TestCase {
//保存一个student对象到t_student表中
public void testSave(){
// 加载hibernate.cfg.xml配置文件
Configuration configuration =
new Configuration().configure("hibernate.cfg.xml");
// 创建SessionFactory
SessionFactory sessionFactory =
configuration.buildSessionFactory();
// 通过SessionFactory创建session
// session:hibernate所有的操作都围绕session来实现
// sessionFactory.openSession():创建一个新的sesssion对象
Session session = sessionFactory.openSession();
// 保存(student对象)
// 创建对象
// 事务:对表的任何修改,必须要开启事务
// Hibernate:手动提交事务
// 开启事务(Transaction)
Transaction transation = session.getTransaction();//获得事务
transation.begin();//开启事务
Student student = new Student();
student.setName("shishi");
student.setPwd("123456");
student.setAge(33);
session.save(student);
transation.commit();//提交事务
}
}
附上jar包的截图,j