sping mvc 結合 hibernate 實現用戶登錄功能(一)!


目錄結構如下所示:

第一步:創建數據庫,sql腳本如下

create database user;
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'wangkun', 'abc');
INSERT INTO `user` VALUES ('2', 'vincent', '123');

配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


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

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<session-config>
<session-timeout>8</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

配置applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:p
="http://www.springframework.org/schema/p"
xmlns:tx
="http://www.springframework.org/schema/tx"
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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>


<!-- Spring注解方式注入 -->
<context:annotation-config />

<!-- 使用annotation 自動注冊bean,並保證@Required,@Autowired的屬性被注入 -->
<context:component-scan base-package="com.spring" />

<!-- 數據庫配置文件 -->
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:dbconfig.properties</value>
</list>
</property>
</bean>



<!-- 數據源 -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${userName}"></property>
<property name="password" value="${pass}"></property>
</bean>

<!-- hibernate sessionFactory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.spring.entity</value>
</list>
</property>
</bean>

<!-- 配置事務管理類 -->
<bean id="myTran" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
<!-- 通過注解方式配置事務 -->
<tx:annotation-driven transaction-manager="myTran"/>

<!-- Service層注入 注入userservice -->
<bean id="userservice" class="com.spring.service.UserServiceImpl">
<property name="userdao" ref="userdao"></property>
</bean>
<!-- Dao層注入 -->
<bean id="userdao" class="com.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
</beans>


配置example-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:p
="http://www.springframework.org/schema/p"
xmlns:tx
="http://www.springframework.org/schema/tx"
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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<!-- 通過讀取路徑下面的@Controller來轉換成bean -->
<context:component-scan base-package="com.spring"></context:component-scan>

<!-- 啟動Spring mvc的注解功能,完成請求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 對模型視圖的名稱的解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
="/WEB-INF/pages/" p:suffix=".jsp"/>
</beans>

寫個dbconfig.properties文件:

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/user
userName=root
pass=root



所有的配置准備工作已經做好,接下來請看---sping mvc 結合 hibernate 實現用戶登錄功能(二)!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM