Mybatis妗嗘灦 鍏ラ棬瀛︿範

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis妗嗘灦 鍏ラ棬瀛︿範相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%85%b3%e6%b3%a8' title='鍏虫敞'>鍏虫敞   -o   springmvc   ref   ODB   gets   class   

鐩綍

Mybatis妗嗘灦绠€浠?/a>

妗嗘灦浠嬬粛

鍘熷JDBC鎿嶄綔鏌ヨ

鍘熷JDBC鎿嶄綔闂鍒嗘瀽

Mybatis妗嗘灦浠嬬粛

Mybatis 鏋舵瀯

Mybatis蹇€熷叆闂?/a>

寮€鍙戞楠?/a>

鐜鎼缓

鐭ヨ瘑灏忕粨


Mybatis妗嗘灦绠€浠?/h1>

妗嗘灦浠嬬粛

浠€涔堟槸妗嗘灦锛屼负浠€涔堜娇?妗嗘灦锛屾鏋朵紭鐐?nbsp;?

妗嗘灦锛?nbsp;framework 锛夋槸?涓熀鏈蹇典笂鐨勭粨鏋勶紝?浜庡幓瑙e喅鎴栬€呭鐞嗗鏉傜殑闂銆?/p>

妗嗘灦锛屽嵆framework銆傚叾瀹炲氨鏄煇绉嶅簲?鐨勫崐鎴愬搧锛屽氨鏄?缁勭粍浠讹紝渚涗綘閫?瀹屾垚浣??鐨勭郴缁熴€傜畝鍗曡灏辨槸浣?鍒?鎼ソ鐨勮垶鍙帮紝浣犳潵鍋氳〃婕斻€?妗嗘灦鏄湪鐗瑰畾鐨勯鍩熷唴瑙e喅闂銆?/p>

 

浼樼偣 :

    1). 閲?浠g爜??澧炲姞锛岃蒋浠?浜ф晥鐜囧拰璐ㄩ噺涔熷緱鍒颁簡鎻?銆?/p>

    2). 浣?妗嗘灦寮€鍙戯紝瀹冩彁渚涚粺?鐨勬爣鍑嗭紝??闄嶄綆浜嗘垜浠殑鍚庢湡缁存姢銆?/p>

 

java 寮€鍙戜腑甯?妗嗘灦 :

    1). springmvc spring mybatis(ibatis) -- SSM

    2). SpringData SpringBoot SpringCloud

 

SSM妗嗘灦鍦↗avaEE寮€鍙戜腑鎵€澶勭殑浣嶇疆锛?/p>

 

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=?

鍘熷JDBC鎿嶄綔鏌ヨ

 
鎶€鏈浘鐗? src=
  1. @Test

  2. public void testQuery(){

  3. Connection connection = null;

  4. PreparedStatement preparedStatement = null;

  5. ResultSet resultSet = null;

  6. try {

  7. //鍔犺浇鏁版嵁搴撻┍鍔?/code>

  8. Class.forName("com.mysql.cj.jdbc.Driver");

  9. //閫氳繃椹卞姩绠$悊绫昏幏鍙栨暟鎹簱閾炬帴

  10. connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/test_wensong","root", "root");

  11. //瀹氫箟sql璇彞 ? 琛ㄧず鍗犱綅绗?/code>

  12. String sql = " select * from user where username = ?";

  13. //鑾峰彇棰勫鐞唖tatement

  14. preparedStatement = connection.prepareStatement(sql);

  15. //璁剧疆鍙傛暟锛岀?涓弬鏁颁负sql璇彞涓弬鏁扮殑搴忓彿锛堜粠1寮€濮嬶級锛岀?涓弬鏁颁负璁剧疆鐨勫弬鏁板€?/code>

  16. preparedStatement.setString(1, "Tom");

  17. //鍚戞暟鎹簱鍙戝嚭sql鎵?鏌ヨ锛屾煡璇㈠嚭缁撴灉闆?/code>

  18. resultSet = preparedStatement.executeQuery();

  19. List<User> userList = new ArrayList<User>();

  20. //閬嶅巻鏌ヨ缁撴灉闆?/code>

  21. while(resultSet.next()){

  22. User user = new User();

  23. user.setId(resultSet.getInteger("id"));

  24. user.setUsername(resultSet.getString("username"))

  25. user.setSex(resultSet.getString("sex"));

  26. user.setAddress(resultSet.getString("address"));

  27. userList.add(user);

  28. }

  29. } catch (Exception e) {

  30. e.printStackTrace();

  31. }finally{

  32. //閲婃斁璧勬簮

  33. if(resultSet!=null){

  34. try {

  35. resultSet.close();

  36. } catch (SQLException e) {

  37. e.printStackTrace();

  38. }

  39. }

  40. if(preparedStatement!=null){

  41. try {

  42. preparedStatement.close();

  43. } catch (SQLException e) {

  44. e.printStackTrace();

  45. }

  46. }

  47. if(connection!=null){

  48. try {

  49. connection.close();

  50. } catch (SQLException e) {

  51. e.printStackTrace();

  52. }

  53. }

  54. }

  55. }

