spring boot整合mybatis

pom.xml

<dependencies>
        <!--mybatis starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>

Controller

@Controller
public class TestController {

    @Autowired
    private TestServiceImpl testService;

    @RequestMapping("/testMapper")
    @ResponseBody
    public String testMapper(){
        return testService.findAllStudent().toString();
    }
}

ServiceImpl

@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private TestDao testDao;

    @Override
    public List<Student> findAllStudent() {
        List<Student> studentList=null;
        studentList=testDao.findAllStudent();
        return studentList;
    }
}

Dao

@Repository
public interface TestDao {

    public List<Student> findAllStudent();
}

***Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mxd.springbootproxy01.dao.TestDao">
    <select id="findAllStudent" resultType="Student">
        select * from student
    </select>
</mapper>

application.properties

#端口
server.port=8080
#编码格式
server.tomcat.uri-encoding=UTF-8
#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mxd
spring.datasource.username=root
spring.datasource.password=190125

#mybatis配置
#映射文件通配
mybatis.mapper-locations=classpath:mapper/'*.xml
#mybatis扫描别名包,和注解@Alias联用
mybatis.type-aliases-package=com/mxd/springbootproxy01/pojo

SpringBootApplication

package com.mxd.springbootproxy01;

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

@MapperScan("com.mxd.springbootproxy01.dao")
@SpringBootApplication
public class SpringBootProxy01Application {

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

目录结构

在这里插入图片描述

测试结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值