Mybati核心组件:
SqlSessionFactoryBuilder(构造器):它关于根据配置信息或者代码来生成SqlSessionFactory(工厂接口)。
SqlSessionFactory:依靠工厂来生成SqlSession(会话)。
SqlSession:是一个即可以发送SQL去执行并返回结果,也可以获取Mapper的接口。
SQL Mapper:它是Mybatis新设计的组件。它是由Java接口和xml文件(或注解)构成的,需要给出对应的SQL和映射规则。它负责发送SQL去执行,并返回结果。
JDBC编程:
public class JdbcExample {
private Connection getConnection(){
Connection connection=null;
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mybatis?zeroDateTime
Behavior=convertToNull";
String user="root";
String password="root";
//创建连接对象
connection=DriverManager.getConnection(url,user,password);
}catch(ClassNotFoundException | SQLException ex){
ex.printstack();
return null;
}
return connection;
}
public Role getRole(Long id){
Connection connection=getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
try{
//创建执行语句
ps=connection.prepareStatement("select id,role_name,note
from t_role where id=?");
//执行语句注入值
ps.setLong(1,id);
//执行语句产生结果集
rs=ps.executeQuery();
while(rs.next()){
Long roleId=rs.getLong("id");
String userName=rs.getString("role_name");
String note=rs.getString("note");
Role role=new Role();
role.setId(id);
role.setRoleName(userName);
role.setNote(note);
return role;
}
}catch(SQLException ex){
ex.printstack();
}finally{
this.close(rs,ps,connection);
}
return null;
}
//关闭数据库连接
private void close(ResultSet rs,Statement stmt,Connection connection){
try{
if(rs!=null && !rs.isClosed()){
rs.close();
}
if(stmt!=null && !stmt.isClosed()){
stmt.close();
}
if(connection!=null && !connection.isClosed()){
connection.close();
}
}catch(SQLException ex){
ex.printstack();
}
}
public static void main(String[]args){
JdbcExample example=new JdbcExample();
ROle role=example.getRole(1L);
System.print.out("role_name="+role.getRoleName());
}
}
每个Mybatis的应用都是以SqlSessionFactory的实例为中心。Mybatis提供两种模式创建SqlSessionFactory:
一.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.learn.chapter2.po.Role"/>
</typeAliases>
<!--定义数据库信息,默认使用development数据库构建环境-->
<enviroments default="developments">
<enviroment id="development">
<!--采用jdbc事务管理-->
<transactionManager type="JDBC"/>
<!--配置数据库连接信息-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</enviroment>
</enviroments>
<!--定义映射器-->
<mappers>
<mapper resource="com/learn/chapter2/mapper/roleMapper.xml"/>
</mappers>
</configuration>
映射文件: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">
<mapper namespace="com.learn.mybatis.chapter1.mapper.RoleMapper">
<select id="getRole" parameterType="long" resultType="com.learn.mybatis.chapter1.pojo.Role>
<!--这里的SQL没有给出映射规则的原因是:我们使用的SQL列名和POJO的属性名保持一致,这个时候Mybatis会自动提供映射规则,所以省去了这部分的配置工作-->
select id,role_name as roleName,note from t_role where id=#{id}
</select>
利用如上配置文件,我们的实现代码:
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=null;
sqlSessionFactory=new SqlSessionFactoryBuilder().buid(inputStream);
二.使用代码方式构建:
//构建数据库连接池
PooledDateSource dateSource=new PooledDateSource();
dateSource.setDriver("com.mysql.jdbc.Driver");
dateSource.setUrl("jdbc:mysql://localhost:3306/mybatis");
dateSource.setUsername("root");
dateSource.setPassword("learn");
//构建数据库事务方式
TranscationFactory transactionFactory=new JdbcTransactionFactory();
//创建数据库运行环境
Environment enviroment=new Enviroment("development",transactionFactory,dataSource);
//构建Configuration对象
Configuration configuration=new Configuration(enviroment);
//注册一个Mybatis上下文别名
configuration.getTypeAliasRegistry().registerAlias("role",Role.class);
//加入一个映射器
configuration.addMapper(RoleMapper.class);
//使用SqlSessionFactoryBuilder 构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(configuration);