SSM框架整合+阿里巴巴数据库连接池

来源:互联网 发布:妙笔生花绘画软件 编辑:程序博客网 时间:2024/06/05 10:29

本文以简单的注册登录为例,演示SpringSpringMVCmybatis框架的集成,采用alibb的数据库连接池的jar

并加入日志。

1.应用mvc模型搭建项目的框架。


文件夹说明:

1.1.controller包:控制层,将客户端提交到服务端的数据进行分发,让service层去处理。

1.2.service包:把控制层提交的数据根据业务规则,去处理,有关数据库的调用dao层处理。

1.3.dao包:主要对数据库进行操作。

1.4.pojo包:主要存放项目中的数据实体。

1.5.resources文件夹:存放配置文件。

1.6.lib:存放jar包。

1.7.view:存放jsp文件。

1.8.web.xml:配置servlet服务。

2.  配置日志

2.1导入jar包

log4j-1.2.17.jar

commons-logging-1.1.1.jar

2.2加入日志的配置文件

在resources文件夹中加入log4j.properties配置文件。

log4j.rootLogger=debug,appender1,appender2

log4j.appender.appender1=org.apache.log4j.FileAppender 

log4j.appender.appender1.File=d:/Log4JDemo02.log

log4j.appender.appender1.layout=org.apache.log4j.PatternLayout

log4j.appender.appender1.layout.ConversionPattern=%n[%d{yy/MM/ddHH:mm:ss:SSS}][%C-%M] %m

 

log4j.appender.appender2=org.apache.log4j.ConsoleAppender

log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout 

3.  配置spring容器。

3.1导入jar包

spring-aop-3.2.0.RELEASE.jar

spring-aspects-3.2.0.RELEASE.jar

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-context-support-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

spring-web-3.2.0.RELEASE.jar

spring-webmvc-3.2.0.RELEASE.jar


aopalliance.jar

aspectj-1.8.9.jar

3.2在resources中配置写spring的配置文件——spring-context.xml

3.2.1让spring去加载数据库连接池的配置文件

  <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

                   <property name ="locations" > 

          <list> 

                <value>classpath:dbconfig.properties</value> 

           </list > 

       </property>

</bean>

value对应的dbconfig.properties配置文件,该文件在resources目录下。

url=jdbc:mysql://localhost:3306/eshare

driverClassName=com.mysql.jdbc.Driver

username=root

password=123

filters=stat

maxActive=20

initialSize=1

maxWait=60000

minIdle=10

maxIdle=15

timeBetweenEvictionRunsMillis=60000

minEvictableIdleTimeMillis=300000

validationQuery=SELECT'x'

testWhileIdle=true

testOnBorrow=false

testOnReturn=false

maxOpenPreparedStatements=20

removeAbandoned=true

removeAbandonedTimeout=1800

logAbandoned=true


3.2.2导入alibb的数据库连接池jar包

druid-1.0.9.jar

3.2.3让spring去管理alibb的数据库连接池

<beanid="dataSource" class= "com.alibaba.druid.pool.DruidDataSource"destroy-method = "close">

         <!-- 数据库基本信息配置 -->

         <property name = "url"value = "${url}" /> 

         <property name = "username"value = "${username}" /> 

         <property name = "password"value = "${password}" /> 

         <property name = "driverClassName"value = "${driverClassName}" /> 

         <property name = "filters"value = "${filters}" /> 

          <!-- 最大并发连接数 -->

         <property name = "maxActive"value = "${maxActive}" />

         <!-- 初始化连接数量 -->

         <property name = "initialSize"value = "${initialSize}" />

         <!-- 配置获取连接等待超时的时间 -->

         <property name = "maxWait"value = "${maxWait}" />

         <!-- 最小空闲连接数 -->

         <property name = "minIdle"value = "${minIdle}" /> 

          <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

         <property name = "timeBetweenEvictionRunsMillis"value ="${timeBetweenEvictionRunsMillis}" />

         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

         <property name = "minEvictableIdleTimeMillis"value ="${minEvictableIdleTimeMillis}" /> 

         <property name = "validationQuery"value = "${validationQuery}" /> 

         <property name = "testWhileIdle"value = "${testWhileIdle}" /> 

         <property name = "testOnBorrow"value = "${testOnBorrow}" /> 

         <property name = "testOnReturn"value = "${testOnReturn}" /> 

         <property name = "maxOpenPreparedStatements"value ="${maxOpenPreparedStatements}" />

         <!-- 打开 removeAbandoned 功能 -->

         <property name = "removeAbandoned"value = "${removeAbandoned}" />

         <!-- 1800 秒,也就是 30 分钟 -->

         <property name = "removeAbandonedTimeout"value ="${removeAbandonedTimeout}" />

         <!-- 关闭 abanded 连接时输出错误日志-->  

         <property name = "logAbandoned"value = "${logAbandoned}" />

</bean>

3.3让spring管理mybatis的sqlSessionFactory工厂

3.3.1导入jar包

Spring与mybatis相关联的jar包:

            spring-jdbc-3.2.0.RELEASE.jar

    spring-tx-3.2.0.RELEASE.jar


    mybatis-spring-1.2.2.jar

Mybatis的jar和mysql数据库驱动

           mysql-connector-java-5.1.7-bin.jar

3.3.2启动sqlsessionFactory的工厂,并把DataSource数据库连接池注入进去,让mybatis用阿里巴巴连接池中的连接,并将mybatis的配置文件配置进去。

 

<!-- 启动sqlsessionFactory,注入DataSource连接池,并加载mybatis的配置文件 -->

     <bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

         <property name="dataSource"ref="dataSource"></property>

         <propertyname="configLocation" value="classpath:sqlMapConfig.xml"></property>

 </bean>

 

