Struts就是使MVC模式应用于web程序设计。hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
1.先导lib
2.新建"hibernate.cfg.xml"文件
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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:3306/xgnfyd?</property>
<property name="connection.username">root</property>
<property name="connection.password">xxx</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</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>
<!-- 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>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<!-- Names the annotated entity class -->
<mapping class="cn.whb.model.UserInfo"/>
</session-factory>
</hibernate-configuration>
数据库的表要与model类对应,如UserInfo类对应数据库中user_info表,在UserInfo类应加入标注如下图所示
3.新建struct.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false"
/> <constant name="struts.devMode" value="true" /> <package name="default"
namespace="/" extends="struts-default"> <default-action-ref name="index"
/> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception"
result="error"/> </global-exception-mappings> <action name="index"> <result
type="redirectAction"> <param name="actionName">HelloWorld</param> <param
name="namespace">/example</param> </result> </action> </package> <include
file="example.xml"/> -->
<constant name="struts.devMode" value="true" />
<!-- 表示启用struts2的DMI -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!-- 表示允许OGNL访问类的静态方法 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="error"></default-action-ref>
<action name="error" class="cn.whb.action.ErrorAction"
method="doError">
</action>
</package>
<!--方法-->
<package name="user" namespace="/user" extends="default">
<action name="login" class="cn.whb.action.DoLogin"
method="doUserAction">
</action>
</package>
<!-- Add packages here -->
</struts>
4.在web.xml添加过滤
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其它
hibernate自动生成表
//读取配置文件
Configuration cfg = new Configuration().configure();
//创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);
//创建数据库表
export.create(true,true);