上一篇https://blog.csdn.net/stark_jc/article/details/79778962采用了Spring+Struts2+Hibernate的方式实现了登录,且上次采用的是XML配置(包括实体类与数据表之间的配置、以及Struts2的action映射关系的配置),本文以SpringMVC取代Struts2,在路径和处理器以及实体类属性和数据表字段的映射关系上使用注解.
这是此次所需的文件
Model层开发
创建实体类
UserInfo,采用注解的方式
package com.digital.entity;
import javax.persistence.*;
@Entity
@Table(name = "user_info", catalog = "digital")
public class UserInfo {
private int id;
private String userName;
private String password;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "userName", length = 16)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "password", length = 16)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// 无参构造
public UserInfo() {
}
// 有参构造
public UserInfo(String userName, String password) {
this.userName = userName;
this.password = password;
}
}
Spring与Hibernate的整合
applicationContext
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///digital" />
<property name="user" value="root" />
<property name="password" value="admin" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
</bean>
<!-- 配置Hibernate的sessionFactory实例 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->