006-从零开始学习SpringBoot-Spring Boot集成MyBatis

Spring Boot集成MyBatis

引入依赖

<!--由于springboot整合mybatis版本中默认依赖mybatis,不需要再单独引入-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.22</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

编写配置文件:数据源、MyBatis配置

#数据源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root

#MyBatis配置
mybatis.mapper-locations=classpath:com/thrstones/mapper/*.xml
mybatis.type-aliases-package=com.thrstones.entity

在项目入口出加入映射扫描

@SpringBootApplication
@MapperScan("com.thrstones.dao")//在⼊⼝类中加⼊这个配置
public class Application {

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

}

建表

create table account
(
    id       int auto_increment
        primary key,
    name     varchar(80) null,
    age      int         null,
    birthday date        null
);

编写实体类

@Component
@ConfigurationProperties(prefix = "account")
public class Account {

    private int id;
    private String name;
    private int age;
    private Date birthday;
 
 	//省略setter()、getter()、toString()方法
 	//可以使用lombok插件,使用注解来解决
 
}

编写Dao接口、映射

public interface AccountDao {

    public List<Account> findAll();

}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thrstones.dao.AccountDao">

    <select id="findAll" resultType="Account">
        select * from account
    </select>

</mapper>

编写Service接口、和实现类

public interface AccountService {

    public List<Account> findAll();

}
@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountDao accountDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Account> findAll() {
        return accountDao.findAll();
    }
}

引入测试依赖、编写测试类

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestService {

    @Resource
    private AccountService accountService;

    @Test
    public void test(){
        List<Account> accountList = accountService.findAll();
        accountList.forEach(account -> System.out.println(account));
    }

}

测试结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值