【springboot+mybatis实现CURD模版项目-Jesus】

springboot+mybatis实现CURD模版项目-Jesus

    • STEP 1 项目创建
  •    1.1 新建Spring Initializr项目
  •    1.2 选择需要的依赖 springboot有2.7.2直接选272
    • STEP 2 配置更改
  •    2.1更改maven配置
  •    2.2 检查项目配置jdk、sdk、jre版本一致
  •    2.3 检查pom文件,Maven-Reload project构建下载包,不报红即可
  •    2.4 删除properties文件,改用yaml配置文件
  •    2.5 启动一下
    • STEP 3 实现业务CRUD
  •    3.1 库中建表
  •    3.2 项目结构
  •    3.3 实体类、controller层、service层、mapper数据访问层、分别写代码
  •    3.4 启动类增加注解

目的:创建一个新项目Jesus(基于idea2022),实现CRUD,该项目作为学习模版。

————————————————————————————————————————

STEP 1 项目创建

   1.1 新建Spring Initializr项目

  

   1.2 选择需要的依赖 springboot有2.7.2直接选272

  
【springboot+mybatis实现CURD模版项目-Jesus】_第1张图片

STEP 2 配置更改

   2.1更改maven配置

【springboot+mybatis实现CURD模版项目-Jesus】_第2张图片

   2.2 检查项目配置jdk、sdk、jre版本一致

【springboot+mybatis实现CURD模版项目-Jesus】_第3张图片
【springboot+mybatis实现CURD模版项目-Jesus】_第4张图片
【springboot+mybatis实现CURD模版项目-Jesus】_第5张图片

   2.3 检查pom文件,Maven-Reload project构建下载包,不报红即可

   Maven的 Plugins 报红,执行命令mvn dependency:resolve-plugins,该命令的作用是:解决依赖关系,根据POM文件,下载或者更新该项目所依赖的库文件。
   pom文件会报版本不匹配,报错搜一下更新成适配版本就行了,下面是最终可用的pom文件。
pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>JesusartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>Jesusname>
    <description>Jesusdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.2version>
        <relativePath/> 
    parent>


    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <mybatis.spring.boot.version>2.1.3mybatis.spring.boot.version>
        <java.version>1.8java.version>
        <mysql.version>8.0.29mysql.version>
        <druid.version>1.2.6druid.version>
        <commons.lang.version>2.6commons.lang.version>
    properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jdbcartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-web-servicesartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>${mybatis.spring.boot.version}version>
        dependency>

        <dependency>
            <groupId>commons-langgroupId>
            <artifactId>commons-langartifactId>
            <version>${commons.lang.version}version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql.version}version>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>${druid.version}version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

   2.4 删除properties文件,改用yaml配置文件

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080
 
spring:
  datasource:
    username: root【数据库名】
    password: zxy000123【数据库密码】
    url: jdbc:mysql://localhost:3306/zxy【库名称】?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example.entity
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

   2.5 启动一下

【springboot+mybatis实现CURD模版项目-Jesus】_第6张图片
【springboot+mybatis实现CURD模版项目-Jesus】_第7张图片

STEP 3 实现业务CRUD

   3.1 库中建表

CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

   3.2 项目结构

   新建controller、entity、mapper、service文件夹在com.example.jesusu下
   新建mapping在resurces下
【springboot+mybatis实现CURD模版项目-Jesus】_第8张图片

   3.3 实体类、controller层、service层、mapper数据访问层、分别写代码

entity—User.java

package com.example.jesus.entity;

public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }
}

Controller—UserController.java

package com.example.jesus.controller;

import com.example.jesus.entity.User;
import com.example.jesus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("/testBoot")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getUser/{id}")
    public String GetUser(@PathVariable int id){
        return userService.Sel(id).toString();
    }
}

Service—UserService.java

package com.example.jesus.service;

import com.example.jesus.entity.User;
import com.example.jesus.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User Sel(int id){
        return userMapper.Sel(id);
    }
}

Mapper—UserMapper.java

package com.example.jesus.mapper;

import com.example.jesus.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface UserMapper {

    User Sel(int id);
}

Mapper—UserMapping.xml

<?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.example.jesus.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.example.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>

    <select id="Sel" resultType="com.example.entity.User">
        select * from user where id = #{id}
    </select>

</mapper>

   3.4 启动类增加注解

   加上注解用于给出需要扫描的mapper文件路径
   @MapperScan(“com.example.mapper”)

package com.example.jesus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@MapperScan("com.example.jesus.mapper") //扫描的mapper
@SpringBootApplication
public class JesusApplication {

    public static void main(String[] args) {
        SpringApplication.run(JesusApplication.class, args);
        System.out.println("启动Jesus项目成功");
    }

}

你可能感兴趣的:(spring,boot,java,后端,spring,mybatis,intellij-idea)