spring-boot mybatis 注解整合

1 pom.xml

 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </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>

2 application.properties

# Mysql数据库连接配置 : com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.101.111:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

#开启驼峰命名匹配映射
mybatis.configuration.map-underscore-to-camel-case=true

3 mapper

//标识该接口是mybatis的接口文件,并且让springboot能够扫描到该接口,生成该接口的代理对象,存到容器中
@Mapper
public interface CommentMapper {

    //根据id查询对应评论信息
    @Select("select * from t_comment where id = #{id}")
    public Comment findById(Integer id);

}

此处也可以不用添加@Mapper 注解 在启动类上面添加@MapperScan

@MapperScan("com.demo.mapper")
@SpringBootApplication
public class SpringbootDemoDataApplication {

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

}

4 test

@RunWith(SpringRunner.class)
@SpringBootTest
class Springboot03DataApplicationTests {

    @Autowired
    private CommentMapper commentMapper;

    @Test
    void contextLoads() {
        Comment comment = commentMapper.findById(1);
        System.out.println(comment);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值