SpringMVC+ibatis+MySQL+MongoDB構建博客系統(一)

一、引言

工作中用到了SpringMVC、ibatis、MySQL和velocity實現系統的Web部分,由於想構建一個博客系統,所以,使用這套架構構建一套博客系統,目前,已經實現了基本的註冊登錄功能。

二、架構

項目使用了Maven管理項目,通過Idea創建了Maven多模塊項目,整個項目的目錄結構如下圖所示。

其中模塊common爲封裝的一些工具,dao爲封裝的dao層,domain爲PO和VO,service爲封裝的服務層,web爲控制層,在web模塊中WEB-INF目錄下爲視圖如JSP或者vm文件。
使用Idea創建Maven多模塊項目,可以參考文章:http://www.tuicool.com/articles/3aaAVnZ
值得注意的是父模塊中的pom,<dependencyManagement>是用來管理導入的jar包版本,並不真正導入jar包,如果要在子模塊中使用jar包,仍需要寫<dependency>,只是不用再寫版本號了。

三、配置

3.1 web.xml

servlet相關配置,配置名字,類以及映射關係,需要注意的是將對spring的配置目錄以及文件名寫到web.xml中,如下所示:
<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
classpath默認爲resources目錄,即配置spring的文件在resources目錄下,名爲spring-config.xml。

3.2 spring配置

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <aop:aspectj-autoproxy />
       <context:component-scan base-package="com.jcx.blog" />
       <!-- 靜態資源(js、image等)的訪問 -->
       <mvc:default-servlet-handler/>
       <!--ViewResolver 視圖解析器-->
       <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
              <property name="prefix" value="/WEB-INF/pages/"/>
              <property name="suffix" value=".jsp"/>
       </bean>
       <!-- 屬性文件讀入 -->
       <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                     <list>
                            <value>classpath:props/*.properties</value>
                     </list>
              </property>
       </bean>

       <mvc:annotation-driven/>

       <import resource="classpath:spring/spring*.xml" />
</beans>
項目到目前爲止使用的是JSP,還沒使用velocity。web模塊的目錄結構如下。

3.3 ibatis配置

spring-config-dataSource.xml文件,配置了mysql的基本屬性,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://localhost:3306/blog"/>
              <property name="username" value="root"/>
              <property name="password" value="12345678"/>
       </bean>
</beans>
spring-config-dao.xml文件,配置了dao層即ibatis的基本配置,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
              <property name="configLocation" value="classpath:sqlmap-config.xml"/>
              <property name="dataSource" ref="dataSource"></property>
       </bean>
</beans>
該文件指定了SqlMapClient依賴的數據源以及配置ibatis的文件爲sqlmap-config.xml。
sqlmap-config.xml文件在dao模塊中的resources目錄下,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0/" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings cacheModelsEnabled="false" enhancementEnabled="true"
			  lazyLoadingEnabled="false" errorTracingEnabled="true" maxRequests="200"
			  maxSessions="60" maxTransactions="20" useStatementNamespaces="true"
			  defaultStatementTimeout="2" />
	<sqlMap resource="sqlmapmysql/user.xml"/>
</sqlMapConfig>
此處將博客用戶管理的數據庫操作ibatis配置分離到sqlmapmysql目錄下的user.xml中,此處應注意將useStatementNamespace置爲true。否則在user.xml等具體配置中將不能使用namespace。
user.xml配置如下,實現了用戶的添加和簡單的查找。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
        "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="User">
    <typeAlias alias="User" type="com.jcx.blog.domain.po.User"/>
    <resultMap class="User" id="UserResult">
        <result property="id" column="id"/>
        <result property="nickname" column="nickname"/>
        <result property="password" column="password"/>
        <result property="first_name" column="first_name"/>
        <result property="last_name" column="last_name"/>
    </resultMap>
    <select id="selectAllUser" resultMap="UserResult">
        select * from user
    </select>

    <insert id="addUser" parameterClass="User">
        INSERT user (nickname,password,first_name,last_name) VALUES (#nickname#,#password#,#first_name#,#last_name#)
    </insert>
    <select id="selectUserByNickname" resultMap="UserResult">
        SELECT * FROM user WHERE nickname=#nickname#
    </select>

</sqlMap>
至此,基本配置完成,若增加博客的相關映射關係,再建立blog.xml文件,進行相應的CRUD操作。
未完待續。。。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章