安装使用Spring boot 写一个hello1

一、创建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 设置数据库语句的操作

<?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.html

<!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运行结果:

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

时间: 2024-08-26 07:32:44

安装使用Spring boot 写一个hello1的相关文章

学记:为spring boot写一个自动配置

spring boot遵循"约定由于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇不是借助代码的生成来实现的,而是通过条件注解来实现的. 自动配置AutoConfiguration是实现spring boot的重要原理,理解AutoConfiguration的运行原理特别重要,自己写一个AutoConfiguration可以加深我们对spring boot的理解. 1.定义Type-saf

spring cloud教程之使用spring boot创建一个应用

<7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术还是Spring框架,主要是Spring 4.x,所以如果熟悉spring 4的人,能够更快的接受和学会这个框架.Spring boot可以看做是在spring框架基础上再包了一层,这一层包含方便开发者进行配置管理和快速开发的模块,以及提供了一些开箱即用的工具,比如监控等. Spring Boot官方文档有中

Spring Boot实现一个监听用户请求的拦截器

项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承WebMvcConfigurerAdapter // 增加拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new RequestLog()); } //这

模拟spring - 动手写一个spring AOP

一.前言 AOP (Aspect Oriented Programing) - 面向切面编程,它主要用于日志记录.性能分析.安全控制.事务处理.异常处理等方面. AOP主要使用JDK的反射和动态代理,AOP代理其实是由AOP框架动态生成的一个对象,该对象可作为目标对象使用,AOP代理包含了目标对象的全部方法,但AOP代理的方法与目标对象的方法存在差异:AOP方法在特定切入点添加了增强处理,并回调了目标对象的方法. 动态代理的文章请参考:http://blog.csdn.net/zdp072/ar

阿里微服务专家手写Spring Boot 实现一个简单的自动配置模块

为了更好的理解 Spring Boot 的 自动配置和工作原理,我们自己来实现一个简单的自动配置模块. 假设,现在项目需要一个功能,需要自动记录项目发布者的相关信息,我们如何通过 Spring Boot 的自动配置,更好的实现功能呢? 实战的开端 – Maven搭建 先创建一个Maven项目,我来手动配置下 POM 文件. 参数的配置 - 属性参数类 首先,我们定义一个自定义前缀,叫做 custom 吧.之前说到,这里的配置参数,可以通过 application.properties 中直接设置

使用Spring Boot创建一个应用

本文主要演示如何使用Spring Boot加速应用开发的.你可以访问Spring Initializr,快速构建属于你自己的基于Spring Boot的应用. 如图,一键即可生成项目. 1.开始构建项目 1.1)项目结构 1.2 pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmln

Spring Boot . 2 -- 用Spring Boot 创建一个Java Web 应用

通过 start.spring.io 创建工程 通过 IDEA 创建工程 ??<Spring Boot In Action> 中的例子 建立一个展示阅读列表的应用.不同的用户将读过的书的数据登记进来,每次进到页面都能看到相应的读书记录. 1. 首先登录页面 start.spring.io. 页面大概长这个样子: 点击空框内的链接,会展示更全面的参数选择.[参数填好后],选择 "Generate Project" 就可以将一个完整的Spring Boot 工程下载下来了. 工

spring chapter4 用SPRING BOOT创建一个项目

如何开发一个简单的“Hello World!”Web应用程序,该应用程序突出了Spring Boot的一些主要功能.我们使用Maven来构建这个项目,因为大多数IDE都支持它. 在开始之前,打开终端并运行以下命令以确保安装了有效的Java和Maven版本: 1:创建POM 我们需要从创建Maven pom.xml文件开始.打开喜欢的文本编辑器并添加以下内容: <?xml version="1.0" encoding="UTF-8"?> <proje

为spring boot 写的Controller中的rest接口配置swagger

1.pom.xml文件中加入下列依赖: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version></dependency> <dependency> <groupId>io.springfox</groupId&g