MyBatis mapping類基本用法
有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
定義mapping類
MyBatis 有兩種定義查詢結(jié)果到 Java 類的映射關(guān)系的方式,一種是通過xml文件定義,一種是通過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中對應(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);
}初始化并注冊mapping類
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); //注冊mapping類
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 可以在整個app的生命周期中只創(chuàng)建一次,SqlSession需要在每次執(zhí)行sql的函數(shù)中創(chuàng)建一次并且使用后需要進行關(guān)閉:
SqlSession session = sqlSessionFactory.openSession();
try {
// do work
} finally {
session.close();
}以上就是MyBatis mapping類基本用法的詳細(xì)內(nèi)容,更多關(guān)于MyBatis mapping類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot中@ConditionalOnProperty注解的使用方法詳解
這篇文章主要介紹了SpringBoot中@ConditionalOnProperty注解的使用方法詳解,在開發(fā)基于SpringBoot框架的項目時,會用到下面的條件注解,有時會有需要控制配置類是否生效或注入到Spring上下文中的場景,可以使用@ConditionalOnProperty注解來控制,需要的朋友可以參考下2024-01-01
MyEclipse打開文件跳轉(zhuǎn)到notepad打開問題及解決方案
windows系統(tǒng)打開README.md文件,每次都需要右鍵選擇notepad打開,感覺很麻煩,然后就把README.md文件打開方式默認(rèn)選擇了notepad,這樣每次雙擊就能打開,感覺很方便,這篇文章主要介紹了MyEclipse打開文件跳轉(zhuǎn)到notepad打開問題,需要的朋友可以參考下2024-03-03
Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring Boot 配置 IDEA和DevTools 熱部署的方法
這篇文章主要介紹了Spring Boot 配置 IDEA和DevTools 熱部署的方法,需要的朋友可以參考下2018-02-02
解決Nacos成功啟動但是無法訪問 (Connection refused)
這篇文章主要介紹了解決Nacos成功啟動但是無法訪問 (Connection refused)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
SpringMVC Mock測試實現(xiàn)原理及實現(xiàn)過程詳解
這篇文章主要介紹了SpringMVC Mock測試實現(xiàn)原理及實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10

