springboot鎬庝箞寮曞叆mybatis閰嶇疆鏂囦欢
瑕佸湪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/>
相关问答