Spring Boot通过纯注解方式整合mybatis

一、导入依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

二、创建Mapper文件

public interface DemoMapper {

    @Select("SELECT demo_id , demo_name FROM tb_demo WHERE demo_id=#{id}")
    @Results({
              @Result(property = "demoId", column = "demo_id" , javaType = Integer.class),
              @Result(property = "demoName", column = "demo_name", javaType = String.class)
            })
    DemoDTO selectOneById(@Param("id") Integer id);
}

以上采用mybatis的注解方式。

@Select:查询语句

@Results:结果映射配置。如果属性名称和表的字段名称一致的话,则不需要此配置,会自动完成映射。

三、创建Java Bean类

public class DemoDTO implements Serializable {

    private Integer demoId;
    private String demoName;

    public DemoDTO() {
    }

    public DemoDTO(Integer demoId, String demoName) {
        this.demoId = demoId;
        this.demoName = demoName;
    }

    public Integer getDemoId() {
        return demoId;
    }

    public void setDemoId(Integer demoId) {
        this.demoId = demoId;
    }

    public String getDemoName() {
        return demoName;
    }

    public void setDemoName(String demoName) {
        this.demoName = demoName;
    }
}

对应的数据库结构如下:

+———–+————-+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+————-+——+—–+———+—————-+
| demo_id | int(11) | NO | PRI | NULL | auto_increment |
| demo_name | varchar(22) | YES | | NULL | |
+———–+————-+——+—–+———+—————-+

四、application.properties配置

spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/java1706
spring.datasource.username=root
spring.datasource.password=123456

以上只是配置了数据源,跟传统意义上的mybatis的配置简化了很多。Spring boot会自动将数据源注入到SqlSessionFactory中,并且自动将SqlSessionFactory注入到mapper中,不需要我们操心,简化了配置过程,这也是Spring Boot的目的。

注意:这个位置的spring.datasource.username,不要写成了spring.datasource.user,否则会出现连接数据库的异常

spring.datasource.type:指定采用数据源的类型,也可以不配置,Spring Boot有默认的数据源。

五、配置Mapper扫描

import com.qianfeng.springbootdemo.dto.DemoDTO;
import com.qianfeng.springbootdemo.mapper.DemoMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan("com.qianfeng.springbootdemo.mapper") //mapper扫描
public class SpringbootdemoApplicationTests {

    @Autowired
    private DemoMapper demoMapper;

    @Test
    public void testCase1(){
        System.out.println(demoMapper);
        DemoDTO demoDTO = demoMapper.selectOneById(1);
        System.out.println(demoDTO.getDemoName());
    }
}

以上代码的@MapperScan(“com.qianfeng.springbootdemo.mapper”)此句用来扫描DemoMapper文件。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值