Hibernate使用JPA注解声明一个PO类

来源:互联网 发布:图像处理常用滤波算法 编辑:程序博客网 时间:2024/05/22 04:39
这里写代码片开发步骤第一步:在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>        <!-- 配置连接数据库 -->        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>        <!-- 配置方言是非常重要!!!我这里配置的为MySQL5Dialect -->        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>        <!-- 配置C3P0连接池 -->        <property name="hibernate.c3p0.max_size">200</property>        <property name="hibernate.c3p0.min_size">2</property>        <property name="hibernate.c3p0.max_statements">50</property>        <property name="hibernate.c3p0.timeout"></property>        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <property name="hbm2ddl.auto">update</property>        <!-- 配置映射文件 -->        <mapping  class="cn.codekong.entity.Students"/>    </session-factory></hibernate-configuration>第二步编写持久化类package cn.codekong.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="t_StuInfo")public class Students{    @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    private int sid;    private String sname;    private String gender;    private String address;    public Students() {    }    public Students(int sid, String sname, String gender, String address) {        super();        this.sid = sid;        this.sname = sname;        this.gender = gender;        this.address = address;    }    public int getSid() {        return sid;    }    public void setSid(int sid) {        this.sid = sid;    }    public String getSname() {        return sname;    }    public void setSname(String sname) {        this.sname = sname;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    @Override    public String toString() {        return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", address=" + address + "]";    }}第三步测试import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.service.ServiceRegistry;import org.junit.After;import org.junit.Before;import org.junit.Test;import cn.codekong.entity.Students;public class StudentsTest {    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;    @Before    public void init() {        // 创建服务注册对象        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();        // 创建会话工厂对象        sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();        // 会话对象        session = sessionFactory.openSession();        // 开启事物        transaction = session.beginTransaction();    }    @After    public void destory() {        // 提交事物        transaction.commit();        // 关闭会话        session.close();        // 关闭会话工厂        sessionFactory.close();    }    @Test    // 向数据库中添加数据    public void testSaveStudents() { // 生成学生对象        Students student = new Students(1, "小丽", "女", "成都");        System.out.println(student);        session.save(student);        System.out.println(session);    }}结果:Hibernate:     create table t_StuInfo (       sid integer not null auto_increment,        address varchar(255),        gender varchar(255),        sname varchar(255),        primary key (sid)    ) engine=MyISAMStudents [sid=1, sname=小丽, gender=女, address=成都]Hibernate:     insert     into        t_StuInfo        (address, gender, sname)     values        (?, ?, ?)SessionImpl(PersistenceContext[entityKeys=[EntityKey[cn.codekong.entity.Students#1]],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])
原创粉丝点击