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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM