扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

springboot鎬庝箞寮曞叆mybatis閰嶇疆鏂囦欢

扬州沐宇科技
2024-01-18 09:16:09
mybatis, springboot

瑕佸湪Spring Boot涓紩鍏yBatis閰嶇疆鏂囦欢锛岄渶瑕佸畬鎴愪互涓嬫楠わ細
1. 鍦╜src/main/resources`鐩綍涓嬪垱寤轰竴涓悕涓篳mybatis-config.xml`鐨勯厤缃枃浠躲€傚湪璇ユ枃浠朵腑锛屽彲浠ラ厤缃甅yBatis鐨勪竴浜涘叏灞€鍙傛暟鍜屾彃浠剁瓑銆?br/>2. 鍦╜application.properties`鎴朻application.yml`涓紝娣诲姞MyBatis鐨勯厤缃」銆傚彲浠ュ弬鑰冧互涓嬬ず渚嬶細

  # MyBatis閰嶇疆鏂囦欢鐨勪綅缃?/p>

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

   

   # MyBatis mapper鎺ュ彛鎵€鍦ㄧ殑鍖呰矾寰?/p>

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

  mybatis:

     config-location: classpath:mybatis-config.xml

     mapper-locations: classpath:mapper/*.xml

鍦ㄤ笂杩伴厤缃腑锛宍mybatis.config-location`鎸囧畾浜哅yBatis鐨勯厤缃枃浠朵綅缃紝`mybatis.mapper-locations`鎸囧畾浜哅apper鎺ュ彛鎵€鍦ㄧ殑XML鏂囦欢璺緞銆?br/>3. 鍦⊿pring Boot鐨勫惎鍔ㄧ被涓婂姞涓奰@MapperScan`娉ㄨВ锛屾寚瀹歁apper鎺ュ彛鎵€鍦ㄧ殑鍖呰矾寰勩€備緥濡傦細

  import org.springframework.boot.SpringApplication;

   import org.springframework.boot.autoconfigure.SpringBootApplication;

   import org.mybatis.spring.annotation.MapperScan;

   

   @SpringBootApplication

   @MapperScan("com.example.mapper")

   public class Application {

       public static void main(String[] args) {

           SpringApplication.run(Application.class, args);

       }

   }

鍦ㄤ笂杩颁唬鐮佷腑锛宍@MapperScan`鎸囧畾浜哅apper鎺ュ彛鎵€鍦ㄧ殑鍖呰矾寰勶紝璁㏒pring Boot鑳藉鑷姩鎵弿骞舵敞鍐岃繖浜汳apper鎺ュ彛銆傚畬鎴愪笂杩版楠ゅ悗锛孧yBatis鐨勯厤缃枃浠跺拰Mapper鎺ュ彛灏嗕細琚纭姞杞藉拰浣跨敤銆?br/>

扫码添加客服微信