MyBatis的本质就是对JDBC的一个封装,包括JDBC的事务管理、SQL组装、CRUD操作、结果映射等等。
1. 常规使用代码
整合代码片段MyBatisTest.java(基本上把这一段调试一边之后,对MyBatis的核心代码就有一些了解了)
package i.love.mylife;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.pooled.PooledDataSourceFactory;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import i.love.mylife.vo.Stat;
import i.love.mylife.vo.StatMapper;
public class MyBatisTest {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("driver", "com.mysql.jdbc.Driver");
properties.setProperty("url", "jdbc:mysql://localhost:3306/employees?useSSL=false");
properties.setProperty("username", "root");
properties.setProperty("password", "123456!");
//1.获得数据源,对DriverManaager和Connection对象本身对象做了一些封装,比如连接池。
//池化连接之后,在调用close的时候,通过JDK代理模式,修改了方法的执行逻辑,不适直接关闭Conneciton,而是将Connection放到DataSource的PoolState对象变量中。
PooledDataSourceFactory pooledDataSourceFactory = new PooledDataSourceFactory();
pooledDataSourceFactory.setProperties(properties);
DataSource ds = pooledDataSourceFactory.getDataSource();
//2.事务工厂,获取事务,其实就是获取连接,对Connection的事务相关的功能进行了封装
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, ds);
//3.初始化Configuration
Configuration configuration = new Configuration(environment);
//初始化Mapper接口。并缓存对应的Mapper接口的代理类,要不然你以为,Mapper接口是怎么执行方法的啊。
configuration.addMapper(StatMapper.class);
//4.默认的DefaultSqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
//sqlSession封装了一些调用数据库的方法,组合了一个 Excutor
//Excutor用于真正的去做拿Connection、拿PrepareStatement、参数映射、结果映射。组合了一个Transaction。
//Transaction ,组合了一个Connection
SqlSession session = sqlSessionFactory.openSession();
StatMapper mapper = null;
try {
//1.基于StatementID
//Map<String,Object> params = new HashMap<String,Object>();
//params.put("id", 832);
//sqlSession.selectList("i.love.mylife.vo.StatMapper。getAll",params);
//2.基于Mapper接口
//MapperProxy jdk动态代理
mapper = session.getMapper(StatMapper.class);
List<Stat> stats = mapper.getAll(1);
System.out.println(stats.size());
for (int i = 0; i < stats.size(); i++) {
System.out.println(stats.get(i).getJobName());
}
session.commit();
} finally {
session.close();
}
}
}
StatMapper.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">
<mapper namespace="i.love.mylife.vo.StatMapper">
<select id="getAll" resultType="i.love.mylife.vo.Stat">
select * from Stat where id = #{id};
</select>
</mapper>
StatMapper.java
package i.love.mylife.vo;
import java.util.List;
public interface StatMapper {
public List<Stat> getAll(int id);
}
Stat.java
package i.love.mylife.vo;
import java.util.Date;
public class Stat {
private int id;
private Date time;
private String type;
private String appId;
private String jobName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
}