一文教會你用mybatis查詢數(shù)據(jù)庫數(shù)據(jù)
一、新建數(shù)據(jù)庫和表
建一個數(shù)據(jù)庫mybatis,建一個表person,然后填充一些數(shù)據(jù)(這里我是使用了圖形化工具Navicat for MySQL,就不寫原生的數(shù)據(jù)庫代碼了)
二、新建maven項目,添加mybatis依賴
新建一個maven項目,我以往文章有提到如何建一個maven項目,這里不多贅述:
添加依賴,在pom.xml文件上添加以下代碼
<dependencies> <!-- 數(shù)據(jù)庫驅(qū)動 版本不能亂用5.1.47 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- 單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> </dependencies>
三、新建實(shí)體類、接口(Mapper)
Person實(shí)體類
package entity; public class Person { private int id; private String name; private int age; public Person() { } public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
PersonMapper接口
import entity.Person; import java.util.List; public interface PersonMapper { //查詢所有數(shù)據(jù) public List<Person> getPerson(); }
四、新建實(shí)現(xiàn)類(使用.xml映射文件)
在resources目錄下新建一個PersonMapper.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"> <!-- 映射 namespace : 和哪個接口有映射關(guān)系--> <mapper namespace="mapper.PersonMapper"> <!-- select標(biāo)簽中的語句 需要和接口中哪個方法有映射關(guān)系 id : 接口中的方法名 resultType: 方法的返回類型(寫全類名) --> <select id="getPerson" resultType="entity.Person"> select * from person </select> </mapper>
五、新建全局配置文件(mybatis-config.xml)
在resources目錄下新建一個mybatis-config.xml文件
數(shù)據(jù)庫密碼(password)寫你自己的
<?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"> <!-- 接下里的配置也就是一個數(shù)據(jù)源配置 --> <configuration> <!-- environments 多環(huán)境配置 environment 配置數(shù)據(jù)源 transactionManager 事務(wù)管理器 dataSource 數(shù)據(jù)源 --> <environments default="p1"> <environment id="p1"> <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="123456"/> </dataSource> </environment> </environments> <!-- 注冊映射 --> <mappers> <mapper resource="PersonMapper.xml"/> </mappers> </configuration>
六、編寫demo類,進(jìn)行測試查詢數(shù)據(jù)庫
1、讀取配置文件
2、根據(jù)配置文件構(gòu)建工廠
3、獲取會話
4、獲取Mapper對象
5、執(zhí)行方法
6、關(guān)閉會話
public class demo { @Test public void test2() throws IOException { //讀取配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); //根據(jù)配置文件構(gòu)建工廠 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //獲取會話 SqlSession sqlSession = sessionFactory.openSession(); //獲取Mapper對象 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); //執(zhí)行方法 List<Person> person = personMapper.getPerson(); System.out.println(person); //關(guān)閉會話 sqlSession.close(); } }
輸出結(jié)果
總結(jié)
到此這篇關(guān)于mybatis查詢數(shù)據(jù)庫數(shù)據(jù)的文章就介紹到這了,更多相關(guān)mybatis查詢數(shù)據(jù)庫數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用jmx?exporter采集kafka指標(biāo)示例詳解
這篇文章主要為大家介紹了使用jmx?exporter采集kafka指標(biāo)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Java基礎(chǔ)第二篇方法與數(shù)據(jù)成員
在上一篇文章中介紹了Java基礎(chǔ) 從HelloWorld到面向?qū)ο螅覀兂醪搅私饬藢ο?object)。對象中的數(shù)據(jù)成員表示對象的狀態(tài)。對象可以執(zhí)行方法,表示特定的動作。這篇文章我們進(jìn)一步深入到對象。了解Java中方法與數(shù)據(jù)成員的一些細(xì)節(jié)。2021-09-09java鏈?zhǔn)絼?chuàng)建json對象的實(shí)現(xiàn)
本文主要介紹了java中如何通過最簡單的方式實(shí)現(xiàn)鏈?zhǔn)絼?chuàng)建json對象,解決創(chuàng)建json代碼臃腫的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02詳解Spring中singleton?bean如何同時服務(wù)多個請求
這篇文章主要介紹了詳解Spring中singleton?bean如何同時服務(wù)多個請求2023-02-02Java集合定義與用法實(shí)例總結(jié)【Set、List與Map】
這篇文章主要介紹了Java集合定義與用法,結(jié)合實(shí)例形式總結(jié)分析了Java集合中Set、List和Map相關(guān)概念、功能、用法及操作注意事項,需要的朋友可以參考下2018-08-08windows系統(tǒng)使用mvn命令打包并指定jdk路徑方式
這篇文章主要介紹了windows系統(tǒng)使用mvn命令打包并指定jdk路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Java中設(shè)置session超時(失效)的三種方法
這篇文章主要介紹了Java中設(shè)置session超時(失效)的三種方法,本文講解了在web容器中設(shè)置、在工程的web.xml中設(shè)置、通過java代碼設(shè)置3種方法,需要的朋友可以參考下2015-07-07