鍘熷JDBC鎿嶄綔闂鍒嗘瀽

鍘熷 jdbc 寮€鍙戝瓨鍦ㄧ殑闂濡備笅锛?/p>

鈶?nbsp;. 鏁版嵁搴撹繛鎺ュ垱寤恒€侀噴鏀鹃绻侀€犳垚绯荤粺璧勬簮娴垂浠?褰卞搷绯荤粺鎬ц兘 銆?/p>

鈶?nbsp;. sql 璇彞鍦ㄤ唬鐮佷腑纭紪鐮侊紝閫犳垚浠g爜涓嶆槗缁存姢锛屽疄闄呭簲? sql 鍙樺寲鐨勫彲鑳借緝?锛?nbsp;sql 鍙樺姩闇€瑕佹敼鍙?java 浠g爜 銆?/p>

鈶?nbsp;. 鏌ヨ鎿嶄綔鏃堕渶瑕?鍔ㄥ皢缁撴灉闆嗕腑鐨勬暟鎹?鍔ㄥ皝瑁呭埌瀹炰綋涓€傛彃?鎿嶄綔鏃堕渶瑕?鍔ㄥ皢瀹炰綋鐨勬暟鎹缃埌 sql 璇彞鐨勫崰浣嶇浣嶇疆 銆?/p>

 

搴斿涓婅堪闂缁欏嚭鐨勮В鍐?妗堬細

鈶?nbsp;. 浣?鏁版嵁搴撹繛鎺ユ睜鍒濆鍖栬繛鎺ヨ祫婧?銆?/p>

鈶?nbsp;. 灏?nbsp;sql 璇彞鎶藉彇鍒?nbsp;xml 閰嶇疆?浠朵腑 銆?/p>

鈶?nbsp;. 浣?鍙嶅皠绛夊簳灞傛妧鏈紝?鍔ㄥ皢瀹炰綋涓庤〃杩?灞炴€т笌瀛楁鐨?鍔ㄦ槧灏?銆?/p>

 

Mybatis妗嗘灦浠嬬粛

mybatis 鏄?涓紭绉€鐨勫熀浜?nbsp;java 鐨勬寔涔呭眰妗嗘灦锛屽畠鍐呴儴灏佽浜?nbsp;jdbc 锛屼娇寮€鍙戣€呭彧闇€瑕佸叧娉?nbsp;sql璇彞鏈韩锛?涓嶉渶瑕佽姳璐圭簿?鍘诲鐞嗗姞杞介┍鍔ㄣ€佸垱寤鸿繛鎺ャ€佸垱寤?nbsp;statement 绛夌箒鏉傜殑杩囩▼ 銆?/p>

