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 实现用户登录功能(二)!

 

原文地址:https://www.cnblogs.com/kunpengit/p/2411886.html