SpringBoot整合Mybatis,SpringMVC简单例子

SpringBoot

目录结构

在这里插入图片描述

数据库

在这里插入图片描述

代码

PersonController

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @GetMapping("/getPersonInfo")
    public String getPersonInfo( Long tel ) {
        Person person = personService.getPersonInfo(tel);
        return person.toString();
    }
}

Person

public class Person {

    private Integer id;
    private String name;
    private Long tel;
    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 Long getTel() {
        return tel;
    }

    public void setTel( Long tel ) {
        this.tel = tel;
    }

    public Integer getAge() {
        return age;
    }

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

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

PersonMapper

@Component
public interface PersonMapper {

    @Select("select * from person where tel = #{tel}")
    Person getPersonInfo( Long tel );
}

PersonServiceImpl

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;


    public Person getPersonInfo( Long tel ) {
        Person person = personMapper.getPersonInfo(tel);
        return person;
    }
}

PersonService

public interface PersonService {
    Person getPersonInfo( Long tel );
}

AppRun

@MapperScan("com.itcast.mapper")
@SpringBootApplication
public class AppRun {

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

application.yml

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/person?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: "123456"

pom.xml

 <dependencies>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!--数据源 默认为HikariCP 数据源-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <!--springboot整合mybatis依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!--mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
    </dependencies>

访问路径

http://localhost:8080/person/getPersonInfo?tel=15611111111

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值