转自:
springboot如何集成mybatis呢?
下文笔者讲述SpringBoot集成MyBatis的方法分享,如下所示
实现思路:引入mybatis-spring-boot-starter即可在SpringBoot中使用MyBatis
例
pom.xml配置
org.springframework.boot spring-boot-dependencies 2.3.0.RELEASE import pom org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.projectlombok lombok org.slf4j slf4j-api org.springframework.boot spring-boot-starter-test mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 com.github.pagehelper pagehelper-spring-boot-starter 1.2.5 src/main/resources **/*.properties **/*.xml **/*.yml true
application.yml配置
mybatis的xml文件我是放在了resources下mybatis文件夹中
#mybatis mybatis:mapper-locations: classpath*:mybatis/*.xmltype-aliases-package: com.java265.pojoconfiguration:map-underscore-to-camel-case: true #showSql logging:level:com.java265.mapper: debug #pagehelper pagehelper:helper-dialect: mysqlreasonable: truesupport-methods-arguments: trueparams: countSql
查询示例分享
@Data
@NoArgsConstructor
@AllArgsConstructorpublic class User implements Serializable {private Integer id;private String lastName;private String email;private String gender;private Integer age;
}
@Repository
public interface UserMapper {List batchFind(List ids);List list();
}
@Service
public class UserService {@AutowiredEmployeeMapper userMapper;public List batchFind(List ids){return userMapper.batchFind(ids);}public List list(){return userMapper.list();}
}
mybaits之xml文件配置
测试@Testpublic void test2(){List users = UserService.batchFind(Arrays.asList(1, 3, 4));log.info("结果:[{}]",users);}