Spring Boot之---简单登录注册demo

版权声明:转载望注明地址 https://blog.csdn.net/x_san3/article/details/81634699

 Spring Boot项目的创建非常简单,就不提了,直接new-->Spring Starter Project即可,建好后直接就可以写代码,实现功能。

  • 项目目录

 

  • pom.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.san</groupId>
	<artifactId>springboot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot-demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
  •  application.properties内容
# 数据库配置
# spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydemo?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456

# mybatis_config  
# mybatis.mapper-locations=classpath:mapper/*.xml
# mybatis.type-aliases-package=com.san.model

# 配置mvc视图解析器
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix: .jsp

# 关闭thymeleaf缓存,thymeleaf热部署,还要开启项目自动构建
spring.thymeleaf.cache=false
  •  实体对象User.java
public class User {
    private Integer user_id;  
    private String user_name;  
    private String user_pwd;
    //getter ... setter ...
  •  数据接口UserDao.java
public interface UserDao {
	
	@Select("SELECT * FROM t_user WHERE user_id = #{userId}")
	public User selectUserById(Integer userId);

	// 添加用户
	@Insert("insert into t_user(user_name, user_pwd) values(#{user_name}, #{user_pwd})")
	public void addUser(User user);

	// 根据用户名查询用户
	// 注解的两个参数会自动封装成map集合,括号内即为键
	@Select("select user_name, user_pwd from t_user where user_name=#{name}")
	public User findByUsername(@Param("name") String name);
}
  •  UserService.java
public interface UserService {
	User selectUserById(Integer userId);

	// 用户注册
	void regist(User user);

	// 用户登录
	User login(String name, String password);
}
  •  UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
	@Autowired  
    private UserDao userDao;  
  
    public User selectUserById(Integer userId) {  
        return userDao.selectUserById(userId);  
          
    }

	public void regist(User user) {
		userDao.addUser(user);		
	}

	public User login(String name, String password) {
		User user = userDao.findByUsername(name);
        if(user != null && user.getUser_pwd().equals(password)){       
            return user;
        }
        return null;
	} 
}
  • UserController.java
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired 
    private UserService userService;  
      
	@RequestMapping("/index")
    public String index(){
        return "index";
    }
	
	@RequestMapping("/userhello")    
    public ModelAndView getIndex(){   
        ModelAndView mav = new ModelAndView("hello");   
        User user = userService.selectUserById(1);
        mav.addObject("hello", "Hello Spring Boot!!!");
        mav.addObject("user", user);
        return mav;    
    } 
	@RequestMapping("/hello")    
    public ModelAndView gIndex(){   
        ModelAndView mav = new ModelAndView();   
        User user = userService.selectUserById(1);
        mav.addObject("hello", "Hello Spring Boot!!!");
        mav.addObject("user", user);
        mav.setViewName("hello");
        return mav;    
    }
      
    @RequestMapping("/regist")
    public String regist(){
        return "regist";
    }
    
    @RequestMapping("/doRegist")
    public String doRegist(User user){
        System.out.println(user.getUser_name());
        userService.regist(user);
        return "success";
    }

    @RequestMapping("/login")
    public String login(User user){
        user = userService.login(user.getUser_name(), user.getUser_pwd());
        if(user != null){
            return "success";
        }
        System.out.println(user);
        return "fail";
    }

}
  •  界面部分随便简单处理就行
1.index.html表单部分:

<form action="/user/login" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input name="user_name" type="text"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input name="user_pwd" type="password"></td>
            </tr>
            <tr >
                <td><input type="submit" value="登录"></td>
                <td><input type="button" value="注册"     onclick="window.location.href='/user/regist'"> </td>
            </tr>

        </table>
    </form>

------------

2.regist.html表单部分:

<form action="/user/doRegist" method="post">
        <table>
            <tr height="35px">
                <td width="100px">用户名</td>
                <td width="270px"> <input type="text" name="user_name"></td>
            </tr>
            <tr height="35px">
                <td>密码</td>
                <td><input type="text" name="user_pwd"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" id="regist" value="注册"/>
                </td>
            </tr>
        </table>
    </form>

-------------

3.success.html 

<body>
    <div>
        <strong> welcome <span th:text="${user.user_name}"></span> </strong>
    </div>
    this is success page!

    <form action="/user/index">
        <table>
            <tr>
                <td><input type="submit" value="退出登录"></td>
            </tr>
        </table>
    </form>
</body>

-------------

4.fail.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>fail page</title>
</head>
<body>
     this is a fail page!
</body>
</html>

-------------

5.hello.html

<body>
	<h2 th:text="${hello}"></h2>
	<hr/>
	用户名: <span th:text="${user.user_name}"></span><br>
	密码:<span th:text=" ${user.user_pwd}"></span>	
</body>

数据库部分和实体对应的字段一样,建立即可。

启动项目,浏览器输入http://localhost:8080/  即到index页面,登录成功如图:

源码下载:点击下载

猜你喜欢

转载自blog.csdn.net/x_san3/article/details/81634699