struts2+hibernate+mysql
来源:互联网 发布:java jna调用64位dll 编辑:程序博客网 时间:2024/06/10 22:09
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);
0 0
- struts2+hibernate+mysql
- hibernate +struts2+mysql中文乱码问题
- Struts2+Hibernate
- struts2+hibernate
- Hibernate+struts2
- Struts2+Spring+Hibernate连接mysql中文乱码解决
- Struts2+Hibernate+Spring 在mysql更新Blob类型的数据
- 内部顾客评价系统(java+struts2+hibernate+mysql)
- spring+hibernate+struts2应用mysql数据库乱码问题
- spring+hibernate+struts2应用mysql数据库乱码问题
- Eclipse整合SSH框架(Struts2+Spring+Hibernate+MySQL+Tomcat)
- S2SH框架整合(注解)Struts2+Spring+Hibernate+MySql
- struts2+hibernate+spring+ftl....
- struts2+hibernate+spring(1)
- struts2+hibernate+spring(2)
- struts2+spring+hibernate分页
- struts2+spring+hibernate
- Struts2与Hibernate整合
- Udacity深度学习-深度神经网络-assignment3
- TSP_旅行商问题
- Hibernate(9)懒加载问题解决方案
- WordCount代码实现详解
- Mint-ui MessageBox.confirm
- struts2+hibernate+mysql
- 'module' object has no attribute 'OP_NO_TLSv1_1'
- 5-5Underscore01
- 使用Anaconda搭建SimpleITK开发环境
- 前后台交互
- HashMap引起的事故:jdk1.7和jdk1.8下的hash函数的不同
- 2017sdut期末机试——和泉纱雾
- Android Am命令用法
- linux命令指南之三