MyBatis mapping類(lèi)基本用法
有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
定義mapping類(lèi)
MyBatis 有兩種定義查詢結(jié)果到 Java 類(lèi)的映射關(guān)系的方式,一種是通過(guò)xml文件定義,一種是通過(guò)Java annonation 定義,這里使用第二種方法。
現(xiàn)在我們有一張mysql的表定義如下:
CREATE TABLE `MY_BATIS_TEST` ( `id` varchar(255) NOT NULL DEFAULT '', `url` varchar(255) DEFAULT NULL )
首先定義table一條數(shù)據(jù)在Java中對(duì)應(yīng)的class
public class Redord {
public String url;
}定義sql查詢到Java class 結(jié)果集合的映射:
public interface SimpleMapper {
@Select("select url from testdb.MY_BATIS_TEST limit 1;")
Redord selectOneRecord();
@Select("select url from testdb.MY_BATIS_TEST;")
Set<Record> selectRecords();
@Select("select url from testdb.MY_BATIS_TEST where id=#{id};")
Record selectRecordByID(int id);
}初始化并注冊(cè)mapping類(lèi)
Properties properties = new Properties();
properties.setProperty("driver", "com.mysql.jdbc.Driver");
properties.setProperty("url", "jdbc:mysql://127.0.0.1:3306/testdb");
properties.setProperty("username", "the_user_name");
properties.setProperty("password", "the_password");
PooledDataSourceFactory pooledDataSourceFactory = new PooledDataSourceFactory();
pooledDataSourceFactory.setProperties(properties);
DataSource dataSource = pooledDataSourceFactory.getDataSource();
Environment environment = new Environment("development", new JdbcTransactionFactory(), dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(SimpleMapper.class); //注冊(cè)mapping類(lèi)
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);從mysql中查詢數(shù)據(jù)
SqlSession session = sqlSessionFactory.openSession();
try{
PluginMapper mapper = session.getMapper(PluginMapper.class);
Plugin pl = mapper.selectPluginsByID(1000);
System.out.println(pl.url);
} finally {
session.close();
}全局唯一以及線程安全
SqlSessionFactory 可以在整個(gè)app的生命周期中只創(chuàng)建一次,SqlSession需要在每次執(zhí)行sql的函數(shù)中創(chuàng)建一次并且使用后需要進(jìn)行關(guān)閉:
SqlSession session = sqlSessionFactory.openSession();
try {
// do work
} finally {
session.close();
}以上就是MyBatis mapping類(lèi)基本用法的詳細(xì)內(nèi)容,更多關(guān)于MyBatis mapping類(lèi)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java如何通過(guò)FileOutputStream字節(jié)流向文件中寫(xiě)數(shù)據(jù)
這篇文章主要介紹了java如何通過(guò)FileOutputStream字節(jié)流向文件中寫(xiě)數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
IDEA?服務(wù)器熱部署圖文詳解(On?Update?action/On?frame?deactivation)
這篇文章主要介紹了IDEA?服務(wù)器熱部署詳解(On?Update?action/On?frame?deactivation),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Spring3?中?RabbitMQ?的使用與常見(jiàn)場(chǎng)景分析
本文介紹了Spring3中RabbitMQ的使用,涵蓋了RabbitMQ的基本知識(shí)、五種模式、數(shù)據(jù)隔離、消費(fèi)者確認(rèn)、死信交換機(jī)、延遲功能、消息堆積解決方法、高可用性以及消息重復(fù)消費(fèi)問(wèn)題的解決方案,感興趣的朋友跟隨小編一起看看吧2025-02-02
C/C++中的struct結(jié)構(gòu)體詳細(xì)解讀
這篇文章主要介紹了C/C++中的struct結(jié)構(gòu)體詳細(xì)解讀,結(jié)構(gòu)體是由一批數(shù)據(jù)組合而成的結(jié)構(gòu)型數(shù)據(jù),組成結(jié)構(gòu)型數(shù)據(jù)的每個(gè)數(shù)據(jù)稱為結(jié)構(gòu)型數(shù)據(jù)的“成員”,其描述了一塊內(nèi)存區(qū)間的大小及意義,需要的朋友可以參考下2023-10-10
Java設(shè)計(jì)模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹,本文講解了如何使用責(zé)任鏈模式,并給出了4種使用實(shí)例,需要的朋友可以參考下2015-03-03

