目錄java
此文已獨家受權給【新華先後端開發】使用。其餘平臺使用聯繫做者後再使用git
在數據庫方面咱們最經常使用的應該JDBC、Hibernate和Mybatis。經過JDBC方式鏈接數據庫,咱們會發現工做量是至關的複雜。咱們得處理一些瑣碎的關閉。而後入參出參咱們都得本身管理。基於次產生了ORM(Object Relational Mapping)模型。github
Mybatis的前身嚴格意義上說應該是Ibatis,後面咱們都稱之爲Mybatis.爲了解決Hibernate的不足,Mybatis產生了相對於Hibernate的全表映射Mybatis能夠說是半自動映射的框架。由於他是實體和sql結合的一個框架。sql
Mybatis有SQL , 實體 , 映射規則三個主要對象主成。和Hibernate相比雖然多出了sql的編寫,可是正是由於sql的編寫使得Mybatis變得很方便。Hibernate由於不用sql,因此他沒法調用存儲過程這些數據庫方法。可是Mybatis不同,Mybatis能夠調用sql中的存儲過程。數據庫
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency>
package com.github.zxhtom.mapper; import com.github.zxhtom.model.Student; import java.util.List; public interface StudentMapper { /** * 獲取學生列表 * @return */ public List<Student> getStudents(); /** * 經過id獲取學生信息 * @param id * @return */ public Student getStudentByStuId(String id); }
<?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" > <mapper namespace="com.github.zxhtom.mapper.StudentMapper"> <select id="getStudents" resultType="com.github.zxhtom.model.Student"> select * from student </select> <select id="getStudentByStuId" resultType="com.github.zxhtom.model.Student"> select * from student where id=#{id} </select> </mapper>
<?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> <!--引入外部配置文件--> <properties resource="config.properties"></properties> <!--定義別名--> <typeAliases> <package name=""/> </typeAliases> <!--定義數據庫信息,默認使用development數據庫構建環境--> <environments default="development"> <environment id="development"> <!--jdbc事物管理--> <transactionManager type="JDBC"></transactionManager> <!--配置數據庫鏈接信息--> <dataSource type="POOLED"> <property name="driver" value="${database.driver}"/> <property name="url" value="${database.url}"/> <property name="username" value="${database.username}"/> <property name="password" value="${database.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/github/zxhtom/mapper/StudentMapper.xml"></mapper> </mappers> </configuration>
//獲取mybatis-config.xml位置 InputStream inputStream = Resources.getResourceAsStream(Constant.MYBATIS); //加載mybatis-config,並建立sqlsessionfactory對象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //建立sqlsession對象 SqlSession sqlSession = sqlSessionFactory.openSession(); Map<String, Object> paramMap = new HashMap<>(); paramMap.put("id", 1); //執行select語句,將resultSet映射成對象並返回 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = studentMapper.getStudents(); studentPrint(students);
Map<Object, Object> properties = PropertiesUtil.getProperties("config.properties"); PooledDataSource dataSource = new PooledDataSource(); dataSource.setDriver(properties.get("database.driver").toString()); dataSource.setUrl(properties.get("database.url").toString()); dataSource.setUsername(properties.get("database.username").toString()); dataSource.setPassword(properties.get("database.password").toString()); //構建數據庫事物方式 TransactionFactory transactionFactory = new JdbcTransactionFactory(); //建立數據庫運行環境 Environment environment = new Environment("development",transactionFactory,dataSource); //構建Configure對象 Configuration configuration = new Configuration(environment); //註冊別名 configuration.getTypeAliasRegistry().registerAlias("stu", Student.class); //加入一個映射器 configuration.addMapper(StudentMapper.class); //使用sqlsessionfactoryBuilder構建sqlsessionfactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = studentMapper.getStudents(); studentPrint(students);
上面我強調了咱們的StudentMapper.xml和StudentMapper.java要同名並且在同一位置上。這是爲何呢。由於在Mybatis源碼中默認會讀取Java對象同目錄下的同名的xml文件做爲sql配置文件。後端
可是後期咱們項目的展開Java和xml會愈來愈多,放在一塊兒的話很亂。做爲一個框架不會這麼侷限的。在xml配置環境方式中咱們是經過Mappers-->Mapper標籤來注入咱們的Mapper.xml的。這裏的xml路徑並無要求在同一個目錄。咱們這裏能夠隨意的改。上面的那些限制是在Java代碼搭建環境的方式中。由於在Java代碼方式中咱們只是經過java對象註冊mapper接口的。在這種方式下默認就會加載同目錄下的同名xml
微信
學習的過程就是認知的過程。接下來咱們來認識下Mybnatis的核心組件吧。session
SQL Mapper : Mybatis獨有的組件。有Java接口和xml文件構成的一個總體。負責將xml中的sql進行解析而後傳遞給數據庫並獲取結果mybatis
resultMap
來實現數據庫字段和實體字段的映射。這就是咱們所謂的半自動映射加入戰隊app