mybatis 閫氳繃 xml 鎴栨敞瑙g殑?寮忓皢瑕佹墽?鐨勫悇绉?nbsp;statement 閰嶇疆璧锋潵锛屽苟閫氳繃 java 瀵硅薄鍜?nbsp;statement 涓璼ql 鐨勫姩鎬佸弬鏁拌繘?鏄犲皠?鎴愭渶缁堟墽?鐨?nbsp;sql 璇彞 銆?/p>

鏈€鍚?nbsp;mybatis 妗嗘灦鎵? sql 骞跺皢缁撴灉鏄犲皠涓?nbsp;java 瀵硅薄骞惰繑鍥炪€傞噰? ORM鎬濇兂瑙e喅浜嗗疄浣撳拰鏁版嵁搴撴槧灏勭殑闂锛屽 jdbc 杩?浜嗗皝瑁咃紝灞忚斀浜?nbsp;jdbc api 搴曞眰璁块棶缁嗚妭锛屼娇鎴戜滑涓?涓?nbsp;jdbc api 鎵撲氦閬擄紝灏卞彲浠ュ畬鎴愬鏁版嵁搴撶殑鎸佷箙鍖栨搷浣?銆?/p>

MyBatis 瀹?鍦板潃锛?nbsp;https://mybatis.org/mybatis-3/

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=?

Mybatis 鏋舵瀯

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=?

1). mybatis 閰嶇疆?浠?/p>

SqlMapConfig.xml 锛屾?浠朵綔涓?nbsp;mybatis 鐨勫叏灞€閰嶇疆?浠讹紝閰嶇疆浜?nbsp;mybatis鐨勮繍?鐜绛変俊鎭€俶apper.xml ?浠跺嵆 sql 鏄犲皠?浠讹紝?浠朵腑閰嶇疆浜嗘搷浣滄暟鎹簱鐨?nbsp;sql璇彞銆傛?浠堕渶瑕佸湪SqlMapConfig.xml 涓姞杞姐€?/p>

2). 閫氳繃 mybatis 鐜绛夐厤缃俊鎭瀯閫?nbsp;SqlSessionFactory 鍗充細璇??

3). 鐢变細璇??鍒涘缓 sqlSession 鍗充細璇濓紝鎿嶄綔鏁版嵁搴撻渶瑕侀€氳繃 sqlSession 杩?銆?/p>

4). mybatis 搴曞眰?瀹氫箟浜?nbsp;Executor 鎵?鍣ㄦ帴?鎿嶄綔鏁版嵁搴擄紝 Executor鎺?鏈変袱涓疄鐜帮紝?涓槸鍩烘湰鎵?鍣?nbsp;BaseExecutor 銆?涓槸缂撳瓨鎵?鍣?nbsp;CachingExecutor 銆?/p>

5). MappedStatement 涔熸槸 mybatis ?涓簳灞傚皝瑁呭璞★紝瀹冨寘瑁呬簡 mybatis 閰嶇疆淇℃伅鍙?nbsp;sql鏄犲皠淇℃伅绛夈€俶apper.xml ?浠朵腑?涓?nbsp;sql 瀵瑰簲?涓?nbsp;MappedStatement 瀵硅薄锛?nbsp;sql 鐨?nbsp;id 鍗虫槸 Mapped statement 鐨?nbsp;id 銆?/p>

6). MappedStatement 瀵?nbsp;sql 鎵?杈?鍙傛暟杩?瀹氫箟锛屽寘鎷?nbsp;HashMap 銆佸熀鏈被鍨嬨€?nbsp;pojo 锛?nbsp;Executor閫氳繃Mapped Statement 鍦ㄦ墽? sql 鍓嶅皢杈?鐨?nbsp;java 瀵硅薄鏄犲皠? sql 涓紝杈?鍙傛暟鏄犲皠灏辨槸 jdbc缂栫▼涓 preparedStatement 璁剧疆鍙傛暟銆?/p>

