Springboot实现登录功能
SpringBoot简介
  Spring Boot是由Pivotal团队提供的全新框架,其设计⽬的是⽤来简化新Spring应⽤的初始搭建以及开发过程。该框架使⽤了特定的⽅式来进⾏配置,从⽽使开发⼈员不再需要定义样板化的配置。
SpringBoot特性
  1. SpringBoot并不是对Spring功能上的增强,⽽是提供了⼀种快速创建独⽴的Spring应⽤程序的框架
  2. 嵌⼊的Tomcat,⽆需部署WAR⽂件
  3. 简化Maven配置
  4. ⾃动配置Spring
  5. 绝对没有代码⽣成和对XML没有要求配置
  6.备受关注,是下⼀代框架,已经是不争的事实,不需要学习springmvc
  7.微服务的⼊门级微框架,springboot是springcloud的基础
结构
导⼊依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wn</groupId>
<artifactId>springbootproject02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootproject02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>jpa mybatis
<!-- 核⼼依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 可以实现热部署,在IDEA上实现热部署还需⼀些额外的配置,请查阅资料 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
<!-- JDBC for mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis -->
<!--mybatis-->
<!-- mvnrepository/batis.spring.boot/mybatis-spring-boot-starter -->        <dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--thymeleaf  新的模板引擎,⽐jsp要出⾊-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--xml配置,此是为了将来整合Hibernate或者mybatis
默认没有需要配置
-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
Users实体类
package com.ity;
public class Users {
private Integer uid;private String userName;
private String password;
private String realName;
public Users(Integer uid, String username, String password, String realname) { this.uid = uid;
this.userName = username;
this.password = password;
}
public Users() {
super();
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : im();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
}
}
dao层接⼝
@Repository("iUserDao")
public interface IUserDao{
//登录
public Users login(@Param("username") String username, @Param("userpassword") String userpassword); }
dao层xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<!-- mapper为映射的根节点,namespace指定Dao接⼝的完整类名
mybatis会依据这个接⼝动态创建⼀个实现类去实现这个接⼝,
⽽这个实现类是⼀个Mapper对象-->
<mapper namespace="com.wn.springbootproject02.dao.IUserDao">
<resultMap id="MapList" type="com.ity.Sale">
<id property="sid" column="sid"></id>
<result property="price" column="price"></result>
<result property="quantity" column="quantity"></result>
<result property="totalPrice" column="totalPrice"></result>
<result property="saleDate" column="saleDate"></result>
<result property="userId" column="userId"></result>
<result property="productId" column="productId"></result>
<association property="users" javaType="com.ity.Users">
<id property="uid" column="uid"></id>
<result property="userName" column="userName"></result>
<result property="password" column="password"></result>
<result property="realName" column="realName"></result>
</association>
<association property="product" javaType="com.ity.Product">
<id property="pid" column="pid"></id>
<result property="productName" column="productName"></result>
<result property="quantity" column="quantity"></result>
</association>
</resultMap>
<!--登录-->
<select id="login" resultType="com.ity.Users">
SELECT * FROM users WHERE username=#{username} AND PASSWORD=#{userpassword}
</select>
</mapper>
UserService接⼝层
public interface IUserService {
//登陸
public Users login(String username, String userpassword);
}
UserService接⼝实现层
@Service("iUserService")
public class IUserServiceImpl implements IUserService {
//植⼊dao层对象
@Resource(name = "iUserDao")
private IUserDao dao;
@Override
public Users login(String username, String userpassword) {
return dao.login(username,userpassword);
}
}
UserController层
@Controller
@RequestMapping("/user")
public class IUserController {
//植⼊对象
@Resource(name = "iUserService")
private IUserService service;
/
*返回⾴⾯*/
@RequestMapping("/getlogin")
public String getlogin(){
return"login";
}
/*登录*/
@RequestMapping("/login")
public ModelAndView login(Users user, ModelAndView mv, HttpServletRequest request, Model model){        Users login = service.UserName(),Password());
System.out.println(login);
if (login!=null){
System.out.println("成功!!");
mv.setViewName("index");
}else{
System.out.println("失败!!");
mv.setViewName("login");
}
return mv;
}
}
application.properties⽂件
spring.datasource.sql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///invoicingsystem
spring.datasource.username=root
spring.datasource.password=123
#Spring Data JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent-output=true
spring.jpa.database=mysql
spring.main.allow-bean-definition-overriding=true
login.html页⾯
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>系统登录 - ⼩型进销存系统</title>
<link rel="stylesheet" href="css/style.css"/>
<style>
#parent{
width:500px;
height:200px;
margin-top:20%;
margin-left:50%;
transform:translate(-50%,-50%) ;
background:#009688;
}
.password,.subBtn{
margin-top: 2%;
margin-left: 3%;
}
.loginHeader{
padding-top: 1%;
}
</style>
</head>
<body class="login_bg">
<div id="parent">
<section class="loginBox">
<header class="loginHeader" >
<h1>⼩型进销存系统</h1>
</header>
<section class="loginCont">

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。