根据配置信息(eg:mybatis-config.xml)或者代码来生成SqlSessionFactory。
依靠工厂来生成SqlSession
线程不安全
获取映射器,让映射器通过命名空间和方法名称找到对应SQL,发送给 数据库执行后返回结果。
直接通过命名信息去执行SQL返回结果(iBatis版本留下的方式)
MyBatis新组件,通常由一个Java Interface和XML文件构成,需给出对应的SQL和映射规则。
负责发送SQL去执行,并返回接口。
-src
-java
-SQLDao
-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();
}
}