歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

MyBatis入門示例

說明:

1,本文采用的Mybatis框架版本為: mybatis-3.0.4.jar

2, 以持久化一個簡單的User bean為例

User實體類的代碼如下:

  1. package bean;
  2.  
  3. publicclass User {
  4.     private String name;
  5.     private Integer age;
  6.    
  7.     public String getName() {
  8.       return name;
  9.     }
  10.     publicvoid setName(String name) {
  11.       this.name = name;
  12.     }
  13.     public Integer getAge() {
  14.       return age;
  15.     }
  16.     publicvoid setAge(Integer age) {
  17.       this.age = age;
  18.     }
  19.     public User(String name, Integer age) {
  20.       super();
  21.       this.name = name;
  22.       this.age = age;
  23.     }
  24.    
  25.     public User() {
  26.       super();
  27.     }  //必須要有這個無參構造方法,不然根據UserMapper.xml中的配置,在查詢數據庫時,將不能反射構造出User實例
  28. }

User實體的映射器取名為UserMapper(接口),其代碼如下

  1. package Mapper;
  2. import bean.User;
  3. publicinterface UserMapper {
  4. publicvoid insertUser(User user);
  5. public User getUser(String name);
  6. }

注意,該接口類似於Hibernate中的DAO層接口,唯一不同的是,MyBatis中,只聲明該接口就可以,不需要去實現.

UserMapper映射器對應的配置文件如下(相當於Hibernate中的User.hbm.xml,當然這裡面不是屬性映射字段,而是直接是映射器中各種方法對應的sql語句):

  1. <?xmlversion="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. <mappernamespace="Mapper.UserMapper">
  6. <!-- 這裡namespace必須是UserMapper接口的路徑,不然要運行的時候要報錯 “is not known to the MapperRegistry”-->
  7. <insertid="insertUser"parameterType="User">
  8. insert into vincent_user(name,age) values(#{name},#{age})
  9. <!-- 這裡sql結尾不能加分號,否則報“ORA-00911”的錯誤 -->
  10. </insert>
  11. <!-- 這裡的id必須和UserMapper接口中的接口方法名相同,不然運行的時候也要報錯 -->
  12. <selectid="getUser"resultType="User"parameterType="java.lang.String">
  13. select * from vincent_user where name=#{name}
  14. </select>
  15. </mapper>
 

MyBatis的配置文件這裡取名為mybatis-config.xml其配置代碼如下(相當與Hibernate的hibernate.cfg.xml):

  1. <?xmlversion="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. <settings>
  7. <!-- changes from the defaults for testing -->
  8. <settingname="cacheEnabled"value="false"/>
  9. <settingname="useGeneratedKeys"value="true"/>
  10. <settingname="defaultExecutorType"value="REUSE"/>
  11. </settings>
  12. <typeAliases>
  13. <typeAliasalias="User"type="bean.User"/>
  14. </typeAliases>
  15. <environmentsdefault="development">
  16. <environmentid="development">
  17. <transactionManagertype="jdbc"/>
  18. <dataSourcetype="POOLED">
  19. <propertyname="driver"value="Oracle.jdbc.driver.OracleDriver"/>
  20. <propertyname="url"value="jdbc:oracle:thin:@192.168.56.201:1521:system"/>
  21. <propertyname="username"value="vincent"/>
  22. <propertyname="password"value="1234"/>
  23. </dataSource>
  24. </environment>
  25. </environments>
  26. <mappers>
  27. <mapperresource="resource/UserMapper.xml"/>
  28. </mappers>
  29. </configuration>

加載配置文件得到sqlSessionFactory的工具類

  1. package Util;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. publicclass MyBatisUtil {
  8. privatefinalstatic SqlSessionFactory sqlSessionFactory;
  9. static {
  10. String resource = "resource/mybatis-config.xml";
  11. Reader reader = null;
  12. try {
  13. reader = Resources.getResourceAsReader(resource);
  14. } catch (IOException e) {
  15. System.out.println(e.getMessage());
  16. }
  17. sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  18. }
  19. publicstatic SqlSessionFactory getSqlSessionFactory() {
  20. return sqlSessionFactory;
  21. }
  22. }

測試代碼如下:

  1. package TestMapper;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.junit.Test;
  5. publicclass TestMapper {
  6. static SqlSessionFactory sqlSessionFactory = null;
  7. static {
  8. sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
  9. }
  10. @Test
  11. publicvoid testAdd() {
  12. SqlSession sqlSession = sqlSessionFactory.openSession();
  13. try {
  14. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  15. User user = new User("tom",new Integer(5));
  16. userMapper.insertUser(user);
  17. sqlSession.commit();//這裡一定要提交,不然數據進不去數據庫中
  18. } finally {
  19. sqlSession.close();
  20. }
  21. }
  22. @Test
  23. publicvoid getUser() {
  24. SqlSession sqlSession = sqlSessionFactory.openSession();
  25. try {
  26. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  27. User user = userMapper.getUser("jun");
  28. System.out.println("name: "+user.getName()+"|age: "+user.getAge());
  29. } finally {
  30. sqlSession.close();
  31. }
  32. }
  33. }

MyBatis 的詳細介紹:請點這裡
MyBatis 的下載地址:請點這裡

Java實戰應用:Mybatis實現單表的增刪改 http://www.linuxidc.com/Linux/2014-06/103456.htm

[Java][Mybatis]物理分頁實現 http://www.linuxidc.com/Linux/2014-04/99889.htm

Mybatis快速入門教程 http://www.linuxidc.com/Linux/2013-06/85762.htm

Mybatis的關於批量數據操作的測試 http://www.linuxidc.com/Linux/2012-05/60863.htm

Mybatis中對List<Object> 對象List的批處理插入操作 http://www.linuxidc.com/Linux/2014-02/96916.htm

Copyright © Linux教程網 All Rights Reserved