3.3.3在resources文件夹中新建mybatis的配置文件sqlMapConfig.xml. sqlMapConfig.xml是mybatis的核心配置文件,在ssm三个框架搭建的工程中主要作用是加载mapper.xml配置文件,在mybatis单独搭建的过程中该文件中还要配置数据源。

sqlMapConfig.xml文件:

<!DOCTYPEconfiguration

PUBLIC"-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<mappers>

<mapper resource="usermapper.xml"/><!--mapper扫描这个文件 -->

</mappers>

</configuration>

3.3.4在resources文件夹中新建mybatis的配置文件usermapper.xml,该配置文件主要是写sql语句的。

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEmapper

PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mappernamespace="com.zhangyike.dao.UserMapper">

 

<select id="getUser"resultType="com.zhangyike.pojo.User" parameterType="String">

select username,password from user whereusername = #{username};

</select>

                                                       

<select id="isContainsUser"resultType="int" parameterType="String">

select count(usename) from user where username = #{username};

</select>

                                                       

<insert id="insertUser"parameterType="com.zhangyike.pojo.User">

insert into user values(#{username},#{password});

</insert>

</mapper>

 3.4让spring管理mybatis的 mapper接口类

<!-- 让spring去管理这个userMapper对象,mybatis会把UserMapper这个接口类与userMaapper结合内部去实现接口。

                                                        并把sqlSessionFactory注入进去。 -->

     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

<property name="mapperInterface" value="com.zhangyike.dao.UserMapper"/>

<property name="sqlSessionFactory"ref="sqlSessionFactory"/>

</bean>

3.5让spring管理事物

<!-- 使用声明式事务配置,可以有效规范代码 -->

<!--spring事务管理器的声明,并且注入DataSource-->

<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource"ref="dataSource"/>

 </bean>

3.6让spring去启动事物

<!-- 启用事务注解   在bean的某个方法上面增加注解‘@Transactional’那么表示这个方法是在一个事务中执行-->

    <tx:annotation-driven transaction-manager="transactionManager"/>

3.7让spring去启动注解

<!-- 让注解生效 -->

         <context:annotation-config></context:annotation-config>

3.8让spring去扫描有注解的包,并让这些注解生效

<!-- 自动扫描这个包下的所有注解 -->

         <context:component-scan base-package="com.zhangyike"></context:component-scan>

 

4.  配置springmvc的配置文件

在resources目录下新建springmvc-config.xml文件

4.1.导入文件头

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

       http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc.xsd    

       http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

4.2.让springmvc扫描控制器的jar包

<!-- 扫描这个包里面的文件,这个包里的文件有control的控制器-->

         <context:component-scan base-package="com.zhangyike.controller"></context:component-scan>

 4.3.让spring框架中的注解起作用

<!-- 让spring框架的注解起作用 -->

         <mvc:annotation-driven></mvc:annotation-driven>

4.4.设置不被springmvc处理的文件夹

<!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->

         <mvc:resources location="/css/*" mapping="/css/*"></mvc:resources>

         <mvc:resources location="/fonts/*" mapping="/fonts/*"></mvc:resources>

         <mvc:resources location="/js/*" mapping="/js/*"></mvc:resources>

         <mvc:resources location="/img/*" mapping="/img/*"></mvc:resources>

         <mvc:resources location="/image/*" mapping="/image/*"></mvc:resources>

5.  配置web.xml文件

5.1配置spring配置文件的路径

<context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:spring_context.xml</param-value>

  </context-param>

5.2配置spring的监听器

    <!-- 配置spring的监听器,开启spring容器 -->

  <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener

</listener-class>

  </listener>

5.2配置servlet

<!-- 所有的请求交给Spring的分发器去处理,当服务器向客户端发起第一个请求的时候,根据映射找到class路径,Tomcat容器用反射创建dispatchServlet对象。

    当客户端再次发送请求的时候,先检查容器中有DispatcherServlet对象,开启一个新的线程去用这个对象,不再创建新的对象,所以Servlet是线程不安全的,但速度快。 -->

 <servlet>

 <servlet-name>myproject</servlet-name>

 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-初始参数是springmvc的路径参数,客户端有请求的时候去启动springmvc容器--->

<init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:springmvc-config.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

  <servlet-name>myproject</servlet-name>

 <url-pattern>*.action</url-pattern>

</servlet-mapping>

5.3配置设置文件编码的过滤器

<filter>

         <filter-name>encodingFilter</filter-name>

         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

         <init-param>

           <param-name>encoding</param-name>

           <param-value>utf-8</param-value>

         </init-param>

         <init-param>

           <param-name>forceEncoding</param-name>

           <param-value>true</param-value>

         </init-param>

    </filter>

   

    <!-- 对于所有的jsp文件而言,根据encodingFilter找到对应的编码 -->

    <filter-mapping>

          <filter-name>encodingFilter</filter-name>

          <url-pattern>*.action</url-pattern>

    </filter-mapping>

   

    <filter-mapping>

          <filter-name>encodingFilter</filter-name>

          <url-pattern>*.jsp</url-pattern>

    </filter-mapping>

6.总结

本文用阿里巴巴数据库连接池作为链接搭建了ssm框架,详细介绍了项目搭建过程中

各个xml配置的步骤,总体思路如下:

1.配置spring框架

2.spring去管理alibaba的数据库连接池。

     2.1数据源的配置

     2.2配置DataSource

3.spring去管理mybatissqlSessionFactory工厂,并把alibbDataSource注入进去,让mybatis去用alibb的链接

4.配置mybatis的配置文件。

5.配置web.xml文件,开启spring.xml,客户端有请求的时候加载springmvc.xml文件,启动springmvc。

0 0