了解Mybatis的基礎
免費學習推薦:#mysql影片教學
#mybatis
mybatis-config.xml詳細配置(配置時要把多餘的屬性刪除不能有中文否則報錯!)<?xml version="1.0" encoding="UTF-8" ?>nbsp;configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"><!--configuration核心配置 配置文件的根元素 --><configuration>
<!-- 属性:定义配置外在化 -->
<properties></properties>
<!-- 设置:定义mybatis的一些全局性设置 -->
<settings>
<!-- 具体的参数名和参数值 -->
<setting></setting>
</settings>
<!-- 类型名称:为一些类定义别名 -->
<typealiases>
<!-- 实体类少 建议 第一种取别名方式-->
<typealias></typealias>
<!--实体类多 建议 第二种取别名方式
默认情况下用这种方式 别名为类名 首字母最好小写
-->
<package></package>
</typealiases>
<!-- 类型处理器:定义Java类型与数据库中的数据类型之间的转换关系 -->
<typehandlers></typehandlers>
<!-- 对象工厂 -->
<objectfactory></objectfactory>
<!-- 插件:mybatis的插件,插件可以修改mybatis的内部运行规则 -->
<plugins>
<plugin></plugin>
</plugins>
<!-- 环境:配置mybatis的环境 -->
<environments>
<!-- 环境变量:可以配置多个环境变量,比如使用多数据源时,就需要配置多个环境变量 -->
<environment>
<!-- 事务管理器 -->
<transactionmanager></transactionmanager>
<!-- 数据源 配置连接我的数据库-->
<datasource>
<property></property>
<property></property>
<property></property>
<property></property>
</datasource>
</environment>
</environments>
<!-- 数据库厂商标识 -->
<databaseidprovider></databaseidprovider>
<!-- 映射器:指定映射文件或者映射类 -->
<mappers>
<mapper></mapper>
</mappers></configuration>
#減少資料存取量
- sql語句: select * from 表名limt 0,5;
- 0:資料開始的位置
第一種:使用Mybatis
# 1介面
List<user> getUserByLimit(Map<string> map);</string></user>
2mapeer.xml
<select> select * from mybatis.user limit ${starIndex},${pageSize} </select>
# 2-1結果集映射
<resultmap> <result></result> </resultmap>
3測試 @Test
public void getUserByLimitTest() {
SqlSession sqlSession = MyBatisUtils.getSqlSession ();
UserMapper mapper = sqlSession.getMapper (UserMapper.class);
HashMap hashMap = new HashMap<string> ();
hashMap.put ("starIndex", 1);
hashMap.put ("pageSize", 2);
List userByLimit = mapper.getUserByLimit (hashMap);
for (Object o : userByLimit) {
System.out.println (o);
}
sqlSession.close ();
}</string>
第二種:使用RowBounds方法
1.介面
2.實作介面
<select> select * from mybatis.user </select>
3.測試: /**
* 测试使用RowBounds实现分页
*/@Test
public void getUserByLimitRowBoundsTest() {
SqlSession sqlSession = MyBatisUtils.getSqlSession ();
RowBounds rowBounds = new RowBounds (0, 2);
List<user> userList = sqlSession.selectList ("com.kuang.w.dao.UserMapper.getUserList", null, rowBounds);
for (User user : userList) {
System.out.println (user);
}
//关闭
sqlSession.close ();
}</user>
#第三種:使用Mybatis的分頁外掛pageHeIper
sql 多對一處理
資料庫:
pojo
資料庫中teacher-table表對應實體類別Teacher
package com.kuang.w.pojo; import lombok.Data; /** * @author W */ @Data public class Teacher { private int tId; private String tName; }
資料庫中user表對應實體類別Student
package com.kuang.w.pojo;import lombok.Data;/** * @author W */@Datapublic class Student { private int id; private int tid; private String name; private String password; private Teacher teacher;}
1.介面
List<student> getStudentList();</student>
2.xml設定實作介面
<!-- 多对一查询 1 子查询 mysql 通过一个表里是数据 与另一个表的一个数据相的情况下 查询另一个的数据 一起显示 --> <select> select * from mybatis.user; </select> <resultmap> <!-- 复杂属性 对象用 :association 集合用:collection--> <!--column 数据库中的字段 property 实体类中的属性--> <result></result> <result></result> <result></result> <!--javaType 一个 Java 类的全限定名 ,或一个类型别名(关于内置的类型别名,可以参考上面的表格)。 如果你映射到一个 JavaBean,MyBatis 通常可以推断类型。 然而,如果你映射到的是 HashMap, 那么你应该明确地指定 javaType 来保证行为与期望的相一致。--> <association></association> </resultmap> <select> select * from mybatis.teacher_table where tid = #{id}; </select>
<!--2 多表联查--> <select> select u.id uid, u.name uname, u.password upassword, u.tid utid, t.tname from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid; </select> <!-- 映射--> <resultmap> <result></result> <result></result> <result></result> <result></result> <association> <result></result> </association> </resultmap>
mybatis-config.xm設定
<?xml version="1.0" encoding="UTF8" ?>nbsp;configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties></properties> <settings> <setting></setting> </settings> <typealiases> <typealias></typealias> <typealias></typealias> </typealiases> <environments> <environment> <transactionmanager></transactionmanager> <datasource> <property></property> <property></property> <property></property> <property></property> </datasource> </environment> </environments> <mappers> <!-- <mapper resource="com/kuang/w/dao/TeacherMapper.xml"></mapper> <mapper resource="com/kuang/w/dao/StudentMapper.xml"></mapper>--> <mapper></mapper> <mapper></mapper> </mappers></configuration>
3 測試 @Test
public void getStudentListTest() {
SqlSession sqlSession = MyBatisUtils.getSqlSession ();
StudentMapper mapper = sqlSession.getMapper (StudentMapper.class);
List<student> studentList = mapper.getStudentList ();
for (Student student : studentList) {
System.out.println (student);
}
sqlSession.commit ();
sqlSession.close ();
}</student>
#資料表結構對應的實體類別不變
1介面
List<teacher> getTeacher(int tid);</teacher>
2.1 xml實作介面
<select> select t.tid, t.tname, u.id, u.name, u.password from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid and t.tid = #{tid}; </select>
2.2對應設定
<resultmap> <result></result> <result></result> <!-- 复杂属性 对象用 :association 集合用:collection--> <collection> <!--javaType 指定属性类型 一个 Java 类的全限定名--> <result></result> <result></result> <result></result> <result></result> </collection> </resultmap>
3測試
/*测试一对多*/ @Test public void getTeacherTest2() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class); List<teacher> teacher = mapper.getTeacher (1); for (Teacher teacher1 : teacher) { System.out.println (teacher1); } //提交事务 架子 这里可以不要 sqlSession.commit (); // 关闭 sqlSession.close (); }</teacher>
結果
com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.kuang.w.dao.myTest,getTeacherTest2 Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.Opening JDBC Connection Created connection 164974746.Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]==> Preparing: select t.tid, t.tname, u.id, u.name, u.password from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid and t.tid = ?; ==> Parameters: 1(Integer)第二種方式: 子查詢1介面
List<teacher> getTeacher(int tid);</teacher>登入後複製2 實作介面
3測試同上<!--第二种方式: 子查询--> <select> select * from mybatis.teacher_table where tid = #{tid}; </select> <resultmap> <!-- 复杂属性 对象用 :association 集合用:collection 我们需要单独处理对象: association 集合: collection javaType=""指定属性的类型! 集合中的泛型信息,我们使用ofType 获取 --> <result></result> <result></result> <collection> </collection> </resultmap> <select> select * from mybatis.user where tid = #{tid}; </select>登入後複製。 。 。 。######相關免費學習推薦:mysql資料庫
(影片)
以上是了解Mybatis的基礎的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

