Spring+SpringMVC+Hibernate实现登录

这篇博客介绍了如何使用Spring、SpringMVC和Hibernate来实现登录功能。作者通过注解方式配置Model层实体类,详细讲解了从DAO层、Service层到Controller层的开发过程,并涵盖了Spring与SpringMVC的整合,以及交互页面的创建。
摘要由CSDN通过智能技术生成

上一篇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">
        <!-- 配置数据源属性 -->
        
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值