IDEA使用Maven和MyBatis簡化數(shù)據(jù)庫連接的詳細(xì)過程
一、引入MyBatis:
1.后端對于前端數(shù)據(jù)的處理和反饋大致分為三個(gè)層:controller層負(fù)責(zé)前后端交互,service層負(fù)責(zé)進(jìn)行邏輯處理,Dao層也叫持久層,負(fù)責(zé)連接數(shù)據(jù)庫。

2.打開數(shù)據(jù)庫,創(chuàng)建一個(gè)新的數(shù)據(jù)庫并創(chuàng)建一個(gè)新的表來方便實(shí)驗(yàn)(我取的數(shù)據(jù)庫名為“newtest”,表名為“student”的學(xué)生表)。在表中添加id、name、sex、age幾個(gè)字段并填寫數(shù)據(jù):

3.打開pom.配置文件,在dependencies標(biāo)簽中導(dǎo)入mybatis和mysql的相關(guān)包(不需要實(shí)際導(dǎo)入,只需要拿到“坐標(biāo)”):
具體引入的內(nèi)容:
(1). 引入MyBatis的3.4.5的版本的坐標(biāo),保證是MyBatis
(2). 引入MySQL驅(qū)動(dòng)的jar包,5.1.6版本,保證能夠連接到數(shù)據(jù)庫
(3). 引入Junit單元測試的jar包
(4). 引入log4j的jar包,1.2.12版本(需要引入log4j.properties的配置文件)
在dependencies中填寫:
<dependencies> <!--mybatis核心包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--mysql驅(qū)動(dòng)包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!-- 日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
導(dǎo)入成功后,點(diǎn)擊右上角的M標(biāo)志刷新:

