根據配置信息(eg:mybatis-config.xml)或者代碼來生成SqlSessionFactory。java
依靠工廠來生成SqlSessionmysql
線程不安全sql
獲取映射器,讓映射器經過命名空間和方法名稱找到對應SQL,發送給 數據庫執行後返回結果。數據庫
直接經過命名信息去執行SQL返回結果(iBatis版本留下的方式)apache
MyBatis新組件,一般由一個Java Interface和XML文件構成,需給出對應的SQL和映射規則。緩存
負責發送SQL去執行,並返回接口。安全
-srcsession
-javamybatis
-SQLDaoapp
-MessageModel<POJO類>
-SQLHandle<Mapper接口>
-TestSQL<運行Test>
-resource
-message.xml<MapperXML文件 >
-mybatis-configure.xml<配置文件>
mybatis-configure.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> <!-- 實體類別名,對應映射文件中的resultType --> <typeAliases> <typeAlias alias="message" type="SQLDao.MessageModel"/> </typeAliases> <!-- 數據庫鏈接配置,配置鏈接字符串、用戶名、密碼或緩存、懶加載、語句執行超時時間等這裏目前只配置了基本的數據庫鏈接屬性 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/testdb" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 實體與數據庫表映射文件位置,配置後MyBatis纔會找到並加載上 --> <mappers> <mapper resource="message.xml" /> </mappers> </configuration>
MessageModel
package SQLDao; public class MessageModel { public int CarID; public int postionX; public int postionY; public int speed; public int getCarID() { return CarID; } public void setCarID(int carID) { CarID = carID; } public int getPostionX() { return postionX; } public void setPostionX(int postionX) { this.postionX = postionX; } public int getPostionY() { return postionY; } public void setPostionY(int postionY) { this.postionY = postionY; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } }
SQLHandle
package SQLDao; public interface SQLHandle { public int addhistory(MessageModel model); }
TestSQL
package SQLDao; 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 java.io.IOException; import java.io.Reader; public class TestSQL { /** * SqlSessionFactory是用來建立SqlSession的工廠,SqlSession會執行映射的語句,進行事物提交、回滾等。 * * @return */ public static SqlSessionFactory getSqlSessionFactory() { Reader reader = null; SqlSessionFactory sqlSessionFactory = null; try { reader = Resources.getResourceAsReader("mybatis-configure.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } return sqlSessionFactory; } public static void selectUserByID() { // 獲得SqlSession實例,便於執行增刪改查、事務提交回滾等操做。 SqlSession session = getSqlSessionFactory().openSession(); MessageModel messageModel = new MessageModel(); messageModel.setCarID(1); messageModel.setPostionX(2); messageModel.setPostionY(3); messageModel.setSpeed(4); try { SQLHandle handle=session.getMapper(SQLHandle.class); handle.addhistory(messageModel); } finally { session.close(); } } public static void main(String[] args) { selectUserByID(); } }