安裝使用Spring boot 寫一個hello1

1、建立springboot 項目css

2、進行代編寫html

1.鏈接數據庫:application.properties裏配置java

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:mysql

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.UserDaoweb

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.UserDaoImplspring

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.sql

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 設置數據庫語句的操做apache

<?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.xiaojungan.huoguo.dao.UserDao">
    <select id="login" parameterType="com.xiaojungan.huoguo.entity.User" resultType="com.xiaojungan.huoguo.entity.User">
        select name,password FROM user WHERE name = #{name} AND password = #{password}
    </select>

    <insert id="addUser" parameterType="com.xiaojungan.huoguo.entity.User">
        INSERT INTO user(name,password) VALUES (#{name},#{password});
    </insert>
    <insert id="adduser">
        INSERT INTO user (name,password) VALUES (#{name},#{password})
    </insert>
    
</mapper>

8.登陸頁面 login.htmlspringboot

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
    <link href="user/css/1.css" type="text/css" rel="stylesheet"/>
    <style>
        /*a標籤去下劃線和點擊不變色,div內容居中*/
        a{
            text-decoration: none;
            color: #333333;
        }
        #idiv{text-align: center;border-radius: 20px;
                            width: 300px;
                            height: 350px;
                            margin: auto;
                            position: absolute;
                            top: 0;
                            left: 0;
                            right: 0;
                            bottom: 0;}

    </style>
</head>
<body  background="../../../../../../java%20%20work/huoguo/src/main/webapp/user/img/2.jpg">
<div id="idiv">
    <form action="/gologin" method="post">
        請輸入姓名<input id="name" name="name" required="required"><br><br>
        請輸入密碼<input id="password" name="password" type="password" placeholder="僅支持正整數" required="required"><br><br>
        <input type="submit" value="登陸"> &nbsp;<button>
        <a href="/goregister1">註冊</a></button>
    </form>
</div>
</body>
</html>

9.註冊頁面  register1.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>註冊</title>
</head>
<body>
<form action="/register1" method="post">
    用戶姓名:<input type="text" name="name" /></br>
    用戶密碼:<input type="password" name="password" placeholder="僅支持整數" /></br>
    確認密碼:<input type="password" name="password2" placeholder="僅支持整數" /></br>

    <input type="submit" value="註冊">
</form>
</body>
</html>

10.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用spring boot寫一個hello1</title>
</head>
<body>
      Hello1
</body>
</html>

11運行結果:

相關文章
相關標籤/搜索