二、測試:
1.創(chuàng)建實(shí)體類:數(shù)據(jù)庫映射到程序中還需要借助entity層,用于存儲(chǔ)對應(yīng)數(shù)據(jù)庫中的信息(類對表,屬性對應(yīng)字段)。在main-->java-->com.newFile目錄下創(chuàng)建一個(gè)“dao”包和“entity”包。在entity中創(chuàng)建students實(shí)體類(表名與類名無關(guān),但字段名與屬性名相關(guān))。
填寫屬性和封裝方法:
private int id;
private String name;
private int age;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//toString方法
@Override
public String toString() {
return "students{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}2.在dao中編寫一個(gè)接口作為規(guī)范:在dao目錄下創(chuàng)建一個(gè)名為“StudentDao”的接口文件(名字可以任?。覀兿染帉懸粋€(gè)findAll方法測試:
package com.newFile.dao;
//引入students包
import com.newFile.entity.students;
//引入list
import java.util.List;
public interface StudentDao {
//定義findAll方法查找所有內(nèi)容
public List<students> findAll();
}3.創(chuàng)建接口的實(shí)現(xiàn)類:在main目錄下創(chuàng)建一個(gè)resources資源目錄,資源目錄中存放著所有接口的實(shí)現(xiàn)類。再在resources中創(chuàng)建一個(gè)mapper目錄存放接口的實(shí)現(xiàn)類(方便后續(xù)配置),在mapper目錄下創(chuàng)建一個(gè)xml配置文件并通過配置文件創(chuàng)建接口的實(shí)現(xiàn)類,將配置文件命名為“StudentDao”并寫入:
<?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.newFile.dao.StudentDao"></mapper>
再在resources目錄下創(chuàng)建一個(gè)主配置文件命名為“SqlMapConfig.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>
<environments default="mysql">
<environment id="mysql">
<!--配置事務(wù)的類型,使用本地事務(wù)策略-->
<transactionManager type="JDBC"></transactionManager>
<!--是否使用連接池 POOLED表示使用鏈接池,UNPOOLED表示不使用連接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--student為數(shù)據(jù)庫名-->
<property name="url" value="jdbc:mysql://localhost:3306/newtest"/>
<!--這里寫你mysql數(shù)據(jù)庫的用戶名和密碼-->
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--實(shí)現(xiàn)類的路徑-->
<mapper resource="mapper/StudentDao.xml"></mapper>
</mappers>
</configuration>在接口實(shí)現(xiàn)文件的“mapper”標(biāo)簽中就可以寫sql語句了,實(shí)現(xiàn)的是findAll方法:
<select id="findAll" resultType="com.newFile.entity.students">
select * from student
</select>其中:(應(yīng)該傳入包裝類型的類全名)
1.id表示接口中定義的對應(yīng)方法
2.parameterType表示方法需要傳入的參數(shù)
3.resultType表示返回值的類型
4.測試:
MyBatis官網(wǎng):從 XML 中構(gòu)建 SqlSessionFactory
每個(gè)基于 MyBatis 的應(yīng)用都是以一個(gè) SqlSessionFactory 的實(shí)例為核心的。SqlSessionFactory 的實(shí)例可以通過 SqlSessionFactoryBuilder 獲得。而 SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個(gè)預(yù)先配置的 Configuration 實(shí)例來構(gòu)建出 SqlSessionFactory 實(shí)例。
從 XML 文件中構(gòu)建 SqlSessionFactory 的實(shí)例非常簡單,建議使用類路徑下的資源文件進(jìn)行配置。 但也可以使用任意的輸入流(InputStream)實(shí)例,比如用文件路徑字符串或 file:// URL 構(gòu)造的輸入流。MyBatis 包含一個(gè)名叫 Resources 的工具類,它包含一些實(shí)用方法,使得從類路徑或其它位置加載資源文件更加容易。
SqlSession 是 MyBatis 框架的核心接口,用于執(zhí)行 SQL 語句、管理事務(wù)以及獲取映射器(Mapper)實(shí)例,是應(yīng)用程序與持久層交互的直接入口。
1.在Test測試類中創(chuàng)建run方法:
public void run() throws IOException {
//主配置文件路徑
String resourcePath = "SqlMapConfig.xml";
//加載主配置文件,為了拿到SqlSessionFactory
InputStream inputStream = Resources.getResourceAsStream(resourcePath);
//拿到SqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//創(chuàng)建SqlSession
SqlSession session = sqlSessionFactory.openSession();
//通過SqlSession對象創(chuàng)建studentDao接口的代理對象
studentDao mapper = session.getMapper(studentDao.class);
//通過代理訪問接口中的方法
List<students> arr = mapper.findAll();
//打印輸出
for(students student : arr){
System.out.println(student);
}
}2.創(chuàng)建Test對象并執(zhí)行run方法:
Test test = new Test();test.run();
輸出結(jié)果:

第一個(gè)sql查詢就成功了。
到此這篇關(guān)于IDEA使用Maven和MyBatis簡化數(shù)據(jù)庫連接的詳細(xì)過程的文章就介紹到這了,更多相關(guān)idea簡化數(shù)據(jù)庫連接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Springboot-MyBatis配置-配置端口號與服務(wù)路徑(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)
- 在IDEA中maven配置MyBatis的流程詳解
- IDEA 創(chuàng)建一個(gè)Mybatis Maven項(xiàng)目的方法步驟(圖文)
- 詳解Spring與Mybatis整合方法(基于IDEA中的Maven整合)
- IDEA Maven Mybatis generator 自動(dòng)生成代碼(實(shí)例講解)
- IDEA連接達(dá)夢數(shù)據(jù)庫的詳細(xì)配置指南
- IntelliJ IDEA 設(shè)置數(shù)據(jù)庫連接全局共享的步驟
相關(guān)文章
使用新版Maven-mvnd快速構(gòu)建項(xiàng)目
本文主要介紹了使用新版Maven-mvnd來快速構(gòu)建項(xiàng)目,相比于Maven,mvnd可以顯著提高構(gòu)建速度,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
淺談SpringSecurity注解與AOP切面執(zhí)行順序
這篇文章主要介紹了淺談SpringSecurity注解與AOP切面執(zhí)行順序,引入Spring Security后,在Controller的方法中會(huì)出現(xiàn)Spring Security的方法注解與AOP同時(shí)存在的問題,這是就會(huì)設(shè)計(jì)順序問題,需要的朋友可以參考下2023-10-10
IDEA?2021.3?使用及idea2021.3.1激活使用方法
IDEA?全稱?IntelliJ?IDEA,是java語言開發(fā)的集成環(huán)境,IntelliJ在業(yè)界被公認(rèn)為最好的java開發(fā)工具之一,今天通過本文給大家介紹idea2021.3.1激活及使用教程,感興趣的朋友一起看看吧2022-01-01
Springboot升級到2.7.2結(jié)合nacos遇到的坑及解決
這篇文章主要介紹了Springboot升級到2.7.2結(jié)合nacos遇到的坑及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

