安装使用Spring boot 写一个hello1

2020-12-13 02:28

阅读:249

YPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

标签:pos   数据库   false   type   add   and   users   a标签   cci   

一、创建springboot 项目

二、进行代编写

1.连接数据库:application.properties里配置

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/huoguo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.showSql=true


mybatis.type-aliases-package=com.xiaojungan.huoguo.entity
mybatis.mapper-locations=mybatis/*.xml

2.用户实体   entity.User:

package com.xiaojungan.huoguo.entity;

public class User {
    private Integer id;
    private String name;
    private Integer password;
    private Integer canzhuo_id;

    public User(){

    }

    public User(Integer id, String name, Integer password,Integer canzhuo_id ) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.canzhuo_id  = canzhuo_id ;
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPassword() {
        return password;
    }

    public void setPassword(Integer password) {
        this.password = password;
    }

    public Integer getCanzhuo_id() {
        return canzhuo_id;
    }

    public void setCanzhuo_id(Integer canzhuo_id) {
        this.canzhuo_id = canzhuo_id;
    }
}

3.UserDao

package com.xiaojungan.huoguo.dao;

import com.xiaojungan.huoguo.entity.User;
import org.apache.ibatis.annotations.Param;

public interface UserDao {

    //登录判断
    User login(User user);
    //注册
    int addUser(User user);
    int adduser(@Param("name") String name, @Param("password") Integer password);
}

4.UserDaoImpl

package com.xiaojungan.huoguo.dao.impl;

import com.xiaojungan.huoguo.dao.UserDao;
import com.xiaojungan.huoguo.entity.User;

public class UserDaoImpl implements UserDao {
    @Override
    public User login(User user) {
        return null;
    }

    @Override
    public int addUser(User user) {
        return 0;
    }

    @Override
    public int adduser(String name, Integer password) {
        return 0;
    }
}

5.控制层    UserController.

package com.xiaojungan.huoguo.controller;

import com.xiaojungan.huoguo.dao.impl.UserDaoImpl;
import com.xiaojungan.huoguo.entity.User;
import com.xiaojungan.huoguo.dao.UserDao;
import com.xiaojungan.huoguo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;




import org.springframework.web.bind.annotation.*;


@Controller
public class UserController {
    @Resource
    UserDao ad = new UserDaoImpl();

    @RequestMapping("/login")//主页
    public String index(){
        return "login";
    }

    @RequestMapping("/goregister1")//去注册页面
    public String goregister(){
        return  "register1";
    }


    @RequestMapping("/gologin")//登录获取用户信息存到seccion
    public String  login(User user,HttpServletRequest request,Model model){
        User aa= ad.login(user);
        if (aa==null){
            return  "public/false";
        }
        HttpSession session =  request.getSession();
        session.setAttribute("name",user.getName());
        session.setAttribute("password",user.getPassword());
        model.addAttribute("user",aa);
        return "user/index";
    }

    @RequestMapping("/index")//从其他页面操作后返回列表页面(重复登录)
    public String login(User user,Model model,HttpServletRequest request){
        HttpSession session =  request.getSession();
        user.setName((String) session.getAttribute("aname"));
        user.setPassword((Integer) session.getAttribute("apassword"));
        User aa = ad.login(user);
        model.addAttribute("user",aa);
        return "user/index";
    }

    @RequestMapping(value = {"/register1"})
    public String adduser(@RequestParam("name") String username,
                          @RequestParam("password") Integer password,
                          @RequestParam("password2") Integer password2){

        if (!password.equals(password2)) {

            return "/user/wrong";
        } else {
            int res = ad.adduser( username, password);
            if (res == 0) {
                return "/user/wrong";
            } else {
                return "/login";
            }
        }
    }
}

6.application中配置

package com.xiaojungan.huoguo;

import org.springframework.boot.SpringApplication;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.xiaojungan.huoguo.dao")
public class HuoguoApplication {

    public static void main(String[] args) {

        SpringApplication.run(HuoguoApplication.class, args);
    }

}

7.UserMapper.xml 设置数据库语句的操作


        INSERT INTO user(name,password) VALUES (#{name},#{password});
    
        INSERT INTO user (name,password) VALUES (#{name},#{password})
    

8.登录页面 login.html

首页
请输入姓名

请输入密码

 

9.注册页面  register1.html

注册
用户姓名: 用户密码: 确认密码:

10.index.html


使用spring boot写一个hello1
      Hello1

11运行结果:

技术图片技术图片技术图片

安装使用Spring boot 写一个hello1

标签:pos   数据库   false   type   add   and   users   a标签   cci   

原文地址:https://www.cnblogs.com/luv-letter/p/11039870.html


评论


亲,登录后才可以留言!