亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Spring5實(shí)現(xiàn)登錄注冊(cè)功能

 更新時(shí)間:2022年09月06日 11:23:52   作者:用編程改改自己吧  
這篇文章主要為大家詳細(xì)介紹了基于Spring5實(shí)現(xiàn)登錄注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Spring5實(shí)現(xiàn)登錄注冊(cè)功能的具體代碼,供大家參考,具體內(nèi)容如下

準(zhǔn)備:

根據(jù)分析用戶注冊(cè)登錄都需要的信息為
①u(mài)sername(String)
②userid(Int)
③userpassword(String)
④useremail(String)

1.生成數(shù)據(jù)庫(kù)、表

2.編寫(xiě)實(shí)體類

import org.springframework.stereotype.Component;
@Component
public class User {
? ? private String UserName;
? ? private int UserId;
? ? private String UserPassWord;
? ? private String UserEmail;

? ? public User(String userName, int userId, String userPassWord, String userEmail) {
? ? ? ? UserName = userName;
? ? ? ? UserId = userId;
? ? ? ? UserPassWord = userPassWord;
? ? ? ? UserEmail = userEmail;
? ? }

? ? public User() {
? ? }

? ? public String getUserName() {
? ? ? ? return UserName;
? ? }

? ? public void setUserName(String userName) {
? ? ? ? UserName = userName;
? ? }

? ? public int getUserId() {
? ? ? ? return UserId;
? ? }

? ? public void setUserId(int userId) {
? ? ? ? UserId = userId;
? ? }

? ? public String getUserPassWord() {
? ? ? ? return UserPassWord;
? ? }

? ? public void setUserPassWord(String userPassWord) {
? ? ? ? UserPassWord = userPassWord;
? ? }

? ? public String getUserEmail() {
? ? ? ? return UserEmail;
? ? }

? ? public void setUserEmail(String userEmail) {
? ? ? ? UserEmail = userEmail;
? ? }
}

3.配置xml文件(jdbcTemplate注入到dao層中)

<?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:context="http://www.springframework.org/schema/context"
? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

? ? <!--掃描包-->
? ? <context:component-scan base-package="MyPackage"></context:component-scan>
? ? <!--數(shù)據(jù)庫(kù)連接池-->
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/review"></property>
? ? ? ? <property name="username" value="root"></property>
? ? ? ? <property name="password" value="123456"></property>
? ? ? ? <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
? ? </bean>
? ? <!--配置JdbcTemplate對(duì)象,把數(shù)據(jù)庫(kù)dataSource注入進(jìn)去-->
? ? <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
? ? ? ? <!--要把數(shù)據(jù)庫(kù)注入到JdbcTemplate對(duì)象中-->
? ? ? ? <property name="dataSource" ref="dataSource"></property>
? ? </bean>
? ? <!--到了這一步我們就連接好了數(shù)據(jù)庫(kù)了-->
</beans>

4.編寫(xiě)Userdao(dao層)

import MyPackage.pojo.User;
import org.springframework.stereotype.Component;
@Component
public interface UserDao {
? ? void Register(User user);

? ? User login(Integer id);
}

UserDaoImpl類

ckage.dao;

import MyPackage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class UserDaoImpl implements UserDao{

? ? @Autowired
? ? JdbcTemplate jdbcTemplate;//自動(dòng)注入數(shù)據(jù)庫(kù)

? ? @Override
? ? public void Register(User user) {
? ? ? ? String sql = "insert into t_user (`username`,`userid`,`userpassword`,`useremail`)" +
? ? ? ? ? ? ? ? "values(?,?,?,?)";
? ? ? ? Object[] args = {user.getUserName(), user.getUserId(), user.getUserPassWord(), user.getUserEmail()};
? ? ? ? int update = jdbcTemplate.update(sql,args);
? ? ? ? System.out.println("注冊(cè)成功:"+update);
? ? }

? ? @Override
? ? public User login(Integer id) {
? ? ? ? String sql = "select *from t_user where `userid`=?";
? ? ? ? User user1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
? ? ? ? return user1;
? ? }
}

5.編寫(xiě)service層

