使用idea快速创建springbootWeb项目(springboot+springWeb+mybatis-Plus)

idea快速创建springbootWeb项目

详细步骤如下

1)创建项目

2)选择springboot版本

3)添加web依赖

4)添加Thymeleaf

5)添加lombok依赖

然后点击create进入下一步

双击pom.xml文件

6)添加mybatis-plus依赖

这里使用的springboot版本比较新,mybatis-plus-boot-starter 不兼容 ,需要一下导入一下依赖

复制代码
<!--导入mybatis-plus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.7</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>3.0.3</version>
</dependency>

依赖导入成功

7)导入mysql驱动

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.33</version>

</dependency>

8)配置application.yml文件

把applicaiton.propitious删除创建application.yml

复制代码
spring:
  application:
    name: xiji

  datasource:
    url: jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root #数据库登录名
    password: root #数据库密码
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml

9)在mysql中 ==》 创建数据库users==》创建表user

sql 复制代码
CREATE TABLE `user` (
  `userid` int NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `username` varchar(255) DEFAULT NULL COMMENT '用户名字',
  `userPassword` varchar(255) DEFAULT NULL COMMENT '用户密码',
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

创建完毕之后如下

10)创建包

1.创建controller包

依次创建如下包结构

2.controller包下的代码如下

java 复制代码
package com.xiji.springbootweb.controller;

import com.xiji.springbootweb.entiy.User;

import com.xiji.springbootweb.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @GetMapping("/")
    public String index(){
        return "index";
    }
    @GetMapping("/list")
    public  String list(Model model){
        List<User> list = userService.list();
        model.addAttribute("list", list);
        return "model";
    }
}
@Controller注解用来标注controller层
@Resource用来注入容器中的组件
@Autowired 这个也可以用来注入组件

3.entiy包下的代码如下

java 复制代码
package com.xiji.springbootweb.entiy;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User {
    @TableField("userid")
    private int userid;
    @TableField("username")
    private String username;
    @TableField("userPassword")
    private String userPassword;
}
@Data用来生成get,set方法
@NoArgsConstructor用来生成无参构造方法
@AllArgsContructor用来生成全参构造
@TableName() 数据库表名
@TableField() 数据表列名

4.mapper包下的代码如下

java 复制代码
package com.xiji.springbootweb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiji.springbootweb.entiy.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}
@mapper用来标注持久层

UserMapper 继承 BaseMapper<实体> 原因里面有很多已经实现的方法

5.service包下impl包的代码如下

java 复制代码
package com.xiji.springbootweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiji.springbootweb.entiy.User;
import com.xiji.springbootweb.mapper.UserMapper;
import com.xiji.springbootweb.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

}
@service用来表组service层的
UserServiceImpl 继承Service<Mapper,实体> 实现 UserService 原因里面有很多已经实现的方法

6.service包下的代码

java 复制代码
package com.xiji.springbootweb.service;


import com.baomidou.mybatisplus.extension.service.IService;
import com.xiji.springbootweb.entiy.User;

public interface UserService extends IService<User> {
}
UserService继承 IService 接口

7.resource下mapper代码如下

java 复制代码
<?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.xiji.springbootweb.mapper.UserMapper">

</mapper>

8.resource下templates代码如下

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body >
    <div class="bgStyle">
        <h1>这是主页</h1>
    </div>
</body>
</html>

9.resource下model页的代码

html 复制代码
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        <h1 > 这是list页面</h1>
        <h1 th:text="${list}"></h1>
    </div>
</body>
</html>

11)点击启动

12)访问localhost:8080

13)访问localhost:8080/list

需要自己往表中添加数据

相关推荐
考虑考虑13 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613513 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊14 小时前
Java学习第22天 - 云原生与容器化
java
渣哥16 小时前
原来 Java 里线程安全集合有这么多种
java
间彧16 小时前
Spring Boot集成Spring Security完整指南
java
间彧16 小时前
Spring Secutiy基本原理及工作流程
java
Java水解17 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆19 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学20 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole20 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端