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