搭建 SpringBoot 2.0 项目 (一) 开发环境的搭建(安装开发工具STS)
搭建 SpringBoot 2.0 项目 (二) 简单项目的搭建
搭建 SpringBoot 2.0 项目 (三) 使用 JPA 访问MySQL数据库
搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库
搭建 SpringBoot 2.0 项目 (五) 搭建一个集成ssm的web项目
简述:在框架搭好的基础上,实现一个用户登录功能,查询数据库并比对数据,输出相应的提示。
需求:页面登录 >> 表单数据提交 >> 对比数据库 >> 判断登录是否成功
用户名及密码正确:
否则:
选择MySQL、MyBatis、Web、DevTools(开发web的工具),Thymeleaf(html模板引擎,根据需要自主选择)
如图创建相关类。
package com.example.demo.common;
public class StringUtil {
public static boolean isNullOrZero (String str) {
if(str == null || str.trim().length() == 0) {
return true;
}
return false;
}
}
package com.example.demo.controller;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.demo.entity.Member;
import com.example.demo.service.IMember;
@Controller
@MapperScan("com.example.demo.dao") //mapper扫描
public class LoginController{
@Autowired
IMember iMemberFunc;
@RequestMapping(value = "/hello")
public String hello(){
System.out.println("Hello");
return "Log";
}
@RequestMapping(value = "/login",method = RequestMethod.GET)
public String addUser1(String name,String password) {
System.out.println("loginName is:"+name);
System.out.println("loginPassword is:"+password);
try {
Member member = iMemberFunc.login(name, password);
if(member == null){
System.out.println("登录失败");
return "Log_fail";
}else {
System.out.println("登录成功");
return "Log_success";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("登录异常");
}
return null;
}
}
package com.example.demo.entity;
public class Member {
private int id;
private String name;
private String password;
public Member(){}
public Member(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Member [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
}
package com.example.demo.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.example.demo.entity.Member;
import org.apache.ibatis.annotations.Select;
//@Mapper
public interface MemberDao {
@Select("select * from Member where name = #{name}")
Member selectMemberByName(@Param("name")String name)throws Exception;
}
6、IMember 服务层接口。
package com.example.demo.service;
import com.example.demo.entity.Member;
public interface IMember {
Member login(String name, String passsword) throws Exception;
}
package com.example.demo.service;
import com.example.demo.service.IMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.common.StringUtil;
import com.example.demo.entity.Member;
import com.example.demo.dao.MemberDao;
@Service
public class MemberImpl implements IMember {
@Autowired
private MemberDao mDao;
public Member login(String name, String password) throws Exception {
// System.out.println(name + password);
if (StringUtil.isNullOrZero(name)) {
System.out.println("登录名不能为空");
return null;
}
if (StringUtil.isNullOrZero(password)) {
System.out.println("密码不能为空");
return null;
}
Member member = mDao.selectMemberByName(name);
if (member == null)
System.out.println("登录名错误");
if (member != null&&member.getName().equals(name)&&!password.equals(member.getPassword())) {
System.out.println("密码错误");
return null;
}
return member;
}
}
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
#扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了
mybatis.type-aliases-package=com.example.demo
新建test数据库,建立menber表格,插入测试数据。如下图。
INSERT INTO `test`.`member` (`id`, `name`, `password`) VALUES ('1', 'a', '123');
INSERT INTO `test`.`member` (`id`, `name`, `password`) VALUES ('2', 'b', '456');
INSERT INTO `test`.`member` (`id`, `name`, `password`) VALUES ('3', 'c', '789');
《1》登录页面
登录页面
《2》登录成功
登录成功
登录成功
[退出]
《3》登录失败
登录失败