springboot集成mybatis

public class Country {
    //实体类参考本人博客地址:https://blog.csdn.net/yl_hahha/article/details/80208161
    private long id;
    private String countryname;
    private String countrycode;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getCountryname() {
        return countryname;
    }
    public void setCountryname(String countryname) {
        this.countryname = countryname;
    }
    public String getCountrycode() {
        return countrycode;
    }
    public void setCountrycode(String countrycode) {
        this.countrycode = countrycode;
    }
    @Override
    public String toString() {
        return "Country [id=" + id + ", countryname=" + countryname + ", countrycode=" + countrycode + "]";
    }
}

/**
* service层是给controller调用,如果想写需要将dao包装成service,然后实现类来调用dao 来提供给controller调用
*/
@Mapper
public interface CountryMapper {
@Insert(“insert into country(countryname, countrycode) values(#{countryname}, #{countrycode})”)
int add(@Param(“countryname”) String countryname, @Param(“countrycode”) String countrycode);

@Select("select id, countryname as countryname, countrycode as countrycode from country")
List<Country> findCounList();

}

@SpringBootApplication
public class DemomybatisApplication {

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

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemomybatisApplicationTests {
    @Autowired(required = false)
    private CountryMapper mapper;
    @Test
    public void contextLoads() {
    }
    @Test
    public void testFindAll(){
        List<Country> counList = mapper.findCounList();
        Assert.assertTrue(counList.size()>1);
    }
    //
    @Test
    public void testInsert(){
        int add = mapper.add("shenzhen", "sz");
        Assert.assertTrue(add>0);
    }
}

项目地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值