现在的位置: 首页 > 综合 > 正文

搭建struts2 + spring + hibernate(二):XML、代码篇

2013年08月27日 ⁄ 综合 ⁄ 共 2168字 ⁄ 字号 评论关闭

1. 配置struts.xml至src目录下

2. 编写相关BEAN,ACTION,SERVICE,DAO接口及其实现类:
部分代码如下:
con = new Configuration().configure();
sessionFactory = con.buildSessionFactory();
Session session = sessionFactory.openSession();
// 开始事物
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(User.class);

3. 编写hibernate.cfg.xml至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://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">******</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache  
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/infoq/bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

4. 编写User.hbm.xml至bean包下:

<?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.infoq.bean">

<class name="User">
<id name="id">
<generator class="identity" />
</id>
<property name="username" column="user_name" type="string"/>
</class>

</hibernate-mapping>

5. 编写applicationContext.xml至WEB-INF目录下。
6.web.xml增加:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

抱歉!评论已关闭.