spring + jdbc + extjs configuration

所有源代码能够訪问我的GitHub

有空没空的稻谷了几天,最终前后台跑通了,提供一套可用的配置文件。

(因为与extjs整合,spring security的登录须要重写原handler。会在后面补上)

首先是pom。主要有junit。spring(mvc + security)。log4j以及jdbc(mysql)依赖。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.paris</groupId>
	<artifactId>fast-contacts</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>fast-contacts Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<jstl.version>1.2</jstl.version>
		<junit.version>4.11</junit.version>
		<spring.version>4.0.0.RELEASE</spring.version>
		<servlet.version>3.0.1</servlet.version>
		<jsp.version>2.2</jsp.version>
		<tomcat.plugin.version>2.2</tomcat.plugin.version>
		<slf4j.version>1.5.6</slf4j.version>
		<spring.security.version>3.2.4.RELEASE</spring.security.version>
		<lombok.version>1.12.6</lombok.version>
		<log4j.version>1.2.16</log4j.version>
		<org.json.version>20090211</org.json.version>
		<common.dbcp.verison>1.4</common.dbcp.verison>
		<commmons.pool.version>1.6</commmons.pool.version>
		<mysql.connector.verison>5.1.6</mysql.connector.verison>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- spring webmvc相关jar包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- spring security configuration -->
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.springframework.security</groupId>
    		<artifactId>spring-security-config</artifactId>
    		<version>${spring.security.version}</version>
  		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>${common.dbcp.verison}</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>${commmons.pool.version}</version>
		</dependency>

		<!-- spring test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

  		<!-- lombok -->
  		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>${lombok.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>

		<!-- org json -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>${org.json.version}</version>
		</dependency>
		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.connector.verison}</version>
		</dependency>
	</dependencies>

	<build>
	<finalName>fast-contacts</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<version>${tomcat.plugin.version}</version>
			<configuration>
				<port>8080</port>
				<path>/</path>
				<uriEncoding>UTF-8</uriEncoding>
				<url>http://localhost:8080/manager/html</url>
				<server>tomcat7</server>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>1.7</source>
				<target>1.7</target>
				<showWarnings>true</showWarnings>
			</configuration>
		</plugin>
	</plugins>
	</build>
</project>

web.xml 主要是spring和spring security。

