MyBatis鎬庝箞瀹炵幇閰嶇疆鍔犺浇
MyBatis鍙互閫氳繃涓ょ鏂瑰紡鏉ュ姞杞介厤缃細
- XML閰嶇疆鏂囦欢鍔犺浇锛氶€氳繃鍒涘缓涓€涓狹yBatis閰嶇疆鏂囦欢锛堥€氬父鍛藉悕涓簃ybatis-config.xml锛夛紝鐒跺悗浣跨敤
SqlSessionFactoryBuilder
绫荤殑build()
鏂规硶鍔犺浇閰嶇疆鏂囦欢銆傜ず渚嬩唬鐮佸涓嬶細
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- Java浠g爜閰嶇疆鍔犺浇锛氬彲浠ラ€氳繃缂栧啓Java浠g爜鏉ラ厤缃甅yBatis锛岃€屼笉鏄娇鐢╔ML鏂囦欢銆傜ず渚嬩唬鐮佸涓嬶細
DataSource dataSource = ... // 鍒涘缓鏁版嵁婧愬璞?/span>
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(MyMapper.class); // 娣诲姞Mapper鎺ュ彛
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
浠ヤ笂鏄袱绉嶅父鐢ㄧ殑鍔犺浇閰嶇疆鐨勬柟寮忥紝鏍规嵁瀹為檯鎯呭喌閫夋嫨閫傚悎鐨勬柟寮忔潵閰嶇疆鍜屽姞杞組yBatis銆?/p>
相关问答