springboot+mybatis plus基礎+IService

創建springboot項目:

1.pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo111</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo111</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <!--集成mybatis -->
        <!-- 與數據庫操作相關的依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

2.controller:

package com.example.demo111.controller;


import com.example.demo111.Bean.Own;
import com.example.demo111.service.OwnSercvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/Own")
public class OwnController {

    @Autowired
    private OwnSercvice ownSercvice;

    @GetMapping("sing")
    public void sing(){
        System.out.println("111111111");
        Own byId = ownSercvice.getById(1);
        System.out.println(byId);
    }

}

3.service:

package com.example.demo111.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo111.Bean.Own;

public interface OwnSercvice extends IService<Own> {
}

4.serviceImpl:

package com.example.demo111.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo111.Bean.Own;
import com.example.demo111.mapper.OwnMapper;
import com.example.demo111.service.OwnSercvice;
import org.springframework.stereotype.Service;

@Service
public class OwnServiceImpl extends ServiceImpl<OwnMapper, Own> implements OwnSercvice {

}

5.mapper:

package com.example.demo111.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo111.Bean.Own;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface OwnMapper extends BaseMapper<Own> {
}

6.

Demo111Application:
package com.example.demo111;

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

@SpringBootApplication
@MapperScan("com.example.demo111.mapper")
public class Demo111Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo111Application.class, args);
    }

}

7.application.properties:

spring:
server.port=8091
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shopping_house?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# ====================MybatisPlus====================

8.bean:
package com.example.demo111.Bean;

import lombok.Data;

@Data
public class Own {

    private Integer id;
    private String name;
    private char sex;
    private Integer year;
    private String address;
}

9表格數據庫自建own

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章