日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis学习之配置优化

發布時間:2025/3/21 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis学习之配置优化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言
接上章學習,我們配置build了之后,成功解決了資源導出失敗的問題,這章我們學習的是配置優化

一、原來的配置
1.數據庫配置(屬性優化)
原先寫死的數據庫核心配置

<!--核心配置文件--> <configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="xxxxxxxx"/><property name="url" value="xxxxxxxxxxxxxxxxxxxxxx"/><property name="username" value="xxxxxx"/><property name="password" value="xxxxxx"/></dataSource></environment></environments><!--每個mapper.xml都需要在Mybatis核心配置文件中注冊--><mappers><mapper resource="com/shan/dao/UserMapper.xml"/></mappers> </configuration>

2.別名優化

<select id="getUserById" resultType="com.shan.pojo.User" parameterType="int">select * from mybatis.user where id=#{id} </select>

3.配置映射器優化
(1).使用對應資源進行注冊(推薦)

<!--每個mapper.xml都需要在Mybatis核心配置文件中注冊--><mappers><mapper resource="com/shan/dao/UserMapper.xml"/></mappers>

(2).使用類注冊

<mappers><mapper class="com.shan.dao.UserMapper"/> </mappers>

(3).使用包注冊

<mappers><package name="com.shan.dao"/> </mappers>

二、需要的配置文件以及配置優化后的效果
1.數據庫配置優化(配置屬性優化)
(1)db.properties數據庫配置文件

driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/xxxxxxxxxxxxx username = xxxxx password = xxxxxxxxxx

(2)數據庫配置優化

<!--引入外部配置文件,優先使用外部配置的--><properties resource="db.properties"><property name="username" value="xxxx"/><!--這里密碼錯誤,外部密碼正確可以正常使用;外部密碼錯誤,這里密碼正確無法使用--><property name="password" value="xxxxxxxxx"/></properties><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments>

2.別名優化

<!--可以給實體類起別名,掃描包的默認為這個類的首字母小寫的類名--><typeAliases><package name="com.shan.pojo"/></typeAliases>

效果:

<!--默認為這個類的首字母小寫的類名,當然用User也是可行的,但是我們要遵守規范--> <select id="getUserList" resultType="user">select * from mybatis.user</select>

3.映射器優化
(1).資源對應注冊
簡單方便好整理
(2).類注冊
注意事項:

  • 接口和它Mapper的配置文件必須同名
  • 接口和它Mapper的配置文件必須在同一個包下
    (3).包注冊
    注意事項:
  • 接口和它Mapper的配置文件必須同名
  • 接口和它Mapper的配置文件必須在同一個包下

總結

以上是生活随笔為你收集整理的Mybatis学习之配置优化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。