搭建struts2 + spring + hibernate(二):XML、代码篇
2013-01-26 00:15
309 查看
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>
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>
相关文章推荐
- 搭建整合Struts2,Spring,Hibernate的Web开发框架
- Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程
- Struts2 Spring3 Hibernate3 菜鸟教程 集成xml版本-1
- Struts2 Spring 整合, Spring 使用 configLocation属性 联接hibernate.cfg.xml 找到不Action
- SSH项目(struts+spring+hibernate)搭建_代码简化
- 关于struts2 、spring 、hibernate 的配置文件xml 的头文件
- Spring,Struts2,Hibernate简单搭建
- 关于搭建restful web service + springMVC + ibatis/hibernate的整合和XML配置意思和ssh+springMVC框架搭建
- 框架搭建(Struts2 + Spring + Hibernate)
- struts2 spring3.2 hibernate4.1 框架搭建 整合
- 搭建Struts2 + spring + hibernate环境
- Struts2——SSH (Spring+Struts+Hibernate)框架搭建之配置文件序列一
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- eclipse配置使spring hibernate struts2 的xml节点自动提示
- struts2 spring3 hibernate4 SSH框架搭建
- SSH开发环境的配置:web.xml、Spring集成hibernate和Spring集成Struts2
- Struts2 + Spring + Hibernate 整合 web.xml的一般配置
- SSH(Struts2 + Hibernate + Spring)环境的搭建
- Struts2 Spring3 Hibernate3 菜鸟教程 集成xml版本-2
- struts2,hibernate,spring三大框架整合的注册程序(xml 配置,mysql数据库)