springboot-整合mybatis-annotations注解方式

前言

前章讲了下springboot使用JPA的当时连接mysql,本章我们来重点学习下mybatis连接数据库,为何重点讲这个?当然是因为流行、好用、用的人多!!

mybatis操作mysql又分两种方式,一种是注解,一种是mapper.xml文件。本章着重于注解方式的使用,因为比较简单。至于xml方式,下一章会讲。

创建一个空项目

好的,如果你和博主一步一步做的话,对这个流程应该无比的熟悉了,还是用IDEA无比流畅的创建出一个springboot空项目,依赖依旧手动加载。

添加依赖

打开pom.xml,添加如下依赖:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

如图
1.png

记住这三个依赖,因为,接下来的几乎所有的案例都会99%有这三个依赖。实际开发中更是如此。

添加配置

老规矩,更改application.properties为application.yml,添加内容:

spring:
  datasource:
    url: jdbc:mysql://192.168.145.131:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

建库

创建test库建表:

CREATE TABLE `person` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `person` VALUES ('1', '1', '1');
INSERT INTO `person` VALUES ('2', '2', '2');
INSERT INTO `person` VALUES ('3', '3', '3');
INSERT INTO `person` VALUES ('4', '4', '4');
INSERT INTO `person` VALUES ('5', '5', '5');
INSERT INTO `person` VALUES ('6', '6', '6');
INSERT INTO `person` VALUES ('7', '7', '7');
INSERT INTO `person` VALUES ('8', '8', '8');
INSERT INTO `person` VALUES ('9', '9', '9');
INSERT INTO `person` VALUES ('10', '10', '10');
INSERT INTO `person` VALUES ('11', '11', '11');

表记录自行添加几条吧

完善

右键包创建如图所示的package和class文件

2.png

controller/DemoController:

package com.mrcoder.sbmannotations.controller;
import com.mrcoder.sbmannotations.domain.Demo;
import com.mrcoder.sbmannotations.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;

@RestController
public class DemoController {
    @Autowired
    private DemoService demoService;

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
    public Demo getPersonById(@PathVariable int id) {
        return demoService.getPersonById(id);
    }

    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public ArrayList<Demo> getPersonList() {
        return demoService.getPersonList();
    }
}

dao/DemoDao:

package com.mrcoder.sbmannotations.dao;
import com.mrcoder.sbmannotations.domain.Demo;
import org.apache.ibatis.annotations.*;
import java.util.ArrayList;

@Mapper
public interface DemoDao {
    @Select("select * from person where id = #{id}")
    // 返回 Map 结果集
    @Results({
            @Result(property = "id", column = "id"),
    })
    Demo getPersonById(@Param("id") int id);

    @Select("select * from person")
    ArrayList<Demo> getPersonList();

}

domain/Demo:

package com.mrcoder.sbmannotations.domain;

public class Demo {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return
                "id=" + id +
                        ", name='" + name + '\'' +
                        ", age=" + age
                ;
    }
}

service/DemoService:

package com.mrcoder.sbmannotations.service;

import com.mrcoder.sbmannotations.domain.Demo;

import java.util.ArrayList;

public interface DemoService {
    Demo getPersonById(int id);
    ArrayList<Demo> getPersonList();
}

service/impl/DemoService:

package com.mrcoder.sbmannotations.service.impl;

import com.mrcoder.sbmannotations.dao.DemoDao;
import com.mrcoder.sbmannotations.domain.Demo;
import com.mrcoder.sbmannotations.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private DemoDao demoDao;

    public Demo getPersonById(int id) {
        return demoDao.getPersonById(id);
    }

    public ArrayList<Demo> getPersonList() {
        return demoDao.getPersonList();
    }

}

运行

3.png

4.png

项目地址

https://github.com/MrCoderStack/SpringBootDemo/tree/master/sbm-annotations

https://gitee.com/MrCoderStack/MrCoderStackBlog/tree/master/sbm-annotations

Tips

windows端口占用问题解决

#找到对应端口的PID
netstat -ano|findstr "8080"

5.png

#根据PID找到程序名
tasklist|findstr "18804"

6.png

#杀死程序
taskkill /f /t /im java.exe

猜你喜欢

转载自blog.csdn.net/MrCoderStack/article/details/88548127