解決高併發-springboot-redis-mysql醫院預約系統專案超詳細講解--半個小時教你如何使用springboot完成預約專案-----第三章:使用者登入
阿新 • • 發佈:2021-01-14
技術標籤:醫院預約系統mysqlredisjavaspring boot
使用者登入無非就是通過賬號獲取密碼所以
mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kgc.mapper.PatientMapper" >
<select id="selectPatientByName" parameterType="String" resultType="Patient">
select * from patient where username = #{username}
</select>
</mapper>
Service
@Service
public class PatientServiceImpl implements PatientService {
@Autowired
private PatientMapper patientMapper;
@Override
public Patient getByUsername(String username) {
return patientMapper.selectPatientByName(username);
}
}
@Controller
public class PatientController {
@Autowired
private PatientService patientService;
@RequestMapping ("login")
@ResponseBody
public Map<String, Object> patientLogin(String username, String password, HttpSession session) {
Map<String, Object> result = new HashMap<>();
Patient patient = patientService.getByUsername(username);
//登陸成功
if (patient != null && patient.getPassword().equals(password)) {
result.put("success", true);
session.setAttribute("user", patient);//session傳使用者,方便獲取
}
else {
result.put("success", false);
result.put("message", "使用者名稱或密碼錯誤");
}
return result;
}
頁面
模態框登入按鈕出發onclick事件
function userLogin() {
$.ajax({
url: '/login',
type: 'post',
data: $("#login-form").serialize(),
dataType: "json",
success: function (data) {
if (data.success) {
var content = '<button type="button" οnclick="window.open(\'my_booking\')" ' +
'class="btn btn-sm btn-outline-primary">我的預約</button>'; //登入按鈕修改成我的預約
$("#login-button").html(content);
$("#loginModal").modal("hide"); // 登入後關閉模態框
}
else {
alert(data.message);
}
},
error: function(error){ //失敗後回撥
alert("伺服器連線失敗");
}
});
}
<#if user??>//如果user存在
<button type="button" onclick="window.open('/booking/my_booking')" class="btn btn-sm btn-outline-primary">我的預約</button>
<#else>
<button type="button" class="btn btn-sm btn-outline-primary" data-toggle="modal" data-target="#loginModal">
登入系統
</button>
</#if>
繫結事件 按鈕一定設定成buttun
登入前
登入後