快速springboot + thymeleaf + mybat

2021-05-17  本文已影响0人  苏小小北

代码地址

https://github.com/HinsonHsu/springboot_thymeleaf

技术框架

相关名词解释

相关流程:

当用户在浏览器输入地址后,在controller层搜索搜索到匹配的controller,controller中会调用service的方法,进行数据库的增删改查,获取到结果后,再通过Model方式,将数据传递给thymeleaf的模板中,模板将标签替换为后端数据,形成最终的html页面,最终通过浏览器渲染出来。

准备工作

一键生成项目代码

spring提供了快速便捷的方式,https://start.spring.io/

相关属性

确认引入依赖:

示范

image.png

初始化项目

将下载好的包解压,然后用idea打开项目。一般都能够自动加载(第一次会很慢,耐心等待)

按照分层设计添加代码:

image.png

运行:

运行FirstDemoApplication的main方法
输入:http://127.0.0.1:8080/index

image.png

相关代码展示:

server.port=8080

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/first_demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

package cn.zhangsan.first_demo.controller;

import cn.zhangsan.first_demo.model.User;
import cn.zhangsan.first_demo.service.UserService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create on 2021/5/17
 **/
@RequestMapping("/")
@Controller
public class MainController {

    @Autowired
    UserService userService;


    @RequestMapping("/index")
    public String index(Model model) {
        List<User> userList = userService.getAllUsers();
        model.addAttribute("userList", userList);
        return "index";
    }

    /**
     * ResponseBody 作用是直接返回java object对象,不通过模板渲染
     */
    @RequestMapping("/get_user_pwd")
    @ResponseBody
    public Object getUserPwd(@RequestParam("id") int id) {
        Map<String, Object> resMap = new HashMap<>();
        String userPwd = userService.getUserPwd(id);
        if (userPwd == null || "".equals(userPwd)) {
            resMap.put("data", "密码为空");
        } else {
            resMap.put("data", userPwd);
        }
        return resMap;
    }

}

package cn.zhangsan.first_demo.dao;

import cn.zhangsan.first_demo.model.User;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Create on 2021/5/17
 **/
@Mapper
public interface UserDao {

    String table = "user";

    @Select("select * from " + table)
    List<User> findAll();

    @Select("select password from " + table + " where id=#{id}")
    String findPwdById(@Param("id") int id);

}

package cn.zhangsan.first_demo.model;

/**
 * Create on 2021/5/17
 *
 **/
public class User {

    public int id;
    public String username;
    public String password;

}
package cn.zhangsan.first_demo.service;

import cn.zhangsan.first_demo.dao.UserDao;
import cn.zhangsan.first_demo.model.User;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create on 2021/5/17
 *
 **/
@Service
public class UserService {

    @Autowired
    UserDao userDao;

    public List<User> getAllUsers() {
        return userDao.findAll();
    }

    public String getUserPwd(int id) {
        return userDao.findPwdById(id);
    }

}

db.sql


SET NAMES utf8mb4;

CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '' COMMENT 'title',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '' COMMENT 'title',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint(1) DEFAULT '0' COMMENT '0有效1无效',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;

INSERT INTO `user` (`id`, `username`, `password`, `create_time`, `update_time`, `deleted`)
VALUES
    (1,'admin','admin','2021-05-15 14:33:25','2021-05-15 14:33:25',0),
    (2,'visitor','vistor_pwd','2021-05-15 14:33:25','2021-05-17 19:13:41',0);

/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
上一篇 下一篇

猜你喜欢

热点阅读