Springboot整合mybatis(註解而且能看明白版本)

這篇文章主要講解Springboot整合Mybatis實現一個最基本的增刪改查功能,整合的方式有兩種一種是註解形式的,也就是沒有Mapper.xml文件,還有一種是XML形式的,我推薦的是使用註解形式,爲什麼呢?因爲更加的簡介,減少不必要的錯誤。

一、環境配置

對於環境配置我是用了一張表來展示,版本之間差異不大,你可以基於其他版本進行測試。Idea我已經破解了,破解碼是我羣裏的一個朋友提供的,親測可用。而且在2019的版本也可以永久破解。需要的可以私聊我。因爲我之前寫過破解的文章,因爲某些原因,被平臺刪了。

名稱 版本
Idea 2018專業版(已破解)
Maven 3.6.0
SpringBoot 2.2.2
Mybatis 5.1.44(版本高點比較好)
Navicat(可視化工具) 12(已破解)
jdk 1.8

這就是我的基本的環境。下一步我們一步一步來整合一波

二、整合Mybatis

第一步:數據庫新建Person表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for person
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 
    COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 
CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

這個表結構很簡單,也就是三個字段id、name、age。並以id爲主鍵且遞增。

第二步:新建Springboot項目

這個比較簡單,這裏先給出一個最終的目錄結構:

在這裏插入圖片描述

第三步:導入相關依賴

 <!--================================================-->
		<!--springboot開發web項目的起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 加載mybatis整合springboot -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- MySQL的jdbc驅動包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
<!--================================================-->

OK,我們只需要加上這些依賴即可。在我們的pom文件。

第四步:更改application.yml配置文件

我們只需要把application.properties文件改爲yml格式即可。此時添加相關配置

#配置服務器信息
server:
  port: 8082
spring:
  #mysql數據庫相關配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/uav?characterEncoding=utf8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
#mybatis依賴
mybatis:
  type-aliases-package: com.fdd.mybatis.dao

這裏的配置有點多,不過還是一個最基本的配置都在這。

第五步:新建dao包,在dao包下新建Person類

public class Person {
    private int id ;
    private String name;
    private int  age;
    public Person() {
    }
    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
	//getter和setter方法
	//toString方法
}

這個類是和我們數據庫中的Person類一一對應的。

第六步:新建mapper包,在mapper新建PersonMapper類

在這個類中,我們實現基本的增刪改查功能接口:

@Mapper
public interface PersonMapper {
    //增加一個Person
    @Insert("insert into person(id,name,age)values(#{id},#{name},#{age})")
    int insert(Person person);
    //刪除一個Person
    @Delete("delete from person where id = #{id}")
    int deleteByPrimaryKey(Integer id);
    //更改一個Person
    @Update("update person set name =#{name},age=#{age} where id=#{id}")
    int updateByPrimaryKey(Integer id);
    //查詢一個Person
    @Select("select id,name ,age from person where id = #{id}")
    Person selectByPrimaryKey(Integer id);
    //查詢所有的Person
    @Select("select id,name,age from person")
    List<Person> selectAllPerson();
}

這就是最基本的一個增刪改查操作的接口。

第七步:新建service包,在service包創建PersonService接口

public interface PersonService {
    //增加一個Person
    int insertPerson(Person person);
    //刪除一個Person
    int deleteByPersonId(Integer id);
    //更改一個Person
    int updateByPersonId(Person record);
    //查詢一個Person
    Person selectByPersonId(Integer id);
    //查詢所有的Person
    List<Person> selectAllPerson();
}

第八步:在service包下創建PersonServiceImpl接口實現類

@Service
public class PersonServiceImpl implements  PersonService {
    @Autowired
    private PersonMapper personMapper;
    @Override
    public int insertPerson(Person person) {
        return personMapper.insert(person);
    }
    @Override
    public int deleteByPersonId(Integer id) {
        return personMapper.deleteByPrimaryKey(id);
    }
    @Override
    public int updateByPersonId(Person record) {
        return personMapper.updateByPrimaryKey(record);
    }
    @Override
    public Person selectByPersonId(Integer id) {
        return personMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<Person> selectAllPerson() {
        return personMapper.selectAllPerson();
    }
}

第九步:編寫controller層

@RestController
public class PersonController {
    @Autowired
    private PersonService personService;
    @RequestMapping(value = "/add")
    public String students () {
        Person person = new Person();
        person.setId(1);
        person.setName("java的架構師技術棧");
        person.setAge(18);
        int result = personService.insertPerson(person);
        System.out.println("插入的結果是:"+result);
        return result+"";
    }
    @RequestMapping(value = "/findAll")
    public String findAll () {
        List<Person> people = personService.selectAllPerson();
        people.stream().forEach(System.out::println);
        return people.toString()+"";
    }
}

第十步:在啓動主類添加掃描器

@SpringBootApplication
@MapperScan("com.fdd.mybatis.mapper")
public class SpringBootMybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }
}

第十一步:測試

在瀏覽器輸入相應的路徑即可。OK。大功告成。只要你按照上面的步驟一步一步來,就一定OK。

在這裏插入圖片描述

發佈了255 篇原創文章 · 獲贊 74 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章