一、引言
工作中用到了SpringMVC、ibatis、MySQL和velocity實現系統的Web部分,由於想構建一個博客系統,所以,使用這套架構構建一套博客系統,目前,已經實現了基本的註冊登錄功能。
二、架構
三、配置
3.1 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配置
<?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。<?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。<?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操作。