SpringBoot的简单登陆开发例子
1:这个例子用spirngboot整合mybatis,jdbc等技术开发的
2:步骤
2.1:新建一个工程
主要的两个步骤已经贴图了,第二张图是直接在pom.xml文件中加入依赖
2.2:新建完项目,就创建一个数据表
-
CREATE TABLE `tuser` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`loginname` varchar(20) DEFAULT NULL,
-
`password` varchar(20) DEFAULT NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
创建一个名为tuser的数据表
2.3:把项目所需的包和文件夹补充完整
2.4整合jdbc,在application.yml中加入jdbc的支持
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
2.5整合springmvc的前端控制器,在application.yml中加入
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
2.6整合mybatis,添加mapper.xml的目录
mybatis:
mapper-locations: classpath:mapper/*.xml
2.7整个yml文件如下
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
mybatis:
mapper-locations: classpath:mapper/*.xml
2.8实体类的编写,也就是model
和数据表对应的User类
-
public class User {
-
-
private int id;
-
private String loginname;
-
private String password;
//省略构造方法和get/set方法
}
返回数据给页面的封装类ResultEntity
-
public class ResultEntity {
-
-
private int code;
-
private String message;
-
//省略构造方法和get/set方法
-
-
}
2.9 控制层的编写,controller
-
-
-
public class LoginController {
-
-
-
private LoginService loginService;
-
-
-
public String index() {
-
return "login";
-
}
-
-
-
public String success() {
-
return "success";
-
}
-
-
-
-
public ResultEntity login(User user, Model model) {
-
ResultEntity resultEntity = loginService.login(user);
-
if (resultEntity.getCode() == 200)
-
model.addAttribute("user", user);//将user存放到session
-
return resultEntity;
-
}
-
-
}
2.10Service实现层的编写
-
-
class LoginServiceImpl implements LoginService {
-
-
-
private UserMapper userMapper;
-
-
private ResultEntity resultEntity;
-
-
-
public ResultEntity login(User user) {
-
User login = userMapper.login(user);
-
if (login != null) {
-
resultEntity = new ResultEntity(200, "登陆成功");
-
} else {
-
resultEntity = new ResultEntity(201, "用户名或密码错误");
-
}
-
return resultEntity;
-
}
-
}
接口层就忽略啦 我懒。。就一个 ResultEntity login(User user);
2.11 mapper.xml
-
-
-
<mapper namespace="net.stxy.boot.dome.mapper.UserMapper">
-
-
<select id="login" parameterType="net.stxy.boot.dome.model.User" resultType="net.stxy.boot.dome.model.User">
-
select * from tuser where loginname = #{loginname} and password = #{password}
-
</select>
-
-
</mapper>
mapper接口也不写了。。。QAQ..
2.12 到页面了,采用jsp开发,所以要在pom.xml中添加对jsp的支持
-
<!--springboot tomcat jsp 支持开启-->
-
<dependency>
-
<groupId>org.apache.tomcat.embed</groupId>
-
<artifactId>tomcat-embed-jasper</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>javax.servlet</groupId>
-
<artifactId>jstl</artifactId>
-
<scope>compile</scope>
-
</dependency>
因为在yml中指定了前端控制器,所以直接在WEB-INF的jsp下读取jsp页面,静态资源文件就可以在路径前面添加,例如jquery文件放在static/js/jquery.min.js,读取直接读js/jquery.min.js即可,因为默认静态资源路径为/static/
2.13 登陆页面用ajax提交表单,判断返回的code是否==200,等于200则跳转登陆成功页面,否则,提示失败.
ajax代码:
-
//提交表单
-
$.ajax({
-
url: "/login",
-
type: "post",
-
dataType: "json",
-
data: {
-
loginname: account,
-
password: password
-
},
-
success: function (result) {
-
console.log(result);
-
if (result.code == 200) {
-
window.location.href = "success";
-
} else {
-
alert(result.message);
-
}
-
}
-
})
3.效果
3.1地址栏输入 localhost:8080/index,出现登陆页面
3.2登陆失败
3.3登陆成功
这是一个非常简单的springboot登陆例子,新手一枚,勿喷i....QAQ...