import MyPackage.dao.UserDao;
import MyPackage.dao.UserDaoImpl;
import MyPackage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "userService")
public class UserService {
? ? @Autowired
? ? public UserDao userDao;
? ? //用戶注冊(cè)
? ? public void Register(User user){
? ? ? ? System.out.println("Service Register.....");
? ? ? ? userDao.Register(user);
? ? }

? ? //用戶登錄
? ? public void Login(User user){
? ? ? ? System.out.println("Service Login.....");
? ? ? ? User loginsuccess = userDao.login(user.getUserId());
? ? ? ? if (loginsuccess!=null)
? ? ? ? ? ? System.out.println("登錄成功");
? ? ? ? else
? ? ? ? ? ? System.out.println("登錄失敗");
? ? }
}

6.測(cè)試

import MyPackage.pojo.User;
import MyPackage.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
? ? //注冊(cè)功能測(cè)試
? ? @org.junit.Test
? ? public void testJdbcTemplate1(){
? ? ? ? ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
? ? ? ? UserService userService = (UserService) context.getBean("userService", UserService.class);
// ? ? ? ?userService.Register(new User("mary",2,"654321","654321@qq.com"));
? ? ? ? userService.Register(new User("lucy",1,"123456","123456@qq.com"));
? ? }
? ? //登錄功能測(cè)試
? ? @org.junit.Test
? ? public void testJdbcTemplate2() {
? ? ? ? ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
? ? ? ? UserService userService = (UserService) context.getBean("userService", UserService.class);
? ? ? ? userService.Login(new User("lucy",1,"123456","123456@qq.com"));
? ? }
}

運(yùn)行截圖

遇到的問(wèn)題:

剛開(kāi)始把bean1.xml配置文件直接是在src目錄下生成的,所以當(dāng)運(yùn)行時(shí),就會(huì)發(fā)現(xiàn)找不到xml文件
解決方法:

在main目錄下新建一個(gè)resources文件,且把該文件變成Resources類型,然后把bean1.xml放入在這里就可以了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java文件上傳下載功能實(shí)現(xiàn)代碼

    java文件上傳下載功能實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了java文件上傳下載功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的朋友可以參考一下
    2016-06-06
  • java服務(wù)端微信APP支付接口詳解

    java服務(wù)端微信APP支付接口詳解

    這篇文章主要為大家詳細(xì)介紹了java服務(wù)端微信APP支付接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • SpringMVC的詳細(xì)架構(gòu)你了解嘛

    SpringMVC的詳細(xì)架構(gòu)你了解嘛

    這篇文章主要為大家介紹了SpringMVC的詳細(xì)架構(gòu),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • 淺析Java自定義注解的用法

    淺析Java自定義注解的用法

    注解為我們?cè)诖a中添加信息提供一種形式化的方法,使我們可以在源碼、編譯時(shí)、運(yùn)行時(shí)非常方便的使用這些數(shù)據(jù)。本文主要為大家介紹了Java自定義注解的用法,希望對(duì)大家有所幫助
    2023-03-03
  • Spring?IOC注入的兩種方式詳解以及代碼示例

    Spring?IOC注入的兩種方式詳解以及代碼示例

    在Spring框架中,依賴注入(Dependency?Injection,DI)是通過(guò)控制反轉(zhuǎn)(Inversion?of?Control,IOC)實(shí)現(xiàn)的,Spring提供了多種方式來(lái)實(shí)現(xiàn)IOC注入,本文就給大家介紹兩種注入的方式:基于XML和基于注解,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-08-08
  • java實(shí)現(xiàn)Dijkstra最短路徑算法

    java實(shí)現(xiàn)Dijkstra最短路徑算法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Dijkstra最短路徑算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Java 創(chuàng)建URL的常見(jiàn)問(wèn)題及解決方案

    Java 創(chuàng)建URL的常見(jiàn)問(wèn)題及解決方案

    這篇文章主要介紹了Java 創(chuàng)建URL的常見(jiàn)問(wèn)題及解決方案的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • java objectUtils 使用可能會(huì)出現(xiàn)的問(wèn)題

    java objectUtils 使用可能會(huì)出現(xiàn)的問(wèn)題

    這篇文章主要介紹了java objectUtils 使用可能會(huì)出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java之swing實(shí)現(xiàn)復(fù)選框的方法

    java之swing實(shí)現(xiàn)復(fù)選框的方法

    這篇文章主要介紹了java之swing實(shí)現(xiàn)復(fù)選框的方法,實(shí)例分析了java基于圖形界面復(fù)選框的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • springboot如何通過(guò)URL方式訪問(wèn)外部資源

    springboot如何通過(guò)URL方式訪問(wèn)外部資源

    這篇文章主要介紹了springboot如何通過(guò)URL方式訪問(wèn)外部資源,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論