springboot -- 整合mybatis

一、

1、添加jar包文件

<!--添加Mybatis  -->

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

2、配置yaml文件

mybatis:

  config-location: classpath:/mybatis/mybatis-config.xml

  mapper-locations: classpath:/mybatis/mappers/*.xml

  type-aliases-package: com.tedu.pojo

3、设置mybatis-config.xml

<configuration>
   <settings>
      <!-- 开启驼峰自动映射 -->
      <setting name="mapUnderscoreToCamelCase" value="true" />
   </settings>
</configuration>

4、设置UserMapper.xml

<mapper namespace="com.tedu.mapper.UserMapper">
    
    <select id="findUserList" resultType="User">
        select * from user
    </select>
    
</mapper>

5、编辑UserMapper.java

public interface UserMapper {
   
   List<User> findUserList();
}

6、编辑UserServiceImpl.java

@Service
public class UserServiceImpl implements UserService {
   
   @Autowired
   private UserMapper userMapper;  //spring为其生成代理对象

   @Override
   public List<User> findUserList() {
      
      return userMapper.findUserList();
   }
   
}

5、为mybatis生成代理对象

@SpringBootApplication
@MapperScan("com.tedu.mapper") //为com.tedu.mapper接口创建代理对象
public class Springboot17112MvcApplication {

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

猜你喜欢

转载自blog.csdn.net/qq_24271537/article/details/81433949