0


使用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 不兼容 ,需要一下导入一下依赖

  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

需要自己往表中添加数据


本文转载自: https://blog.csdn.net/2301_76862031/article/details/141612680
版权归原作者 惜.己 所有, 如有侵权,请联系我们删除。

“使用idea快速创建springbootWeb项目(springboot+springWeb+mybatis-Plus)”的评论:

还没有评论