首页 > 代码库 > hibernate-4.3.5安装配置
hibernate-4.3.5安装配置
起初照着官方文档配,一直出错,貌似官方的文档时错的,查了非常多资料,综合整理了一个可行的方案,例如以下:
0.1包结构
test.demo
test.domain //实体类
test.util//工具类
0.2导如的jar包
hibernate-4.3.5的required包中的全部
optional包中的c3p0中的全部
下载slf4j,导入slf4j-api.jar 和 slf4j-log4j.jar
下载log4j, 导入log4j.jar
导入mysql-connector-java.jar
Log4j和slf4j的关系:http://blog.csdn.net/lifuxiangcaohui/article/details/7278595
apache-log4j-1.2.17 http://pan.baidu.com/share/link?shareid=121565833&uk=2047106924
slf4j-1.7.7 http://pan.baidu.com/share/link?shareid=123250651&uk=2047106924
1.POJO例如以下
package test.domain; public class Message { private Long id; private String text; private Message nextMessage; public Message(String text){ this.text = text; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Message getNextMessage() { return nextMessage; } public void setNextMessage(Message nextMessage) { this.nextMessage = nextMessage; } }
2.与之相应的映射文件Message.hbm.xml,注意,要与POJO放在一起
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="test.domain.Message" table="MESSAGES"> <id name="id" column="MESSAGE_ID"> <generator class="increment"></generator> </id> <property name="text" column="MESSAGE_TEXT"></property> <many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID" foreign-key="FK_NEXT_MESSAGE"></many-to-one> </class> </hibernate-mapping>
3.hibernate.cfg.xml,即hibernate的配置文件,放在src的根文件夹下
<?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://127.0.0.1:3306/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <!-- Use the c3p0 connection pool provider --> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.timeout">300</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- Show and print nice SQL on stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hbm2ddl.auto">update</property> <!-- List of XML mapping files --> <mapping resource="test/domain/Message.hbm.xml" /> </session-factory> </hibernate-configuration>
4.HibernateUtil.java工具类
package test.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration .buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
5.測试
package test.demo; import org.hibernate.Session; import test.domain.Message; import test.util.HibernateUtil; public class HelloWorld { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Message message = new Message("Hello,world"); session.save(message); session.getTransaction().commit(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。