Hibernate_4.3.5_001_XML_HelloWorld

来源:互联网 发布:模拟退火算法c 编辑:程序博客网 时间:2024/06/05 10:54

结构


1、搭建目录结构


2、各种分层文件源码
  •        UserDao

      

package com.jxau.hibernate.dao;import com.jxau.hibernate.model.User;public interface UserDao {public void save(User user);}
  • UserDaoImpl

 

package com.jxau.hibernate.dao.impl;import org.hibernate.Session;import com.jxau.hibernate.dao.UserDao;import com.jxau.hibernate.model.User;import com.jxau.hibernate.util.HibernateUtil;public class UserDaoImpl implements UserDao {@Overridepublic void save(User user) {Session session = HibernateUtil.getSessionFactory().openSession();try {session.beginTransaction();session.save(user);session.getTransaction().commit();} catch (Exception e) {e.printStackTrace();session.getTransaction().rollback();}finally {if(session != null) {session.close();session = null;}}}}

  • User

package com.jxau.hibernate.model;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;public class User {private int id;private String username;private String password;private Date birthday;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getBirthday() {return birthday;}@Columnpublic void setBirthday(Date birthday) {this.birthday = birthday;}}

  • User.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 package="com.jxau.hibernate.model"> <class name="User" table="t_user">        <id name="id" column="id">            <generator class="native"/>        </id>        <property name="username"/>        <property name="password"/>        <property name="birthday" type="timestamp"/>    </class></hibernate-mapping>

  • UserService

package com.jxau.hibernate.service;import com.jxau.hibernate.model.User;public interface UserService {public void add(User user) ;}

  • UserServiceImpl

package com.jxau.hibernate.service.impl;import com.jxau.hibernate.dao.UserDao;import com.jxau.hibernate.dao.impl.UserDaoImpl;import com.jxau.hibernate.model.User;import com.jxau.hibernate.service.UserService;public class UserServiceImpl implements UserService {private UserDao userDao = new UserDaoImpl();@Overridepublic void add(User user) {userDao.save(user);}}

  • HibernateUtil
package com.jxau.hibernate.util;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;public class HibernateUtil {    private static final SessionFactory sessionFactory = buildSessionFactory();    private static SessionFactory buildSessionFactory() {    SessionFactory sessionFactory = null;        try {            // Create the SessionFactory from hibernate.cfg.xml            sessionFactory =   new Configuration().configure().buildSessionFactory(    new StandardServiceRegistryBuilder().build() );            System.out.println(sessionFactory.getClass());        }        catch (Throwable ex) {            // Make sure you log the exception, as it might be swallowed            System.err.println("Initial SessionFactory creation failed." + ex);            throw new ExceptionInInitializerError(ex);        }        return sessionFactory;    }    public static SessionFactory getSessionFactory() {        return sessionFactory;    }}
3、配置文件
hibernate.properties
hibernate.connection.driver_class = com.mysql.jdbc.Driverhibernate.connection.url = jdbc:mysql://localhost/hibernatehibernate.connection.username = roothibernate.connection.password = roothibernate.c3p0.min_size=5hibernate.c3p0.max_size=20hibernate.c3p0.timeout=1800hibernate.c3p0.max_statements=50hibernate.dialect = org.hibernate.dialect.MySQL5Dialectcurrent_session_context_class = threadcache.provider_class = org.hibernate.cache.internal.NoCacheProvidershow_sql = truehbm2ddl.auto =update
注:hibernate官方文档把hibernate.properties的属性,写在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>        <mapping resource="com/jxau/hibernate/model/User.hbm.xml"/>         <!-- <mapping class="com.jxau.hibernate.model.User"/> -->    </session-factory></hibernate-configuration>


4、测试类

package com.jxau.hibernate.service.impl;
import java.util.Date;
import org.junit.Test;
import com.jxau.hibernate.model.User;import com.jxau.hibernate.service.UserService;
public class UserServiceImplTest {  @Test public void testAdd() {  User user = new User();  user.setUsername("tomcat");  user.setPassword("manager");  user.setBirthday(new Date());  UserService userService = new UserServiceImpl();  userService.add(user); }}
5、导入的包
antlr.jar   ANTLR(ANother Tool for Language Recognition)风头正盛,经常可以看到用它做语法解释器的项目,比如Hibernate就在3.0的时候换上它来解释HQL,使HQL的语法获得了加强
dom4j.jar    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.
hibernate-core.jar  hibernate的核心包,封装了hibernate的核心类库
hibernate-jpa.jar  Java Persistence API分析
JPA的宗旨是为了POJO提供持久化标准规范,JPA框架中支持大数据量。事务、并发等容器级别事务,JPA基于非侵入式原则设计,因此很融合框架或者容器
jandex.jar  
javassist.jar  Javassist是一个开源的分析、编辑和创建Java字节码的类库。它已加入了开放源代码JBoss 应用服务器项目,通过使用Javassist对字节码操作为JBoss实现动态AOP框架。
jboss-logging.jar
jboss-logging.annotions.jar
jobss-transaction-specc.jar

6、数据库 
create database hibernate;
use hibernate;
create table t_user (
id primary key auto_increment,
username varchar(12),
password varchar(20),
birthday timestamp
);




   

0 0
原创粉丝点击