學習的初始 一個 Hello World。mysql
一、搭建好環境web
工欲善其事,必先利其spring
這是須要的jarsql
簡單的說下 :數據庫
standard.jar 這個jar包是咱們在jsp中使用JSTL標籤的時候用到的。你也可使用SpringEL 。api
servlet-api.jar 這是你在SpringMvc中 用到HttpServletRequest 等這些類。緩存
導入的包都OK了,上傳下載的包我沒有導。如今就應該到配置文件了。 session
二、配置文件mvc
2.1web.xml app
項目使用了什麼框架。看這個配置就知道了。
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 配置Spring的用於初始化容器對象的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 注意個人applicationContext.xml 和下面的springmvc的配置文件都是放在src下的-->
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 中央控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- struts習慣使用/*,在springmvc無論用 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.2 applicationContext.xml 的配置
個人數據庫鏈接使用的是spirng進行鏈接。而後使用了 mysql.properties 配置的文件解耦。
注意若是你的spring、hibernate 的版本和個人不同。你修改下面綠色字體版本號。
<!-- 自動掃描與裝配bean -->
<context:component-scan base-package="org.pk.ssh"></context:component-scan>
<!-- 導入外部的properties文件 -->
<context:property-placeholder location="classpath:mysql.properties"/>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0數據庫鏈接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 數據鏈接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其餘配置 -->
<!--初始化時獲取三個鏈接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--鏈接池中保留的最小鏈接數。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--鏈接池中保留的最大鏈接數。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制數據源內加載的PreparedStatements數量。若是maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定義了鏈接池內單個鏈接所擁有的最大緩存statements數。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空閒時間,1800秒內未使用則鏈接被丟棄。若爲0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
<!-- 配置聲明式事務管理(採用註解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
2.3 mysql.propertiese
#注意格式
jdbcUrl = jdbc:mysql:///spring
driverClass = com.mysql.jdbc.Driver
user = root
password = root
2.4 hibernate的配置
<!-- 1,數據庫鏈接信息 -->
<property name="dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>
<!-- 2,其餘配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 3,導入映射文件,我使用的的是hibernate的註解,若是你用到是配置文件 -->
<!-- <mapping resource="org/pk/ssh/model/User" /> 配置文件-->
<mapping class="org.pk.ssh.model.User" />
</session-factory>
2.5 SpringMvc的 配置 sp ringmvc-servlet.xm
<!-- mvc註解驅動 -->
<mvc:annotation-driven/>
<!-- 掃描器已經有了上面這個mvc註解驅動的功能了,全部不須要了 -->
<context:component-scan base-package="org.pk.ssh.controller" />
<!-- 前綴+ viewName +後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- webroot到某一指定的文件夾的路徑 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 視圖名稱的後綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
三、配置文件已經OK了,我主要是寫個登陸的HelloWorld
我都是居於註解的方式來實現的。
model :HelloWorld 類
dao和它的實現類,登陸我就直接把用戶名和密碼存入到個人數據庫中了。其實應該是註冊。可是是個新手嘛。我就這樣寫了。嘿嘿。以後在慢慢的去改進它。
如今輪到controller 類隆重登場了。你們歡迎它
jsp界面的請求
在WEB-IN下創建jsp目錄。 jsp界面都放這裏。爲何要放在這裏而不放在webroot呢?如下是網上的一些說法
http://blog.csdn.net/saygoodbyetoyou/article/details/9944773
http://uule.iteye.com/blog/1853531
上面這個就是個人登陸的界面了。請求注意加 *.do 由於個人配置攔截就是這樣的。
而後這個是在另一個jsp界面中的輸出,我使用的是JSTL進行輸出的。
<c:out value="${user.name}"/>