一些術(shù)語
POJO(Plain Ordinary Java Object)簡單的Java對象
ORM:Object Relational Mapping,對象關(guān)系映射
JDBC:Java Data Base Connectivity
JDBC和MyBatis
JDBC是由Sun公司提出的規(guī)范朴皆,用于Java和數(shù)據(jù)庫之間的連接。它只是一系列接口,各數(shù)據(jù)庫廠商負(fù)責(zé)具體的實現(xiàn)。
通過JDBC對數(shù)據(jù)庫進(jìn)行操作通常需要幾步,以查詢?yōu)槔?/p>
- 注冊驅(qū)動提供數(shù)據(jù)庫信息并得到一個Connection對象
- 通過Connection得到Statement對象
- 執(zhí)行SQL語句占遥,獲得結(jié)果集
- 操作結(jié)果集得到相應(yīng)數(shù)據(jù)轉(zhuǎn)化為具體的POJO對象
- 關(guān)閉相關(guān)資源
可見,通過JDBC操作數(shù)據(jù)庫非常復(fù)雜,工作量較大愚臀。于是出現(xiàn)了ORM模型,ORM是一種數(shù)據(jù)庫表和POJO的映射模型矾利,ORM模型使得程序員集中精力于代碼的編寫姑裂,Hibernate是一種典型的ORM框架,它提供了一種全表映射男旗,不用程序員自己編寫SQL語句舶斧,其余相關(guān)知識在此不在贅述。MyBatis是另外一種ORM模型察皇,它提供了一種比hibernate更加靈活的方式茴厉。
MyBatis核心組件
SqlSessionFactoryBuilder:根據(jù)配置信息生成SqlSessionFactory
SqlSessionFactory:一個接口泽台,用于生成SqlSession
SqlSession:發(fā)送SQL去執(zhí)行,也可以得到Mapper
Mapper:用于發(fā)送SQL執(zhí)行矾缓,并返回結(jié)果怀酷。
如圖:
構(gòu)建SqlSessionFactory
我們有兩種方式得到SqlSessionFactory,一種是通過XML文件方式嗜闻,另一種是通過代碼方式蜕依。通過XML方式得到SqlSessionFactory如下:
<?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>
<!-- 數(shù)據(jù)庫相關(guān)信息 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/wechatrobot"/>
<property name="username" value="root"/>
</dataSource>
</environment>
</environments>
<!--定義映射器-->
<mappers>
<mapper resource="config/sqlconfig/Message.xml"/>
</mappers>
</configuration>
// 通過配置文件得到SqlSessionFactory,并得到SqlSession
public SqlSession getSqlSession() throws IOException{
String resources = "config/Configuration.xml";
SqlSession sqlSession = null;
Reader reader = Resources.getResourceAsReader(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
映射器
我們有兩種方式得到映射器琉雳,一種是XML文件方式笔横,一種是通過注解。
XML
注解
一個簡單的例子
下邊這個例子不通過映射器直接通過SQL ID加參數(shù)發(fā)送SQL并執(zhí)行咐吼。
Mapper配置文件:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="Message">
// 返回類型
<resultMap type="model.Message" id="MessageResult">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="command" jdbcType="VARCHAR" property="command"/>
<result column="description" jdbcType="VARCHAR" property="description"/>
<result column="content" jdbcType="VARCHAR" property="content"/>
</resultMap>
<!--查詢語句-->
<!---- // ONGI表達(dá)式-->
<select id="queryList" parameterType="model.Message" resultMap="MessageResult">
select id,command,description,content from message where 1=1
</select>
<!--// 刪除指定列-->
<delete id="deleteOne" parameterType="int" >
DELETE FROM message WHERE id=#{_parameter}
</delete>
</mapper>
查詢代碼:
public List<Message> queryList(String command, String description) {
DbAccess dbAccess = new DbAccess();
List<Message> messageList = new ArrayList<>();
try {
// 這個請查閱上一節(jié)得到SQLSession的代碼
SqlSession sqlSession = dbAccess.getSqlSession();
Message message = new Message();
message.setCommand(command);
message.setDescription(description);
// 只能有一個參數(shù)吹缔,故用message包裝參數(shù)
// 直接通過SQL ID加參數(shù)發(fā)送SQL語句
messageList = sqlSession.selectList("Message.queryList",message);
} catch (IOException e) {
e.printStackTrace();
}
return messageList;
}