7). MappedStatement 瀵?nbsp;sql 鎵?杈撳嚭缁撴灉杩?瀹氫箟锛屽寘鎷?nbsp;HashMap 銆佸熀鏈被鍨嬨€?nbsp;pojo 锛?nbsp;Executor閫氳繃Mapped Statement 鍦ㄦ墽? sql 鍚庡皢杈撳嚭缁撴灉鏄犲皠? java 瀵硅薄涓紝杈撳嚭缁撴灉鏄犲皠杩囩▼鐩稿綋浜?nbsp;jdbc缂栫▼涓缁撴灉鐨勮В鏋愬鐞嗚繃绋嬨€?/p>

 

Mybatis蹇€熷叆闂?/h2>

寮€鍙戞楠?/h3>

鈶?nbsp;. 娣诲姞 MyBatis 鐨勫潗鏍?/p>

鈶?nbsp;. 鍑嗗鏁版嵁搴撶幆澧?/p>

鈶?nbsp;. 缂栧啓 User 瀹炰綋绫?/p>

鈶?nbsp;. 缂栧啓鏍??浠?nbsp;SqlMapConfig.xml

鈶?nbsp;. 缂栧啓鏄犲皠?浠?nbsp;UserMapper.xml

鈶?nbsp;. 缂栧啓娴嬭瘯绫?/p>

 

鐜鎼缓

瀵?渚濊禆

 
鎶€鏈浘鐗? src=
  1. <!--mybatis鍧愭爣-->

  2. <dependency>

  3. <groupId>org.mybatis</groupId>

  4. <artifactId>mybatis</artifactId>

  5. <version>3.4.5</version>

  6. </dependency>

  7. <!--mysql椹卞姩鍧愭爣-->

  8. <dependency>

  9. <groupId>mysql</groupId>

  10. <artifactId>mysql-connector-java</artifactId>

  11. <version>8.0.16</version>

  12. </dependency>

  13. <!--鍗曞厓娴嬭瘯鍧愭爣-->

  14. <dependency>

  15. <groupId>junit</groupId>

  16. <artifactId>junit</artifactId>

  17. <version>4.12</version>

  18. </dependency>

  19. <!--lombok鍧愭爣-->

  20. <dependency>

  21. <groupId>org.projectlombok</groupId>

  22. <artifactId>lombok</artifactId>

  23. <version>1.16.20</version>

  24. </dependency>

  25. <!--?蹇楀潗鏍?->

  26. <dependency>

  27. <groupId>log4j</groupId>

  28. <artifactId>log4j</artifactId>

  29. <version>1.2.17</version>

  30. </dependency>