我用的html。假设喜欢jsp的,能够去掉html的filterMapping

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0" metadata-complete="false">
	<display-name>Fast contacts</display-name>

	<!--
      - Location of the XML file that defines the root application context
      - Applied by ContextLoaderListener.
      -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           classpath:spring-security.xml
           classpath:spring-servlet.xml
        </param-value>
    </context-param>

     <!-- Nothing below here needs to be modified -->

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>servletapi.root</param-value>
    </context-param>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <!--bak <url-pattern>/*</url-pattern> -->
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--
      - Loads the root application context of this web app at startup.
      - The application context is then available via
      - WebApplicationContextUtils.getWebApplicationContext(servletContext).
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
		<servlet-name>jsp</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
</web-app>

spring-security.xml 不全,可是可用。

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:security="http://www.springframework.org/schema/security"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/security
            http://www.springframework.org/schema/security/spring-security-3.2.xsd">

	<!-- no need to filter login page -->
	<security:http pattern="/login" security="none"/>
    <!-- 不要过滤图片等静态资源,当中**代表能够跨越文件夹。*不能够跨越文件夹。 -->
	<security:http pattern="**.jpg" security="none" />
	<security:http pattern="**.png" security="none" />
	<security:http pattern="**.gif" security="none" />
	<security:http pattern="**.css" security="none" />
	<security:http pattern="**.js" security="none" />
   	<security:http auto-config="true">
        <!--
        <security:intercept-url pattern="/auth/login" access="permitAll"/>
        <security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/>  -->
        <security:intercept-url pattern="/login.html" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:intercept-url pattern="/*" access="ROLE_USER"/>
        <security:form-login
        	login-page="/login.html"
        />
        <security:logout delete-cookies="JSESSIONID"/>
        <security:remember-me key="fast-contacts"/>
        <security:session-management invalid-session-url="/invalidSession.html"></security:session-management>
    </security:http>  

	<security:authentication-manager>
		<security:authentication-provider user-service-ref="fastUserDetailsService">
			<security:password-encoder ref="passwdEcoder"/>
			<!--
			<security:user-service>
				<security:user name="test" password="test" authorities="ROLE_USER"/>
				<security:user name="admin" password="admin" authorities="ROLE_USER,ROLE_ADMIN"/>
			</security:user-service>-->
		</security:authentication-provider>
	</security:authentication-manager>

	<b:bean id="fastUserDetailsService"
		class="com.chinadreamer.contacts.filter.FastUserDetailsService">
	</b:bean>
	<b:bean id="passwdEcoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>
</b:beans>

spring-servlet.xml db连接是mysql driver + jdbc

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- Scans the classpath of this application for @Components to deploy as
		beans -->
	<context:component-scan base-package="com.chinadreamer.contacts.*,demo.*">
			<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<mvc:annotation-driven />

	<!-- 静态资源映射 -->
    <mvc:resources mapping="/comp/**" location="/WEB-INF/comp/"/>
    <mvc:resources mapping="/pages/**" location="/WEB-INF/pages/"/>
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
    <mvc:resources mapping="/view/**" location="/WEB-INF/view/"/>

	 <!-- 对模型视图名称的解析。在请求时模型视图名称加入前后缀 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     p:prefix="/WEB-INF/view/"
     p:suffix=".html" />

	<!-- 配置数据源 -->
	<bean class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close"
		id = "dataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/fastcontacts"></property>
		<property name="username" value="root"/>
		<property name="password" value="Password2"/>
		<!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="10"/>
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="60000"/>
        <!-- 最大空暇值.当经过一个高峰时间后。连接池能够慢慢将已经用不到的连接慢慢释放一部分,一直降低到maxIdle为止 -->
        <property name="maxIdle" value="20"/>
        <!--  最小空暇值.当空暇的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="5"/>
	</bean>
	<!-- transaction -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       	<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 事务拦截器 -->
	<!-- 配置事务拦截器 -->
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   		<property name="transactionManager" ref="transactionManager"/>
   		<property name="transactionAttributes">
    	<props>
     		<prop key="*">PROPAGATION_REQUIRED</prop>
     		<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
     		<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
     		<prop key="request*">PROPAGATION_REQUIRED,readOnly</prop>
    	</props>
   		</property>
	</bean>
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   		<property name="beanNames">
    	<value>*Service</value>
   		</property>
   		<property name="interceptorNames">
    	<list>
     		<value>transactionInterceptor</value>
    	</list>
   		</property>
	</bean>
	<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
   		<property name="transactionInterceptor" ref="transactionInterceptor"/>
	</bean>

	<!-- jdbc template -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    	<property name="dataSource" ref="dataSource"/>
	</bean>  

</beans>
时间: 03-04

spring + jdbc + extjs configuration的相关文章

Spring JDBC详解

<Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.概述 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1)core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJdbcInsert类,SimpleJdbcCall类,以及NamedP

Spring JDBC 例子

http://www.yiibai.com/spring/spring_jdbc_example.html 要了解有关Spring JDBC框架与JdbcTemplate类的概念,让我们写这将实现所有在下面Student表的CRUD操作的简单例子. CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, PRIMARY KEY (ID) ); 在开始之前,

一个spring jdbc实例

一.使用示例 (1)springJdbcContext.xml Java代码   <?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

Spring JDBC 框架中, 绑定 SQL 参数的另一种选择:具名参数(named parameter)

使用spring的jdbcTemplate-----使用具名参数 在JDBC用法中,SQL参数是用占位符?表示,并且受到位置的限制,定位参数的问题在于,一旦参数的位置发生变化,必须改变参数的绑定,在Spring JDBC中,绑定SQL参数的另一种选择是使用具名参数,SQL具名参数是按照名称绑定,而不是位置绑定. 什么是具名参数? 具名参数: SQL 按名称(以冒号开头)而不是按位置进行指定. 具名参数更易于维护, 也提升了可读性. 具名参数由框架类在运行时用占位符取代 具名参数只在 NamedP

Unit06: Spring对JDBC的 整合支持 、 Spring+JDBC Template、Spring异常处理

Unit06: Spring对JDBC的 整合支持 . Spring+JDBC Template .Spring异常处理 1. springmvc提供的异常处理机制 我们可以将异常抛给spring框架,让spring来帮我们处理异常. (1)使用简单异常处理器 step1. 配置简单异常处理器. step2. 添加对应的异常处理页面. 注:该方式只适合处理简单异常的处理,如果要对异常做复杂处理,比如 记录日志等,则不合适了. (2)使用@ExceptionHandler注解 step1. 在处理

Spring JDBC入门

Spring将替我们完成所有使用JDBC API进行开发的单调乏味的.底层细节处理工作. 操作JDBC时Spring可以帮我们做这些事情: 定义数据库连接参数,打开数据库连接,处理异常,关闭数据库连接 我们仅需要关注: 声明SQL语句,处理每一次得到的结果 一个较为简单的例子与讲解 JdbcTemplate类 JdbcTemplate是core包的核心类.它替我们完成了资源的创建以及释放工作,从而简化了我们对JDBC的使用.它还可以帮助我们避免一些常见的错误,比如忘记关闭数据库连接.JdbcTe

Spring JDBC实现查询

1 db.properties 1 jdbc.user=root 2 jdbc.password=920614 3 jdbc.driverClass=com.mysql.jdbc.Driver 4 jdbc.jdbcUrl=jdbc:mysql:///spring?useUnicode=true&characterEncoding=utf-8 5 6 jdbc.initPoolSize=5 7 jdbc.maxPoolSize=10 2 applicationContext.xml 1 <?

Spring DAO vs Spring ORM vs Spring JDBC

Pat 的疑惑 最近关注于 Spring 提供的数据访问技术,对于 Spring 相关的这几个项目有何不同我不是太明白: Spring-DAO (http://docs.spring.io/spring/docs/2.0.8/reference/dao.html) Spring-ORM (http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/orm.html) Spring-JDBC (http://doc

Spring JDBC 访问数据库

Spring JDBC是Spring所提供的持久层技术,它以一种更直接.更简单的方式使用JDBC API.在Spring JDBC里,用户仅需要做那些必不可杀的事儿,而将资源获取.Statement创建.异常处理.资源释放等繁杂的工作交给Spring. 虽然ORM的框架已经很成熟,但是JDBC灵活直接的特性依旧让它有自己的用武之地. 本节的主要内容:使用JdbcTemplate模板类进行CRUD数据操作.BLOB和CLOB类型数据的操作,支持命名参数绑定NamedParameterJdbcTem