第一個Mybatis程式
一、創建資料庫、資料表
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-7bVooJgK-1613463680557)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216152028595.png)]](https://img-blog.csdnimg.cn/20210216162207507.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
sql陳述句如下:
create database mybatis;
use mybatis;
create table user(
id int primary key auto_increment ,
name varchar(50) not null,
pwd varchar(50) not null
);
插入資料:
insert into user values (1,'daidaimei','12345'),(2,'chenxm','12345'),(5,'dym','11111');
二、新建一個Maven專案
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-0A44rjZo-1613463680565)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210215183255092.png)]](https://img.uj5u.com/2021/02/17/225200170608441.png)


點擊完成,
我們可以將mybatis公共的代碼(例如pom.xml檔案許多重要配置代碼是重復的)在父專案中完成,這樣下次新建專案的時候只要在父專案的基礎上新建模塊(Module)就行了,就不用每次寫pom.xml檔案里的重復內容;
-
洗掉src檔案夾;
父專案不寫代碼,只做一些固定的設定,代碼在子專案中完成, -
撰寫父專案mybatis的pom.xml檔案:
匯入依賴:
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
? *匯入資源路徑:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
三、在mybatis中新建子專案


注意Parent繼承父專案mybatis;
點擊完成,
-
在mybatis_01的src>resource檔案夾下新建核心組態檔mybatis-config.xml ;
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="https://www.cnblogs.com/1908834224cxm/archive/2021/02/16/com.mysql.jdbc.Driver"/> <property name="url" value="https://www.cnblogs.com/1908834224cxm/archive/2021/02/16/jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"/> <property name="username" value="https://www.cnblogs.com/1908834224cxm/archive/2021/02/16/root"/> <property name="password" value="https://www.cnblogs.com/1908834224cxm/archive/2021/02/16/1234"/> </dataSource> </environment> </environments>注意password填寫自己資料庫的密碼!
-
撰寫util、dao、pojo層代碼;
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-NQYHAJIW-1613463680576)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210215183527239.png)]](https://img-blog.csdnimg.cn/20210216163428158.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
Util工具類:public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; static{ try { //使用mybatis獲取sqlSession物件 String resource = "mybatis-config.xml"; InputStream inputStream = null; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (IOException e) { e.printStackTrace(); } } //既然有了 SqlSessionFactory,顧名思義,我們可以從中獲得 SqlSession 的實體, // SqlSession 提供了在資料庫執行 SQL 命令所需的所有方法, public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }User物體類:
public class User { private int id; private String name; private String pwd; public User(int id,String name,String pwd) { this.id = id; this.name = name; this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } 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 String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }UserMapper介面:
public interface UserMapper { //查詢全部結果 List<User> GetUserList(); }UseMapper.xml組態檔(每一個Mapper介面類都需要一個同名的xml組態檔來實作它):
<mapper namespace="com.cxm.dao.UserMapper"> <select id="GetUserList" resultType="com.cxm.pojo.User" parameterType="int"> select * from mybatis.user </select> </mapper>在核心組態檔mybatis-config.xml中添加mapper宣告:
<mappers> <mapper resource="com/cxm/dao/UserMapper.xml"/> </mappers>
每寫一個介面組態檔(如UserMapper.xml)都需要在核心組態檔mybatis-config中宣告,
即mybatis-config完整代碼如下:

四、撰寫測驗類
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-tKzJsAdT-1613463680580)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216150715336.png)]](https://img-blog.csdnimg.cn/20210216163654460.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
public class UserDaoTest {
@Test
public void TestSelect(){
//獲得Session物件
SqlSession sqlSession =MybatisUtil.getSqlSession();
//getMapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.GetUserList();
//遍歷得到結果
for(User user : userList){
System.out.println(user);
}
//關閉sqlSesssion
sqlSession.close();
}
}
控制臺輸出:
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-0AOh0j9C-1613463680584)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216153009861.png)]](https://img-blog.csdnimg.cn/20210216163807770.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
五、可能遇到的問題:
- 找不到xml組態檔
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-DpETOYsq-1613463680586)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216154756829.png)]](https://img-blog.csdnimg.cn/20210216164027108.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
原因:未在pom.xml中匯入資源路徑,
決議:若未在pom.xml中匯入build資源路徑,系統不能識別到我們的xml檔案,還有其他形式的組態檔比如properties組態檔;
解決:把java檔案夾和resources檔案夾下的xml檔案、properties檔案宣告為資源檔案,
代碼如下:(只需在父專案的xml檔案中寫一遍即可,在父專案的基礎上新建的專案都不用寫)
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
注:每次修改pom.xml檔案后需點擊更新maven
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Tlarmemm-1613463680589)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216155621561.png)]](https://img-blog.csdnimg.cn/20210216164153917.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
2. 核心組態檔中亂碼問題
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-04Ij2UD3-1613463680591)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216160307550.png)]](https://img-blog.csdnimg.cn/20210216165322829.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
原因:在mybatis-config.xml檔案中寫中文注釋,字串決議時出問題;
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ApxLIfbV-1613463680592)(C:\Users\呆呆槑\AppData\Roaming\Typora\typora-user-images\image-20210216160537540.png)]](https://img-blog.csdnimg.cn/20210216165406116.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDA2NDUy,size_16,color_FFFFFF,t_70)
決議:核心組態檔mybatis -config.xml檔案和各個介面組態檔UserMapper.xml中都盡量不要有多余的注釋、字符,格式盡量標準、統一,有的IDE可能沒有亂碼問題,有的很嚴格,
解決:檢查、洗掉xml檔案中的多余字符、注釋,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/260244.html
標籤:其他
上一篇:云原生系列3 pod核心欄位
下一篇:Go string 一清二楚
