一:登录项目
第一个springboot项目
SpringbootexampleApplication:负责启动程序
SpringbootexampleApplicationTests:一个空的测试程序
application.properties:一个空的 properties 文件,可以根据需要添加配置属性
pom.xml:maven配置文件
在其目录下,创建一个文件,运行即可
spring boot 实现第一个登录功能
目录结构如下:
完成相关文件的配置:配置文件的精简是SpringBoot的一个优势,在resources包下有application.properties,配置文件就在这里配置,因为要实现的功能比较简单,这里的配置文件也很简单
在application.properties文件中配置以下信息
spring.datasource.url=jdbc:mysql://localhost:3306/stu?
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
mybatis.mapper-locations=classpath:mapper/*.xml
这里配置了数据库、SpringMvc的简单配置:说明jsp文件在/WEB-INF/jsp/路径下以.jsp为后缀,还有Mybatis的mapper文件路径。这里没有写Mybatis的配置文件,在SpringbootApplication这个启动文件中加上dao层扫描接口,实现对dao层的扫描:
package com.zcy.springbootexample;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.zcy.springbootexample.dao")//实现堆dao层的扫描
public class SpringbootexampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootexampleApplication.class, args);
}
}
pom.xml中的文件配置
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.0
com.zcy
springbootexample
0.0.1-SNAPSHOT
springbootexample
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
8.0.21
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
org.apache.tomcat.embed
tomcat-embed-jasper
org.springframework.boot
spring-boot-maven-plugin
src/main/java
**/*.xml
src/main/resources
true
功能类编写
controller:控制层用于控制转发;
dao:dao层用于进行数据库相关操作;
entity:实体层用于存放实体类相关代码;
service:服务层用于存放执行的服务,通过接口+实现类的形式
首先,创建一个实体类,里面存放username、password属性值
package com.zcy.springbootexample.entity;
public class User {
private String username;
private String password;
public User(String username, String password){
this.username=username;
this.password=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;
}
}
创建一个UserDao接口
package com.zcy.springbootexample.dao;
import com.zcy.springbootexample.entity.User;
public interface UserDao {
User select(User user);
}
在mapper包下创建mapper.xml,用于实现上面这个接口,查找该用户是否存在,mapper.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" >
<mapper namespace="com.zcy.springbootexample.dao.UserDao">
<select id="select" parameterType="com.zcy.springbootexample.entity.User" resultType="com.zcy.springbootexample.entity.User">
select * from user where username = #{username} and password = #{password}
</select>
</mapper>
创建一个UserService接口
package com.zcy.springbootexample.service;
import com.zcy.springbootexample.entity.User;
public interface UserService {
User get(User user);
}
定义UserServiceImpl类实现UserService接口
package com.zcy.springbootexample.service;
import com.zcy.springbootexample.dao.UserDao;
import com.zcy.springbootexample.entity.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public User get(User user) {
// 通过Mapper的select方法查询用户
return userDao.select(user);
}
}
最后编写UserController,首先进入login.jsp界面,如果用户名密码正确,跳转到user.jsp,否则继续保持login.jsp
package com.zcy.springbootexample.service;
import com.zcy.springbootexample.dao.UserDao;
import com.zcy.springbootexample.entity.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public User get(User user) {
// 通过Mapper的select方法查询用户
return userDao.select(user);
}
}
login.jsp配置文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<h1>用户登录界面</h1>
<br>
<form id="form" action="/login" method="post">
<table>
<tr>
<td>用户名</td>
<td><input id="username" name="username" value=""
</tr>
<tr>
<td>密码</td>
<td><input id="password-always-checkbox" name="password"
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
user.jsp配置文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User</title>
</head>
<body>
<h1>用户登录成功</h1>
<br>
欢迎您: ${user.username}
</body>
</html>
SpringBoot项目的启动程序为创建项目后自动生成的SpringbootApplication,直接运行即可。如果控制台没有出现报错信息,则表示启动成功,在浏览器输入http://localhost:8080/login即可。
无法解析jsp问题,程序正常启动,在访问时提示以下警告提醒
WARN 14836 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/login.jsp]
在pom.xml文件中配置以下信息,重新加载maven。导入jsp解析依赖包
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>