2019獨角獸企業重金招聘Python工程師標準>>>
學習的初始 一個 Hello World。
spring與mybatis整合。1、搭建好環境
工欲善其事,必先利其
這是需要的jar
maven java、
簡單的說下 :
? standard.jar ?這個jar包是我們在jsp中使用JSTL標簽的時候用到的。你也可以使用SpringEL 。
???servlet-api.jar ?這是你在SpringMvc中 用到HttpServletRequest 等這些類。
導入的包都OK了,上傳下載的包我沒有導。現在就應該到配置文件了。 ?
2、配置文件
?2.1web.xml ?
? ?項目使用了什么框架。看這個配置就知道了。
?<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>?
3、配置文件已經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}"/>