天天看点

MyBitis(iBitis)系列随笔之一:MyBitis入门实例 

聽 聽

MyBits鍓嶈韩鏄痠Bitis,鐩稿浜嶩ibernate鑰岃█,瀹冩槸鍗婅嚜鍔ㄥ寲ORM妗嗘灦銆傛湰鐫€鍒嗕韩鍜屽涔犵殑鐩殑锛岀瑪鑰呭皢闄嗙画鎶婂涔犵瑪璁颁笌浠g爜璐村嚭锛屽笇鏈涘鎯冲涔爉ybitis鐨勫悓绐椾滑鏈夋墍甯姪銆?聽聽聽聽 聽 閴翠簬鎴戠殑娴嬭瘯鏁版嵁搴撴槸mysql锛屽悓浠佷滑鐜板湪鎶妋ysql瑁呭ソ锛岀劧鍚庡噯澶囧ソmysql椹卞姩浠ュ強浠庡畼缃戜笂涓嬭浇mybitis(http://code.google.com/p/mybatis/)銆傛垜涓嬭浇鐨勬槸鏈€鏂扮殑mybatis-3.2.2.zip銆?

聽聽聽

聽聽聽聽聽聽鍒涘缓鏁版嵁搴搕est

聽聽聽聽聽聽聽鐒跺悗鍒涘缓琛╰_user

[sql]聽 view plain 聽copy 聽

  1. DROP聽TABLE聽IF聽EXISTS聽`t_user`;聽聽
  2. CREATE聽TABLE聽`t_user`聽(聽聽
  3. 聽聽`id`聽int(11)聽NOT聽NULL聽AUTO_INCREMENT,聽聽
  4. 聽聽`name`聽varchar(20)聽DEFAULT聽NULL,聽聽
  5. 聽聽`pass`聽varchar(20)聽DEFAULT聽NULL,聽聽
  6. 聽聽PRIMARY聽KEY聽(`id`)聽聽
  7. )聽ENGINE=InnoDB聽AUTO_INCREMENT=24聽DEFAULT聽CHARSET=gbk;聽聽
  8. 聽聽
  9. INSERT聽INTO聽`t_user`聽VALUES聽('1',聽'ssssssssssssssss',聽'ddsssssssdd');聽聽

聽聽聽聽聽鐒跺悗鍒涘缓涓庤〃瀵瑰簲鐨凧ava bean瀵硅薄

[java]聽 view plain 聽copy 聽

  1. <span聽style="color:#000000;">package聽com.jefry;聽聽
  2. 聽聽
  3. public聽class聽User聽{聽聽
  4. 聽聽聽聽private聽int聽id;聽聽
  5. 聽聽聽聽private聽String聽name;聽聽
  6. 聽聽聽聽private聽String聽pass;聽聽
  7. 聽聽聽聽聽聽
  8. 聽聽聽聽聽聽
  9. 聽聽
  10. 聽聽聽聽public聽String聽getPass()聽{聽聽
  11. 聽聽聽聽聽聽聽聽return聽pass;聽聽
  12. 聽聽聽聽}聽聽
  13. 聽聽
  14. 聽聽聽聽public聽void聽setPass(String聽pass)聽{聽聽
  15. 聽聽聽聽聽聽聽聽this.pass聽=聽pass;聽聽
  16. 聽聽聽聽}聽聽
  17. 聽聽
  18. 聽聽聽聽public聽int聽getId()聽{聽聽
  19. 聽聽聽聽聽聽聽聽return聽id;聽聽
  20. 聽聽聽聽}聽聽
  21. 聽聽
  22. 聽聽聽聽public聽void聽setId(int聽id)聽{聽聽
  23. 聽聽聽聽聽聽聽聽this.id聽=聽id;聽聽
  24. 聽聽聽聽}聽聽
  25. 聽聽
  26. 聽聽聽聽public聽String聽getName()聽{聽聽
  27. 聽聽聽聽聽聽聽聽return聽name;聽聽
  28. 聽聽聽聽}聽聽
  29. 聽聽
  30. 聽聽聽聽public聽void聽setName(String聽name)聽{聽聽
  31. 聽聽聽聽聽聽聽聽this.name聽=聽name;聽聽
  32. 聽聽聽聽}聽聽
  33. 聽聽
  34. }</span>聽聽

聽聽聽聽聽聽聽鎺ヤ笅鏉ワ紝鍦╟lasspath鐩綍涓嬮潰鍒涘缓涓庨厤缃甿ybatis-config.xml鏂囦欢

聽聽聽聽聽聽 鍐呭濡備笅锛? [html]聽 view plain 聽copy 聽

  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. 聽聽聽聽<environments聽default="development">聽聽
  7. 聽聽聽聽聽聽聽聽<environment聽id="development">聽聽
  8. 聽聽聽聽聽聽聽聽聽聽聽聽<transactionManager聽type="JDBC"/>聽聽
  9. 聽聽聽聽聽聽聽聽聽聽聽聽<dataSource聽type="POOLED">聽聽
  10. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<property聽name="driver"聽value="com.mysql.jdbc.Driver"/>聽聽
  11. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<property聽name="url"聽value="jdbc:mysql://127.0.0.1:3306/test"/>聽聽
  12. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<property聽name="username"聽value="root"/>聽聽
  13. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<property聽name="password"聽value="root"/>聽聽
  14. 聽聽聽聽聽聽聽聽聽聽聽聽</dataSource>聽聽
  15. 聽聽聽聽聽聽聽聽</environment>聽聽
  16. 聽聽聽聽</environments>聽聽
  17. 聽聽聽聽<mappers>聽聽
  18. 聽聽聽聽聽<!--杩欎釜鏂囦欢涓嬮潰灏嗗垱寤?->聽聽
  19. 聽聽聽聽聽<mapper聽resource="com/jefry/UserMapper.xml"/>聽聽
  20. 聽聽聽聽</mappers>聽聽
  21. </configuration>聽聽

聽聽聽聽聽聽聽聽閫氳繃mybatis-config.xml涓?lt;mapper resource="com/jefry/UserMapper.xml"/>閰嶇疆椤癸紝鎴戜滑鍦ㄥ寘com.jefry涓嬮潰鍒涘缓鏂囦欢UserMapper.xml

聽聽聽聽聽聽聽聽 鍐呭濡備笅锛? [html]聽 view plain 聽copy 聽

  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="User">聽聽
  6. 聽聽聽聽<!--resultType聽琛ㄧずcom.jefry.User杩欐牱涓€涓疄浣撳璞÷?->聽聽
  7. 聽聽聽聽<select聽id="selectUser"聽parameterType="int"聽resultType="com.jefry.User">聽聽
  8. 聽聽聽聽聽聽聽聽select聽*聽from聽t_user聽where聽id聽=聽#{id}聽聽
  9. 聽聽聽聽</select>聽聽
  10. </mapper>聽聽

聽聽聽聽聽缂栧啓娴嬭瘯浠g爜

[java]聽 view plain 聽copy 聽

  1. package聽com.jefry;聽聽
  2. 聽聽
  3. import聽java.io.IOException;聽聽
  4. import聽java.io.InputStream;聽聽
  5. 聽聽
  6. import聽org.apache.ibatis.io.Resources;聽聽
  7. import聽org.apache.ibatis.session.SqlSession;聽聽
  8. import聽org.apache.ibatis.session.SqlSessionFactory;聽聽
  9. import聽org.apache.ibatis.session.SqlSessionFactoryBuilder;聽聽
  10. 聽聽
  11. public聽class聽Test聽{聽聽
  12. 聽聽聽聽static聽String聽resource聽=聽"mybatis-config.xml";聽聽
  13. 聽聽
  14. 聽聽聽聽public聽static聽void聽main(String[]聽args)聽throws聽IOException聽{聽聽
  15. 聽聽聽聽聽聽聽聽//聽TODO聽Auto-generated聽method聽stub聽聽
  16. 聽聽聽聽聽聽聽聽InputStream聽inputStream聽=聽Resources.getResourceAsStream(resource);聽聽
  17. 聽聽聽聽聽聽聽聽SqlSessionFactory聽sqlSessionFactory聽=聽new聽SqlSessionFactoryBuilder().build(inputStream);聽聽
  18. 聽聽聽聽聽聽聽聽SqlSession聽session聽=聽sqlSessionFactory.openSession();聽聽
  19. 聽聽聽聽聽聽聽聽try聽{聽聽
  20. 聽聽聽聽聽聽聽聽聽聽聽聽//User.selectUser涓璘ser瀵瑰簲UserMapper.xml鏂囦欢涓爣绛?lt;mapper聽namespace="User">聽namespace鐨勫€悸犅?
  21. 聽聽聽聽聽聽聽聽聽聽聽聽//selectUser聽瀵瑰簲UserMapper.xml鏂囦欢涓爣绛?lt;select聽id="selectUser"聽parameterType="int"聽resultType="com.jefry.User">涓璱d鍊悸犅?
  22. 聽聽聽聽聽聽聽聽聽聽聽聽User聽user聽=聽session.selectOne("User.selectUser",聽1);聽聽
  23. 聽聽聽聽聽聽聽聽聽聽聽聽System.out.println("user.getName()="聽+聽聽user.getName());聽聽
  24. 聽聽聽聽聽聽聽聽}聽finally聽{聽聽
  25. 聽聽聽聽聽聽聽聽聽聽聽聽session.close();聽聽
  26. 聽聽聽聽聽聽聽聽}聽聽
  27. 聽聽聽聽}聽聽
  28. 聽聽
  29. }聽聽

閫氳繃杩愯灏卞彲浠ョ粨鏋滐細user.getName()=ssssssssssssssss