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

嚴華  金牌会员 | 2024-10-19 07:53:18 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 913|帖子 913|积分 2739

目录
  
  idea快速创建springbootWeb项目
  
  详细步骤如下
  1)创建项目
  2)选择springboot版本
  3)添加web依赖
  4)添加Thymeleaf
  5)添加lombok依赖
  6)添加mybatis-plus依赖        
  7)导入mysql驱动
  8)设置application.yml文件
  9)在mysql中 ==》 创建数据库users==》创建表user
  10)创建包
  1.创建controller包
  2.controller包下的代码如下
  @Controller注解用来标注controller层
  @Resource用来注入容器中的组件
  @Autowired 这个也可以用来注入组件
  3.entiy包下的代码如下
  @Data用来天生get,set方法
  @NoArgsConstructor用来天生无参构造方法
  @AllArgsContructor用来天生全参构造
  @TableName() 数据库表名
  @TableField() 数据表列名
  4.mapper包下的代码如下
  @mapper用来标注长期层
  
  5.service包下impl包的代码如下
  @service用来表组service层的
  UserServiceImpl 继承Service 实现 UserService 原因内里有许多已经实现的方法,实体>
  6.service包下的代码
  UserService继承 IService 接口
  7.resource下mapper代码如下 
  8.resource下templates代码如下 
  9.resource下model页的代码
  11)点击启动
  
  12)访问localhost:8080
  13)访问localhost:8080/list
  
  
  idea快速创建springbootWeb项目



详细步骤如下

1)创建项目



2)选择springboot版本


3)添加web依赖


4)添加Thymeleaf


5)添加lombok依赖


然后点击create进入下一步
双击pom.xml文件

6)添加mybatis-plus依赖        

这里利用的springboot版本比较新,mybatis-plus-boot-starter 不兼容 ,需要一下导入一下依赖
  
  1. <!--导入mybatis-plus-->
  2. <dependency>
  3.     <groupId>com.baomidou</groupId>
  4.     <artifactId>mybatis-plus-boot-starter</artifactId>
  5.     <version>3.5.7</version>
  6.     <exclusions>
  7.         <exclusion>
  8.             <groupId>org.mybatis</groupId>
  9.             <artifactId>mybatis-spring</artifactId>
  10.         </exclusion>
  11.     </exclusions>
  12. </dependency>
  13. <!-- mybatis-spring -->
  14. <dependency>
  15.     <groupId>org.mybatis</groupId>
  16.     <artifactId>mybatis-spring</artifactId>
  17.     <version>3.0.3</version>
  18. </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
  
  1. spring:
  2.   application:
  3.     name: xiji
  4.   datasource:
  5.     url: jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
  6.     username: root #数据库登录名
  7.     password: root #数据库密码
  8.     driver-class-name: com.mysql.cj.jdbc.Driver
  9. mybatis-plus:
  10.   mapper-locations: classpath:mapper/*.xml
复制代码
9)在mysql中 ==》 创建数据库users==》创建表user

  1. CREATE TABLE `user` (
  2.   `userid` int NOT NULL AUTO_INCREMENT COMMENT '用户id',
  3.   `username` varchar(255) DEFAULT NULL COMMENT '用户名字',
  4.   `userPassword` varchar(255) DEFAULT NULL COMMENT '用户密码',
  5.   PRIMARY KEY (`userid`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
复制代码
创建完毕之后如下

10)创建包

1.创建controller包



依次创建如下包结构

2.controller包下的代码如下

  1. package com.xiji.springbootweb.controller;
  2. import com.xiji.springbootweb.entiy.User;
  3. import com.xiji.springbootweb.service.UserService;
  4. import jakarta.annotation.Resource;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.Banner;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.util.List;
  12. @Controller
  13. public class UserController {
  14.     @Resource
  15.     UserService userService;
  16.     @GetMapping("/")
  17.     public String index(){
  18.         return "index";
  19.     }
  20.     @GetMapping("/list")
  21.     public  String list(Model model){
  22.         List<User> list = userService.list();
  23.         model.addAttribute("list", list);
  24.         return "model";
  25.     }
  26. }
复制代码
@Controller注解用来标注controller层

@Resource用来注入容器中的组件

@Autowired 这个也可以用来注入组件

3.entiy包下的代码如下

  1. package com.xiji.springbootweb.entiy;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableName;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. @Data
  8. @NoArgsConstructor
  9. @AllArgsConstructor
  10. @TableName("user")
  11. public class User {
  12.     @TableField("userid")
  13.     private int userid;
  14.     @TableField("username")
  15.     private String username;
  16.     @TableField("userPassword")
  17.     private String userPassword;
  18. }
复制代码
@Data用来天生get,set方法

@NoArgsConstructor用来天生无参构造方法

@AllArgsContructor用来天生全参构造

@TableName() 数据库表名

@TableField() 数据表列名

4.mapper包下的代码如下

  1. package com.xiji.springbootweb.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.xiji.springbootweb.entiy.User;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper
  6. public interface UserMapper extends BaseMapper<User> {
  7. }
复制代码
@mapper用来标注长期层

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


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

  1. package com.xiji.springbootweb.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.xiji.springbootweb.entiy.User;
  4. import com.xiji.springbootweb.mapper.UserMapper;
  5. import com.xiji.springbootweb.service.UserService;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  9. }
复制代码
@service用来表组service层的

UserServiceImpl 继承Service<Mapper,实体> 实现 UserService 原因内里有许多已经实现的方法

6.service包下的代码

  1. package com.xiji.springbootweb.service;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.xiji.springbootweb.entiy.User;
  4. public interface UserService extends IService<User> {
  5. }
复制代码
UserService继承 IService 接口

7.resource下mapper代码如下 

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.xiji.springbootweb.mapper.UserMapper">
  4. </mapper>
复制代码
8.resource下templates代码如下 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6. </head>
  7. <body >
  8.     <div class="bgStyle">
  9.         <h1>这是主页</h1>
  10.     </div>
  11. </body>
  12. </html>
复制代码
9.resource下model页的代码

  1. <html>
  2. <head>
  3.     <meta charset="UTF-8">
  4.     <meta name="viewport"
  5.           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Document</title>
  8. </head>
  9. <body>
  10.     <div>
  11.         <h1 > 这是list页面</h1>
  12.         <h1 th:text="${list}"></h1>
  13.     </div>
  14. </body>
  15. </html>
复制代码
11)点击启动




12)访问localhost:8080



13)访问localhost:8080/list


需要自己往表中添加数据


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

嚴華

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表