spring boot项目mybatis配置注解+配置文件

maven依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>
知识兔

设置配置文件、Mapper的路径

# mapper 
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
mybatis.config-location=classpath:/mybatis/config/mybatis-config.xml
知识兔
  • dao
public interface UserMapper {
    List<User> findAll();
}
知识兔
  • 配置dao的位置,生产代理对象
@SpringBootApplication
@MapperScan("com.getword.dao")
知识兔
<?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"><!-- namespace指用户自定义的命名空间。和接口路径一致 --><mapper namespace="com.getword.dao.UserMapper">  <!-- select操作     resultType="map"表示返回的是一个Map对象    使用列名做key,值做value     -->  <select id="findAll" resultType="map">    SELECT * FROM tb_user where id=1  </select></mapper>
计算机