mybatis的第一次簡易實現
阿新 • • 發佈:2018-12-02
實現mybatis的簡易增刪改查的基本步驟記錄
0.專案結構
1.第一步:確保mybatis的jar包都有
從此https://github.com/mybatis/mybatis-3/releases地址可以獲取所需的jar包
基本一個簡易的mybatis需要這個jar包,其中mysql-connector-java-5.137.jar是連線mysql所需
2.構建資料庫表的POJO物件,最終mybatis的查詢都會對映到它上面,活著將其存入資料庫中(資料庫表別忘了建)
package com.mybatis.pojo; public class Role {private Integer id; private String roleName; private String note; /**setter and gettet @Override public String toString() { return "Role [id=" + id + ", roleName=" + roleName + ", note=" + note + "]"; } }
3..構建對映器,採用XML方式構建,包含一個介面和一個XML檔案,把他們都放入到com.mybatis.mapper包裡
介面RoleMapper.java
package com.mybatis.mapper; import com.mybatis.pojo.Role; public interface RoleMapper { public Role getRole(Integer id); }
對應的XML檔案:RoleMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace對應於mapper介面的位置 --> <mapper namespace="com.mybatis.mapper.RoleMapper"> <!-- 對應於增刪改查語句,以select/update/delete/insert開頭 id值對應於介面內的方法名,parameterType值對應於方法的引數型別 resultType對應於方法的返回值型別 --> <select id="getRole" parameterType="Integer" resultType="role"> select * from t_role where id=#{id} </select> </mapper>
4.設定用於連線資料庫的XML檔案mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases><!-- 別名 --> <typeAlias alias="role" type="com.mybatis.pojo.Role"/> </typeAliases> <!-- 資料庫環境 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- type="POOLED"指mybatis內部提供的連線池方式 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 對映檔案 --> <mappers> <mapper resource="com/mybatis/mapper/RoleMapper.xml" /> </mappers> </configuration>
5.建立獲取SqlSeesionFactory和SqlSession的工具類,當然也可以直接放到主函式內
package com.mybatis.utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mybatis.utils.SqlSessionFactoryUtils; public class SqlSessionFactoryUtils { //使用單例模式 private final static Class<SqlSessionFactoryUtils> LOCK=SqlSessionFactoryUtils.class; private static SqlSessionFactory sqlSessionFactory=null; private SqlSessionFactoryUtils(){} //獲取SqlSeesionFactory public static SqlSessionFactory getSqlSessionFactory(){ synchronized(LOCK){ if(sqlSessionFactory !=null){ return sqlSessionFactory; } String resource="mybatis-config.xml"; InputStream inputStream; try{ inputStream=Resources.getResourceAsStream(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); }catch(IOException e){ e.printStackTrace(); return null; } return sqlSessionFactory; } } //獲取SqlSession public static SqlSession openSqlSession(){ if(sqlSessionFactory==null){ getSqlSessionFactory(); } return sqlSessionFactory.openSession(); } }
6.測試類
package com.mybatis.main; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import com.mybatis.mapper.RoleMapper; import com.mybatis.pojo.Role; import com.mybatis.utils.SqlSessionFactoryUtils; public class Main { public static void main(String[] args) { Logger log=Logger.getLogger(Main.class); SqlSession sqlSession=null; try{ sqlSession=SqlSessionFactoryUtils.openSqlSession(); RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class); Role role=roleMapper.getRole(1); log.info(role.getRoleName()); System.out.println(role); }catch(Exception e){ e.printStackTrace(); }finally{ if(sqlSession != null){ sqlSession.close(); } } } }
至此一個簡單的mybatis例項已經執行成功了