一个登录练习搞了我好几天,我是真的好菜啊,有很多菜鸟和我一样在崩溃的边缘徘徊,写本文帮助一下..
所涉及:IDEAspringboot项目,mybatis, 使用的是Oracle(其他数据库自己改配置),ajax将前端页面数据传入后台(关于session,本菜鸟不会,自行添加补充)
1、前端页面设计
大致是如此,源码是同学的,不能透露,Login.ftl 大致为:
2、项目结构:
3、源码:
LoginController
@Controller
@RequestMapping(value = "/Login")
public class LoginController {
@Autowired
private LoginService loginservice;
// 先跳转页面
@RequestMapping(value = "/toLogin")
public String toLogin(){
return "Login";
}
@RequestMapping(value = "/login")
public String Login(String username, String password){
Integer userlogin = loginservice.toLogin(username, password);
if(username != null && password != null && userlogin == 1) {
return "success";
}else {
return "error";
}
}
}
LoginDao
@Repository
public interface LoginDao {
Integer toLogin(Login login);
}
LoginDao.xml
Login
public class Login {
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;
}
@Override
public String toString() {
return "Login{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
LoginService
public interface LoginService {
Integer toLogin(String username,String password);
}
LoginServiceImpl
@Service
public class LoginServiceImpl implements LoginService{
@Autowired
private LoginDao logindao;
@Override
public Integer toLogin(String username,String password){
Login login = new Login();
login.setPassword(password);
login.setUsername(username);
Integer userLogin = logindao.toLogin(login);
return userLogin;
// return userLogin = 0 或 1
}
}
LoginApplication
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.dao")
public class LoginApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
}
}
success.ftl
success
error.ftl
error
validation.js
function validation(){
var params = {};
params.username = $("#inputUserName").val();
params.password = $("#inputPassWord").val();
$.ajax({
type:"GET",
url:"/Login/login",
data:params,
dataType:"json",
success:function(data){
alert(data);
},
error:function(data){
alert(data)
}
})
}
pom.xml
4.0.0
com.example
demo
0.0.1-SNAPSHOT
jar
Login
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.oracle
ojdbc6
11.2.0.4
com.alibaba
druid
1.0.11
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-maven-plugin
application.properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL
spring.datasource.username=scott
spring.datasource.password=tiger
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:mapper/*.xml
############################################################
#
# freemarker配置
#
############################################################
#模板文件路径(不推荐使用)
spring.freemarker.template-loader-path=classpath:freemaker/
#关闭缓存即时刷新,生产环境需要改成true;
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
表结构:
表名称:TB_USER
表字段:
USERNAME VARCHAR2(55)
PASSWORD VARCHAR2(55)
数据库中用于验证的数据:
Login页面:数据数据库中已有的数据,点击登录
验证成功,自动跳转成功页面:
成功啦,有什么问题和建议,请下面留言。