Luminous takes you into the field of management system development (1)

Luminous Prologue:

 

Ink marks are not young, letters and papers are not as good as you, don't miss the past, who planted a mile of peach blossom Acacia, the next life is long.

 

 

 

 
 
Text:
 
                                              Recognize the Tao with the Tao

Let's demonstrate how the communication between the front-end and back-end separated projects is passed

The process of creating and using SpringBoot project

 

 

 

At present, most of these projects in enterprises are SSM frameworks. SSH, which used to be very popular, is not used much now. In fact, SSH is really a pit, there will be a lot of configuration problems.

 

 

SpringBoot + SSM framework integration

Take a step by step look

The picture above is the complete project structure

 

First write an entity class

Match the table on the database

package com.example.demo.model.bean;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = -5611386225028407298L;

    private Integer id;
    private String name;
    private String password;
    private String address;

    // 可以省略get和set方法,大家自己设置即可
    // 我们写实体类记得要写完整
    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 String getPassword() {
        return password;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'JB', '111', '美国');
INSERT INTO `user` VALUES ('2', 'BL', '123', '新加坡');

 

Step 2: The pom of the project

 

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <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>
        <!-- springboot推荐的模板引擎,要想映射HTML/JSP,必须引入thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- 热部署用,改变代码不需要重启项目  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- mysql连接  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


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

</project>

This is the configuration file

# server config
server.port: 8081

# mysql
spring.datasource.url: jdbc:mysql://localhost:3306/umanager?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true
spring.datasource.username: root
spring.datasource.password: 1111
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
spring.datasource.dbcp2.validation-query: 'select 1'
spring.datasource.dbcp2.test-on-borrow: true
spring.datasource.dbcp2.test-while-idle: true
spring.datasource.dbcp2.time-between-eviction-runs-millis: 27800
spring.datasource.dbcp2.initial-size: 5
spring.datasource.dbcp2.min-idle: 5
spring.datasource.dbcp2.max-idle: 100
spring.datasource.dbcp2.max-wait-millis: 10000

# thymleaf
spring.thymeleaf.cache : false

# mybatis
mybatis.mapper-locations: classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case: true

package com.example.demo.model.dao;


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.example.demo.model.bean.User;


@Mapper
public interface UserDAO {

    public User find(@Param("name")String name, @Param("password")String password);

    // 注:额,快捷键  CRTL+Shift+O,快捷导入所有import,很实用
}

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.model.bean.User;
import com.example.demo.model.dao.UserDAO;

import javax.annotation.Resource;

// 这里提一提:@RestController = @Controller + @ResponseBody
@RestController
public class BasicController {

//    @Autowired idea写这个容易导致下面注入的显示红杠杠
    @Resource
    private UserDAO userDAO;

    @GetMapping(value = "")
    public String index() {
        return "login"; // 此处表示返回值是一个值为“login”的String。不指向界面的原因是类的注解是@RestController
    }


    @GetMapping(value = "index.hy")
    public ModelAndView index2() {
        return new ModelAndView("login"); // 此处指向界面
    }

    //下面这个是登录的方法,我们这里需要传入两个参数
    @GetMapping(value = "login.hy")
    public Object login(String name, String password) {
        System.out.println("传入参数:name=" + name + ", password=" + password);
        if (StringUtils.isEmpty(name)) {
            return "name不能为空";
        } else if (StringUtils.isEmpty(password)) {
            return "password不能为空";
        }
        User user = userDAO.find(name, password);
        if (user != null) {
            return user;
        } else {
            return "用户名或密码错误";
        }
    }

}

<?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.example.demo.model.dao.UserDAO">
    <select id="find" resultType="com.example.demo.model.bean.User">
    SELECT id, name, password, address from user where name = #{name} and password = #{password}
  </select>
</mapper>
<!DOCTYPE html>
<html>
<!-- meta这一句指定编码格式,能够防止中文乱码  -->
<meta charset="UTF-8" />
<head>
    <title>登录</title>
</head>
<body>
<form action="/login.hy" method="GET">
    用户名:<input type="text" id="name" name="name" />
    密码:  <input type="password" id="password" name="password" />
    <input type="button" value="登录" onclick="submit()" />
</form>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 1529 original articles · praised 305 · 180,000 views +

Guess you like

Origin blog.csdn.net/weixin_41987706/article/details/104672861