2鍑嗗鏁版嵁搴撶幆澧?/p>

 
鎶€鏈浘鐗? src=
  1. create database mybatis;

  2. use mybatis;

  3. CREATE TABLE `user` (

  4. `id` int(11) NOT NULL AUTO_INCREMENT,

  5. `username` varchar(32) NOT NULL COMMENT 鈥?鎴峰悕绉扳€?

  6. `sex` char(1) DEFAULT NULL COMMENT 鈥樻€у埆鈥?

  7. `address` varchar(256) DEFAULT NULL COMMENT 鈥樺湴鍧€鈥?

  8. PRIMARY KEY (`id`)

  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  10. insert into `user` (`id`, `username`, `sex`, `address`)

  11. values(NULL,鈥楾om鈥?鈥樼敺鈥?鈥樺寳浜€?;

  12. insert into `user` (`id`, `username`, `sex`, `address`)

  13. values(NULL,鈥楯ack鈥?鈥樼敺鈥?鈥樹笂娴封€?;

  14. insert into `user` (`id`, `username`, `sex`, `address`)

  15. values(NULL,鈥楻ose鈥?鈥?鈥?鈥?瀹夆€?;

  16. insert into `user` (`id`, `username`, `sex`, `address`)

  17. values(NULL,鈥楶oll鈥?鈥?鈥?鈥樺埄?浜氣€?;

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=?

 

3缂栧啓 User 瀹炰綋

 

 
鎶€鏈浘鐗? src=
  1. @Data

  2. public class User {

  3. private Integer id;

  4. private String username;

  5. private String sex;

  6. private String address; }

缂栧啓 MyBatis鏍稿績鏂囦欢

?浠跺悕 : SqlMapConfig.xml

 
鎶€鏈浘鐗? src=
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE configuration

  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">

  5. <configuration>

  6. <!--閰嶇疆鏁版嵁搴撹繍?鐜-->

  7. <environments default="development">

  8. <environment id="development">

  9. <transactionManager type="JDBC"/>

  10. <dataSource type="POOLED">

  11. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>

  12. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test_wensong"/>

  13. <property name="username" value="root"/>

  14. <property name="password" value="root"/>

  15. </dataSource>

  16. </environment>

  17. </environments>

  18. <!--閰嶇疆鍏宠仈鐨凷QL璇彞鐨勬槧灏?浠?->

  19. <mappers>

  20. <mapper resource="UserMapper.xml"/>

  21. </mappers>

  22. </configuration>

缂栧啓 UserMapper 鏄犲皠?浠?/p>

?浠跺悕 : UserMapper.xml

 
鎶€鏈浘鐗? src=
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE mapper

  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  5. <mapper namespace="userMapper">

  6. <!--鏌ヨ鎵€鏈?鎴?->

  7. <select id="findAll" resultType="com.wensong.pojo.User">

  8. select * from user

  9. </select>

  10. </mapper>

鏌ヨ鎵€鏈夌敤鎴?/p>

 
鎶€鏈浘鐗? src=
  1. public class UserTest {

  2. @Test

  3. public void findAllTest() throws IOException {

  4. //鍔犺浇鏍?閰嶇疆?浠?SqlMapConfig.xml

  5. InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");

  6. //鏋勯€燬QLSessionFactory瀵硅薄

  7. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

  8. //鏋勯€燬QLSession瀵硅薄

  9. SqlSession sqlSession = sqlSessionFactory.openSession();

  10. //鎵?鏌ヨ璇彞

  11. List<User> userList = sqlSession.selectList("userMapper.findAll");

  12. for (User user : userList) {

  13. System.out.println("user = " + user);

  14. }

  15. //閲婃斁璧勬簮

  16. sqlSession.close();

  17. }

  18. }

鏌ヨ缁撴灉鏄剧ず锛?/h3>

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=?

鐭ヨ瘑灏忕粨

 
鎶€鏈浘鐗? src=
  1. 1). 鎼缓鐜, 瀵?渚濊禆

  2. mybatis , mysql鏁版嵁搴撻┍鍔? junit , lombok

  3. 2). 鍑嗗鏁版嵁搴撶幆澧?/code>

  4. 3). 缂栧啓瀹炰綋绫籙ser

  5. 4). 缂栧啓鏍?閰嶇疆?浠?/code>

  6. A. mybatis鐨勮繍?鐜 -----> <environments>

  7. B. 鍔犺浇鏄犲皠閰嶇疆?浠?-------> <mappers>

  8. 5). 缂栧啓鏄犲皠閰嶇疆?浠?/code>

  9. SQL璇彞

  10. 6). 缂栧啓娴嬭瘯浠g爜

  11. A. 鍔犺浇Mybatis鐨勬牳?閰嶇疆?浠?/code>

  12. B. 鏋勯€燬qlSessionFactory

  13. C. 鏋勯€燬qlSession

  14. D. 璋?SqlSession涓殑selectList鎵?鏌ヨ

  15. E. 閲婃斁璧勬簮

以上是关于Mybatis妗嗘灦 鍏ラ棬瀛︿範的主要内容,如果未能解决你的问题,请参考以下文章

mybatis鍏ラ棬

鍏ラ棬娣卞害瀛︿範锛岀悊瑙g缁忕綉缁溿€佸弽鍚戜紶鎾畻娉曟槸绗竴鍏?/h1>

webpack鍏ラ棬-涓汉瀛︿範璧勬簮鏀堕泦

Java鍏ラ棬(鍩虹绡囦竴)

鍊嶅鍏ラ棬

JavaPoet鍏ラ棬