全表掃描在MySQL中可能比使用索引更快,具體情況包括:1)數據量較小時;2)查詢返回大量數據時;3)索引列不具備高選擇性時;4)複雜查詢時。通過分析查詢計劃、優化索引、避免過度索引和定期維護表,可以在實際應用中做出最優選擇。

是的,可以在 Windows 7 上安裝 MySQL,雖然微軟已停止支持 Windows 7,但 MySQL 仍兼容它。不過,安裝過程中需要注意以下幾點:下載適用於 Windows 的 MySQL 安裝程序。選擇合適的 MySQL 版本(社區版或企業版)。安裝過程中選擇適當的安裝目錄和字符集。設置 root 用戶密碼,並妥善保管。連接數據庫進行測試。注意 Windows 7 上的兼容性問題和安全性問題,建議升級到受支持的操作系統。

InnoDB的全文搜索功能非常强大,能够显著提高数据库查询效率和处理大量文本数据的能力。1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

聚集索引和非聚集索引的區別在於:1.聚集索引將數據行存儲在索引結構中,適合按主鍵查詢和範圍查詢。 2.非聚集索引存儲索引鍵值和數據行的指針,適用於非主鍵列查詢。

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

MySQL 和 MariaDB 可以共存,但需要謹慎配置。關鍵在於為每個數據庫分配不同的端口號和數據目錄,並調整內存分配和緩存大小等參數。連接池、應用程序配置和版本差異也需要考慮,需要仔細測試和規劃以避免陷阱。在資源有限的情況下,同時運行兩個數據庫可能會導致性能問題。

MySQL 數據庫中,用戶和數據庫的關係通過權限和表定義。用戶擁有用戶名和密碼,用於訪問數據庫。權限通過 GRANT 命令授予,而表由 CREATE TABLE 命令創建。要建立用戶和數據庫之間的關係,需創建數據庫、創建用戶,然後授予權限。

MySQL支持四種索引類型:B-Tree、Hash、Full-text和Spatial。 1.B-Tree索引適用於等值查找、範圍查詢和排序。 2.Hash索引適用於等值查找,但不支持範圍查詢和排序。 3.Full-text索引用於全文搜索,適合處理大量文本數據。 4.Spatial索引用於地理空間數據查詢,適用於GIS應用。
