SpringbootとMYSQL + Mybatis + thymeleafによるシンプルなログイン機能
まず、Springbootプロジェクトパッケージを作成します
mysqlで単純なログインユーザーとパスワードの
構成プロパティを作成します。
最初のapplication.properties
これはeclipseによって生成されたスタートアップクラスです。
次に、エンティティにUserエンティティクラスを記述します
package com.example.second.entity;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
次に、データアクセスレイヤー(Dao)へ
package com.example.second.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import com.example.second.entity.User;
@Mapper
@Component(value="userDao")
public interface UserDao {
//登录功能接口
public User Login(@Param("username")String username,
@Param("password")String password);
}
次に、マッパーパッケージに.xmlファイルを作成
し、データサービスレイヤー(サービス)に移動します。
package com.example.second.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.second.dao.UserDao;
import com.example.second.entity.User;
import com.example.second.service.UserService;
@Service("UserService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public User Login(String username, String password) {
return userDao.Login(username, password);
}
}
そしてサービスインターフェース
package com.example.second.service;
import com.example.second.entity.User;
public interface UserService {
public User Login(String username,String password);
}
最後にフロントコントローラー(コントローラー)へ
package com.example.second.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.second.entity.User;
import com.example.second.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value="/logining")
public ModelAndView login(HttpServletRequest request,ModelAndView mv, User u) {
String username=request.getParameter("username");
String password=request.getParameter("password");
u=userService.Login(username, password);
if(u!=null) {
mv.setViewName("logining");
mv.addObject("info", "登陆成功");
return mv;
}
mv.setViewName("logining");
mv.addObject("info", "登陆失败");
return mv;
}
}
ログインページlogin.htmlコントローラーも作成します
package com.example.second.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//登录页面控制
@Controller
public class PageController {
@RequestMapping(value="/login")
public String str(String s) {
return "logining";
}
}
@Controllerを使用し(次回説明します)、
最後にlogin.htmlを記述してフロントエンドログインを実装するためです。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>实现简单的登录</title>
</head>
<body>
<center>
<table>
<form action="/logining" method="post">
<tr>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
<input type="submit" value="登陆" >
</td>
</tr>
</form>
<span th:text="${info}"></span>
</table>
</center>
</body>
</html>
スクリーンショットを実行
して正常にログインします。
注:mysqlのユーザーとパスワードを使用していない場合は、ログインに失敗したことを示すメッセージが表示されます。