SSH框架搭建

自己一直在用java的一些框架,但是自己从来没有搭过,昨天正好有点时间,就自己搭了一个SSH框架,这个是比较常用的,今天把一些东西记录下,以便以后查看~~~(我是在eclipse下搭,如果是MyEclipse,它直接生成就行了,不需要什么配置以及包的导入了)
首先新建一个WEB项目,导入SSH框架需要的包,这个就不说了~~~
接下来看整个项目的关键部分,就是配置文件啦~~~
1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SimpleSSH</display-name>
	
	<!-- struts2的拦截器 -->
	<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>
 
 	<!-- spring的监听器 -->
 	<listener>
 		<listener-class>
 			org.springframework.web.context.ContextLoaderListener
 		</listener-class>
 	</listener>
 
 	<!-- spring配置文件 -->
 	<context-param>
 		<param-name>contextConfigLocation</param-name>
 		<param-value>
 			classpath:applicationContext*.xml
 		</param-value>
 	</context-param>
	
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

2.struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC     
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     
    "http://struts.apache.org/dtds/struts-2.0.dtd">    
<struts>
	<!-- 声明action由spring管理 -->
	<constant name="struts.objectFactory" value="spring" />
	<constant name="struts.i18n.encoding" value="utf-8"></constant>
	
	<package name="default" extends="struts-default">
		<!-- 此处action的class属性直接由spring管理,loginAction是在spring中的一个bean -->
		<action name="login" class="loginAction">
			<result name="success">/success.jsp</result>
			<result name="input">/login.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
	</package>
</struts>
 

3.hibernate.cfg.xml

<?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="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
		</property>
		<!-- 数据库url -->
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:orcl
		</property>
		<!-- 用户名 -->
		<property name="connection.username">wwd</property>
		<!-- 密码 -->
		<property name="connection.password">edison</property>
		<!-- 数据库方言 -->
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
		</property>
		<!-- 后台是否显示SQL -->
		<property name="show_sql">true</property>
		<!-- 根据model类生成表 -->
		<property name="hbm2ddl.auto">update</property>  
		<!-- 是否自动提交 -->
		<property name="connection.autocommit">true</property>
		
		<!-- 对应表xml文件 -->
		<mapping resource="test.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 4.test.hbm.xml

<?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.action.model.People" table="test" schema="wwd">
		<id name="id" type="java.lang.Long">
			<column name="id" not-null="true"/>
		</id> 
		<property name="name" type="java.lang.String">
			<column name="name" length="50" not-null="true"/>
		</property>
		<property name="age" type="java.lang.Integer">
			<column name="age" not-null="true"/>
		</property>
	</class>
</hibernate-mapping>

 5.applicationContext.xml(这个是最关键的,他整合的struts和hibernate )

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="autodetect" xmlns="http://www.springframework.org/schema/beans"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         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-2.0.xsd  
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
    <!-- 设置hibernate文件(其实数据源直接在spring的配置文件中设置也行的) -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	<!-- 事物处理(暂时没用) -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory"/>
		</property>
	</bean>
	
	<!-- 注入hibernateTemplate -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
	<!-- 抽象DAO的实现,要用到hibernateTemplate,这里我是直接注入的,你也可以让你的实现DAO继承自HibernateDaoSupport这样就可以直接getHibernateTemplate()获取 -->
	<bean id="testDaoImpl" class="com.action.dao.impl.TestDaoImpl">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate"/>
		</property>
	</bean>
	
	<!-- 与struts中的class相对应 -->
	<!-- 此处就是由spring来管理action -->
	<bean id="loginAction" class="com.action.LoginAction" scope="prototype">
		<property name="testDaoImpl">
			<ref bean="testDaoImpl"/>
		</property>
	</bean>
</beans>

 经过上面的配置之后,我们可以进行测试了~~~

 

测试代码:

1.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <s:form action="login" name="form1">  
    	<s:textfield name="userName" label="userName"/>  
    	<s:password name="password" label="password"/>  
    	<input type="submit" value="提交"/>  
    </s:form>
    <s:actionerror/>
</body>
</html>

 2.success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	this is a success page!
	<input type="button" οnclick="alert('ok')"/>
</body>
</html>

 3.People.java

public class People implements Serializable {
	private static final long serialVersionUID = 4372014843795725183L;
	
	private Long id;
	private String name;
	private Integer age;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

 4.testDaoImpl.java

public class TestDaoImpl implements BaseDao{

	private HibernateTemplate hibernateTemplate;
	
	@Override
	public void saveOrUpdate(Object obj) {
		hibernateTemplate.saveOrUpdate(obj);
	}

	public void test(){
		System.out.println("test");
	}
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}

 5.LoginAction.java

public class LoginAction extends ActionSupport{
	private static final long serialVersionUID = 5654569961853096359L;
	
	private TestDaoImpl testDaoImpl;

	public String execute(){
		People p = new People();
		p.setId(6L);
		p.setAge(15);
		p.setName("test");
		testDaoImpl.saveOrUpdate(p);
		
		return SUCCESS;
	}
	
	public TestDaoImpl getTestDaoImpl() {
		return testDaoImpl;
	}

	public void setTestDaoImpl(TestDaoImpl testDaoImpl) {
		this.testDaoImpl = testDaoImpl;
	}
}

 好了,这样启动服务器就能运行了,这只是一个很简单的SSH框架,要灵活的运用还要慢慢去深入和了解~~~

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
GeoPandas是一个开源的Python库,旨在简化地理空间数据的处理和分析。它结合了Pandas和Shapely的能力,为Python用户提供了一个强大而灵活的工具来处理地理空间数据。以下是关于GeoPandas的详细介绍: 一、GeoPandas的基本概念 1. 定义 GeoPandas是建立在Pandas和Shapely之上的一个Python库,用于处理和分析地理空间数据。 它扩展了Pandas的DataFrame和Series数据结构,允许在其中存储和操作地理空间几何图形。 2. 核心数据结构 GeoDataFrame:GeoPandas的核心数据结构,是Pandas DataFrame的扩展。它包含一个或多个列,其中至少一列是几何列(geometry column),用于存储地理空间几何图形(如点、线、多边形等)。 GeoSeries:GeoPandas中的另一个重要数据结构,类似于Pandas的Series,但用于存储几何图形序列。 二、GeoPandas的功能特性 1. 读取和写入多种地理空间数据格式 GeoPandas支持读取和写入多种常见的地理空间数据格式,包括Shapefile、GeoJSON、PostGIS、KML等。这使得用户可以轻松地从各种数据源中加载地理空间数据,并将处理后的数据保存为所需的格式。 2. 地理空间几何图形的创建、编辑和分析 GeoPandas允许用户创建、编辑和分析地理空间几何图形,包括点、线、多边形等。它提供了丰富的空间操作函数,如缓冲区分析、交集、并集、差集等,使得用户可以方便地进行地理空间数据分析。 3. 数据可视化 GeoPandas内置了数据可视化功能,可以绘制地理空间数据的地图。用户可以使用matplotlib等库来进一步定制地图的样式和布局。 4. 空间连接和空间索引 GeoPandas支持空间连接操作,可以将两个GeoDataFrame按照空间关系(如相交、包含等)进行连接。此外,它还支持空间索引,可以提高地理空间数据查询的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值