ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表
Spring整合Hibernate
Spring的Web项目中,web.xml文件会自动加载,以出现欢迎首页。也可以在这个文件中对Spring的配置文件进行监听,自启动配置文件,
以及之前Struts2框架必备的启动过滤器StrutsPrepareAndExecuteFilter
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>2017-12-30_SSH</display-name> <!-- 监听的文件名 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param> <!-- 服务器启动自动加载XML配置文件 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- structs2过滤器 -->
<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> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
在Spring的核心配置文件中,进行数据库连接池配置,建立sessionFactory对象,直接dao操作,也可以在Spring配置文件中配置Struts2的Action对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- c3p0连接池得到dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lastday"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <bean id="userAction" class="com.swift.action.UserAction" scope="prototype"></bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- Hibernate核心配置文件没有连接数据库,所以需要注入 -->
<property name="dataSource" ref="dataSource"></property>
<!-- Hibernate核心配置文件的位置 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean> </beans>
Hibernate的核心配置文件中,不需要在写连接数据库的属性,因为已经在Spring的配置文件中用连接池了
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sw_database</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property> <property name="hibernate.show_sql">true</property> <!-- create: 先删表,再建表。 create-drop: 启动时建表,退出前删表。 update: 如果表结构不一致,就创建或更新。
validate: 启动时验证表结构,如果不致就抛异常。 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!--指定映射文件,可映射多个映射文件 -->
<mapping resource="com/swift/entity/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Hibernate的这个核心配置文件需要实体类映射文件,体现映射关系
<?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.swift.entity.User" table="t_user">
<!-- 主键 -->
<id name="uid">
<generator class="native"></generator>
</id>
<!-- 其他属性 -->
<property name="username"/>
<property name="address"/>
</class> </hibernate-mapping>
实体类的属性设置setter和getter方法即可
package com.swift.entity;
public class User {
private Integer uid;
private String username;
private String address;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Struts2的UserAction类代码
package com.swift.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("action..................");
return NONE;
}
}
需要继承ActionSupport类,覆写execute()方法
在浏览器中运行项目后的地址,加上对象名.action(如userAction.action)就可以了
<?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>
<package name="default" extends="struts-default" namespace="/"> <!-- action的class不要写全名会创建两个对象,而写Spring配置文件中id的内容,只建一个对象
前提有struts2-spring-plugin-2.3.4.1.jar --> <action name="userAction" class="userAction">
</action>
</package>
</struts>
ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表的更多相关文章
- ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect方言解决无法服务器启动自动update创建表问题
除之前的Spring相关包,还有structs2包外,还需要Hibernate的相关包 首先,Spring整合其他持久化层框架的JAR包 spring-orm-4.2.4.RELEASE.jar ( ...
- SpringBoot使用Hibernate,实现自动创建数据库表【博客数据库设计】
我们准备设计博客,那就要设计数据库. 我们可以使用Hibernate来自动生成数据库. 博客数据库的结构: 实体类: 博客 Blog 博客分类 Type 博客标签 Tag 博客评论 Comment 用 ...
- ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作
UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...
- SSH(Spring SpringMVC Hibernate)框架整合
项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构 1.导入依赖jar包 <!--单测--> <dependency&g ...
- SSH框架之Spring+Struts2+Hibernate整合篇
回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...
- Spring+Struts2+Hibernate的整合
这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形 ...
- spring和hibernate的整合
阅读目录 一.概述 二.整合步骤 1.大致步骤 2.具体分析 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让H ...
- Spring 和 Hibernate的整合
问题 ,spring 和 hibernate 整合 如何整合 1. Spring 使用Hibernate的的SessionFactory 2. Hibernate使用Spring提供的声明式事务
- hibernate动态创建数据库表名几种方式
数据库中数据量很大, 但又不可以删除时同时又要优化程序检索数据时间. 答:方式有很多比如 创建数据库表分区,创建索引, 存储过程等; 我这里采用动态创建数据库表的方式. 完全可以在不创建表分区情况下实 ...
随机推荐
- qsc52(三角形线性插值)
题目链接:http://qscoj.cn/problem/52/ 题意:中文题诶- 思路:水题,只要知道三角形插值和判断点在三角形内就OK了 关于三角形插值:http://www.cnblogs.co ...
- cf706C(dp)
题目链接:http://codeforces.com/problemset/problem/706/C 题意:给出n个字符串,反转第 i 个字符串需要花费 ai,问通过反转操作将n个字符串变成升序排列 ...
- 2014-9-13 NOIP模拟赛
NOIP2014模拟赛 ——lwher 题目名 环上的游戏 舞蹈课 数位和乘积 源文件 cycle.cpp/c/pas dancingLessons.pas/cpp digit.cpp.cpp/c/p ...
- cinder存储服务
一.cinder 介绍: 理解 Block Storage 操作系统获得存储空间的方式一般有两种: 1.通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文件系 ...
- JS高级学习历程-11
[继承] 在php,一个类去继承另一个类,本类实例化出来的对象,既可以调用本身类的成员,也可以调用父类的成员. 在javascript继承主要通过原型实现,构造函数继承一个对象,构造函数的实例会拥有被 ...
- MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe)解决办法(来源网络)
MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe) 启动的时候,报上述错误,从这个报错来看,多半是因为读取到了另外的my.cnf导致的 ...
- Codeforces 140C(二分、构造)
要点 可以贪心选数量最多的那三个构造 二分的话里面的check我不太会.正解是既然当前答案为\(k\)个,那每个物品最多只会出现\(k\)次,多余的丢掉,剩下的总数如果大于等于\(3k\)则true. ...
- 牛客练习赛41E(球的体积并)
球冠公式是\(\frac{\pi h^2(3R-h)}{3}\),这样再余弦公式用\(R_a\)和\(R_b\)导一导两个球冠的\(h\)就做完了.算是补了个camp时没做出来的小坑了. #inclu ...
- Serilog 是 ASP.NET Core 的一个插件,可以简化日志记录
[翻译] ASP.NET Core 利用 Docker.ElasticSearch.Kibana 来记录日志 原文: Logging with ElasticSearch, Kibana, ASP.N ...
- Zeppelin的入门使用系列之使用Zeppelin运行shell命令(二)
不多说,直接上干货! 前期博客 Zeppelin的入门使用系列之创建新的Notebook(一) 接下来,我将以ml-100k数据集,示范如何使用Spark SQL进行数据分析与数据可视化 因为 [ha ...