目录
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 不兼容 ,需要一下导入一下依赖
- <!--导入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
- 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包下的代码如下
- 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包下的代码如下
- 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包下的代码如下
- 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包的代码如下
- 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包下的代码
- 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代码如下
- <?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代码如下
- <!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>
- <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
需要自己往表中添加数据
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |