码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-16 15:03:59      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:ace   tty   oct   interface   org   --   pid   art   utf-8   

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>

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

标签:ace   tty   oct   interface   org   --   pid   art   utf-8   

原文地址:https://www.cnblogs.com/zhuxiang1633/p/11685457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!