入门级别 SpringBoot整合Mybatisplus +thymleft

创建表

CREATE TABLE spring.Untitled (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
password varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
PRIMARY KEY (id) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

INSERT INTO user VALUES (1, ‘a’, ‘123’);
INSERT INTO user VALUES (2, ‘b’, ‘456’);
INSERT INTO user VALUES (3, ‘c’, ‘789’);

创建项目

在这里插入图片描述

Spring Boot 基本导包
在这里插入图片描述
在这里插入图片描述

配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/spring
thymeleaf:
  prefix: classpath:/templates  #prefix:指定模板所在的目录
  check-template: false
  check-template-location: false
  cache: false  #cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
  suffix:  .html
  #encoding: UTF-8
  #content-type: text/html
  mode: HTML5
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    banner: false

实体类

@Data
public class User {
@TableId(value = "id",type= IdType.AUTO)//使用新增时自动增长
    int id;
    
    String password;
    //数据库要与实体类属性一致否则使用@TableField(value = "password") value里放数据库类名
    String name;
}

Dao层

//继承BaseMapper<User>Mybaisplus类里面自带增删改查方法
@Mapper
public interface UserDao extends BaseMapper<User> {
}

控制层用@RestController注解也可以实现,但是我这里引用的是thymeleaf模块,所以我这里用的是@Controller注解,Thymeleaf与Spring集成后,你可以:

在SpringMVC应用中完全替代JSP文件,就像控制JSP一样,使用SpringMvc的@Controller注解来映射;

Thymeleaf的模板文件在模板中使用SpringEL表达式来替换OGNL;
在模板中创建的表单,完全支持Beans和结果的绑定,包括使用PropertyEditor,转换,和验证等;
可以通过Spring来管理国际化文件显示国际化信息;

Controller层

package com.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * com.dao.Controller
 * liu
 * 2022/11/21
 * SpringBoot
 * 2022年11月21日10时39分
 */
@Controller

public class UserController {
    @Resource
    UserDao User;

    /**
     * 查询所有
     * @param modelAndView
     * @return
     */
    @RequestMapping("/all")
    public ModelAndView cs1(ModelAndView modelAndView){
        List<com.springboot.User> all1 = User.selectList(null);
        modelAndView.addObject("user",all1);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 根据ID删除
     * @param id
     * @param modelAndView
     * @return
     */
    @RequestMapping("/delete/{id}")
    public ModelAndView cs2(@PathVariable Integer id, ModelAndView modelAndView){
        User.deleteById(id);
        return cs1(modelAndView);
    }

    /**
     * 根据Id查询
     * @param id
     * @param modelAndView
     * @return
     */
    @RequestMapping("/id/{id}")
    public ModelAndView cs3(@PathVariable Integer id,ModelAndView modelAndView){
        com.springboot.User user= User.selectById(id);
        modelAndView.addObject("user",user);
        System.out.println(User);
        modelAndView.setViewName("update");
        return modelAndView;
    }

    /**
     * 根据id修改
     * @param user
     * @param modelAndView
     * @return
     */
    @RequestMapping("/update")
    public ModelAndView cs4(User user, ModelAndView modelAndView){
        System.out.println(user.id);
        User.updateById(user);
        return cs1(modelAndView);
    }

    /**
     * 新增
     * @param user
     * @param modelAndView
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView cs5(com.springboot.User user, ModelAndView modelAndView){
        User.insert(user);
        System.out.println("aaaa");
        return cs1(modelAndView);
    }

    /**
     * 跳转add。html页面
     * @param user
     * @param modelAndView
     * @return
     */
    @RequestMapping("/add.html")
    public  ModelAndView aa(User user,ModelAndView modelAndView){
        modelAndView.setViewName("add");
        return modelAndView;
    }
}

index.HTML

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <th>主键id</th>
        <th>名称</th>
        <th>密码</th>
        <td>操作</td>
    </tr>
    <tr th:each="b:${user}">
        <th th:text="${b.getId}"></th>
        <th th:text="${b.name}"></th>
        <th th:text="${b.password}"></th>
        <th><a href="add.html">新增</a>
            <a th:href="@{'id/'+${b.getId}}">修改</a>
            <a th:href="@{'delete/'+${b.getId}}">删除</a>
        </th>
    </tr>
</table>

</body>
</html>

add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="add">
   姓名 <input type="text" name="name">
   密码 <input type="text" name="password">
    <input type="submit">
</form>
</body>
</html>

update.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="../update"><!--因为开始是通过id 查询跳转update.html页面-->
    id  <input type="text" name="id" th:value="${user.id}" >
    姓名 <input type="text" name="name" th:value="${user.name}">
    密码 <input type="text" name="password" th:value="${user.password}">
    <input type="submit">
</form>
</body>
</html>

模板

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值