环境搭建参考https://blog.csdn.net/YKYZSYA/article/details/108000053
对象封装参考https://blog.csdn.net/YKYZSYA/article/details/108070188
查所有参考https://blog.csdn.net/YKYZSYA/article/details/108034576
index.jsp(body标签内)
<!-- 提交地址是login,提交方式是post -->
<form action="login" method="post" onsubmit="return login()">
<table border="1px solid red" cellpadding="0px" cellspacing="0px" align="center">
<tr>
<td align="center" colspan="2">登录</td>
</tr>
<tr>
<td>账号:</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td> 密码:</td>
<td><input type="password" name="pwd" id="pwd"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
html标签外,判断账号密码是否为空,若为空则不提交
<script type="text/javascript">
function login(){
//得到name输入框对象
var name = document.getElementById("name");
var pwd = document.getElementById("pwd");
//判断输入框是否有内容
if(name.value.length==0){
confirm("账号不能为空");
return false;
}else if(pwd.value.length==0){
confirm("密码不能为空");
return false;
}else {
return true;
}
}
</script>
Dao层接口层(UserDao.java(是一个接口))
//登录
public Userinfo login(Userinfo userinfo);
Dao层mapper层 usermapper.xml
<?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">
<!-- 对应连接的dao层接口 naspace内部输入的是对应接口的路径 -->
<mapper namespace="com.ly.dao.UserDao">
<!-- 因为登录属于按条件查询,所以调用查询标签,resultType表示返回的结果类型,
因为spring配置了对象路径,所以这里填写封装的对象Userinfo 的首字母小写
parameterType表示返回类型-->
<select id="login" resultType="userinfo" parameterType="userinfo">
select * from userinfo where name=#{name} and pwd=#{pwd}
</select>
</mapper>
Service层接口层(UserSer.java(是一个接口))
//登录
public Userinfo login(Userinfo userinfo);
Service层实现层(UserSerImpl.java(UserSer的实现类))
@Service
public class UserSerImpl implements UserSer {
@Autowired
UserDao userDao;
@Override
public Userinfo login(Userinfo userinfo) {
// TODO Auto-generated method stub
return userDao.login(userinfo);
}
}
Controller层
@Controller
public class UserCon {
@Autowired
UserSer userSer;
//login是表单提交的地址
@RequestMapping("login")
public ModelAndView login(Userinfo userinfo,HttpSession session) {
ModelAndView mv=new ModelAndView();
userinfo=userSer.login(userinfo);
if (userinfo!=null) {
session.setAttribute("userinfo", userinfo);
//redirect是重定向,querally是查所有
mv.setViewName("redirect:queryall");
}else {
mv.setViewName("error.jsp");
}
return mv;
}
}