序言:
此前,我们主要通过XML来书写SQL和填补对象映射关系。在SpringBoot中我们可以通过注解来快速编写SQL并实现数据访问。(仅需配置:mybatis.configuration.map-underscore-to-camel-case=true)。为了方便大家,本案例提供较完整的层次逻辑SpringBoot+MyBatis+Annotation。
具体步骤
1. 引入依赖
在pom.xml 引入ORM框架(Mybaits-Starter)和数据库驱动(MySQL-Conn)的依赖。
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.hehe.mapper") //扫描Mapper接口public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); }}6. 单元测试
import static org.assertj.core.api.Assertions.assertThat;@RunWith(SpringRunner.class)@SpringBootTestpublic class MybatisApplicationTest { @SuppressWarnings("all") @Autowired UserMapper userMapper; @Test public void test_db() { //开始进行测试 assertThat(userMapper.list().size()).isGreaterThan(1); assertThat(userMapper.getOne("1")).isNotEqualTo(null); assertThat(userMapper.getOne("xxx")).isEqualTo(null); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。