最少所需jar包
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!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="myeclipse.connection.profile">
mysql5.5-jdbc5.0.8
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<!--<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
-->
<!--use_second_level_cache -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="cache.use_query_cache">true</property>
<!--
<mapping resource="com/anllin/model/Student.hbm.xml" />
<mapping resource="com/anllin/model/Student2.hbm.xml" />
<mapping resource="com/anllin/model/StuIdCard.hbm.xml" />
<mapping resource="com/anllin/one2many/Person.hbm.xml" />
<mapping resource="com/anllin/one2many/Address.hbm.xml" />
-->
<!--
<mapping class="com.anllin.model.Teacher" />
<mapping class="com.anllin.model.Teacher2" />
<mapping class="com.anllin.model.Husband" />
<mapping class="com.anllin.model.Wife" />
<mapping class="com.anllin.component.Person" />
<mapping class="com.anllin.many2one.Person" />
<mapping class="com.anllin.many2one.Address" />
<mapping class="com.anllin.one2many.Person" />
<mapping class="com.anllin.one2many.Address" />
<mapping class="com.anllin.many2many.Teacher" />
<mapping class="com.anllin.many2many.Student" />
<mapping class="com.anllin.tree.Organization" />
-->
<mapping class="com.anllin.studentmgr.Student" />
<mapping class="com.anllin.studentmgr.Score" />
<mapping class="com.anllin.studentmgr.Course" />
</session-factory>
</hibernate-configuration>
ehcache.xml
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
~ indicated by the @author tags or express copyright attribution
~ statements applied by the authors. All third-party contributions are
~ distributed under license by Red Hat Middleware LLC.
~
~ This copyrighted material is made available to anyone wishing to use, modify,
~ copy, or redistribute it subject to the terms and conditions of the GNU
~ Lesser General Public License, as published by the Free Software Foundation.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
~ for more details.
~
~ You should have received a copy of the GNU Lesser General Public License
~ along with this distribution; if not, write to:
~ Free Software Foundation, Inc.
~ 51 Franklin Street, Fifth Floor
~ Boston, MA 02110-1301 USA
-->
<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--设置缓存的算法-->
<!-- memoryStoreEvictionPllicy="LRU" least Recently used-->
<!-- memoryStoreEvictionPllicy="LFU" least Frequently used-->
<!-- memoryStoreEvictionPllicy="FIFO" first in first out-->
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<!-- Sample cache named sampleCache2
This cache contains 1000 elements. Elements will always be held in memory.
They are not expired. -->
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>
log4j.properties
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
# indicated by the @author tags or express copyright attribution
# statements applied by the authors. All third-party contributions are
# distributed under license by Red Hat Middleware LLC.
#
# This copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the GNU
# Lesser General Public License, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this distribution; if not, write to:
# Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301 USA
#
#log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=debug
#log4j.logger.org.hibernate.test=info
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
#log4j.logger.org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator=trace
#log4j.logger.org.hibernate.ejb=debug
#log4j.logger.org.hibernate.ejb.packaging=debug
#log4j.logger.org.hibernate.reflection=debug
#log4j.logger.org.hibernate.hql.ast.QueryTranslatorImpl=trace
#log4j.logger.org.hibernate.hql.ast.HqlSqlWalker=trace
#log4j.logger.org.hibernate.hql.ast.SqlGenerator=trace
#log4j.logger.org.hibernate.hql.ast.AST=trace
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type.BasicTypeRegistry=trace
#log4j.logger.org.hibernate.engine.Cascades=debug
#log4j.logger.org.hibernate.hql=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=trace
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
#log4j.logger.org.jgroups=info
#log4j.logger.org.jboss.cache=trace
#log4j.logger.org.jboss.cache.RegionManager=info
#log4j.logger.org.jboss.cache.lock=info
#log4j.logger.org.jboss.cache.interceptors.PessimisticLockInterceptor=info
#log4j.logger.org.jboss.cache.interceptors.UnlockInterceptor=info
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.anllin.usermgr.model.User" dynamic-update="true" dynamic-insert="true">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="username" type="string">
<column name="username" length="20"/>
</property>
<property name="password" type="string">
<column name="password" length="20"/>
</property>
<property name="age" type="integer">
<column name="age"/>
</property>
<property name="birthday" type="date">
<column name="birthday"/>
</property>
</class>
</hibernate-mapping>