MyBatis框架簡介及入門案例詳解
前言
傳統(tǒng)的JDBC操作數據庫都是通過寫一個java類,在類中調用接口下的API執(zhí)行相應的SQL,存在大量的硬編碼。試想,若是開發(fā)一個日活度高的系統(tǒng),那SQL的變動的非常大,就要我們去相應的類中修改Java代碼,特別是進行查詢操作時需要我們手動將結果集封裝到實體類中,造成后期維護壓力山大
總而言之,缺點多多
MyBatis簡介
mybatis是一個優(yōu)秀的基于java的持久層框架,它內部封裝了jdbc,使開發(fā)者只需要關注sql語句本身,而不需要花費精力去處理加載驅動、創(chuàng)建連接、創(chuàng)建statement等繁雜的過程。mybatis通過xml或注解的方式將要執(zhí)行的各種statement配置起來,并通過java對象和statement中sql的動態(tài)參數進行映射生成最終執(zhí)行的sql語句
最后mybatis框架執(zhí)行sql并將結果映射為java對象并返回。采用ORM思想解決了實體和數據庫映射的問題,對jdbc進行了封裝,屏蔽了jdbc api底層訪問細節(jié),使我們不用與jdbc api打交道,就可以完成對數據庫的持久化操作
快速入門
下面的配置文件在MyBatis的官網里都有,自己去復制過來粘貼到IDEA中更改對應的信息即可,我寫出來的目的是為了保證文章的順序流暢(大家莫煩~)
1.導入MyBatis相關坐標
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.10</version> </dependency>
2.數據庫中建立相應的數據表,并編寫一個表中字段對應的(Brand)實體類
3.編寫B(tài)randMapper映射文件
<?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="mapper.BrandMapper"> //Mapper代理開發(fā): //定義與SQL映射文件同名的Mapper接口, //并且將Mapper接口和SQL映射文件放置在同一目錄下 //設置SQL映射文件的namespace屬性為Mapper接口全限定名 //在Mapper接口中定義方法,方法名就是SQL映射文件中sql語句的id,并保持參數類型和返回值類型一致 <select id="selAll" resultType="sqlTest.Brand"> select * from mybatis; </select> </mapper>
4.編寫封裝查詢結果對象的接口
public interface BrandMapper {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> public List<Brand> selAll(); }
5編寫Mybatis核心配置文件
<?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> <typeAliases> <package name="sqlTest"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--數據庫連接信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/pyq?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="284650"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/userMapper.xml"/> <mapper resource="mapper/BrandMapper.xml"/> </mappers> </configuration>
6.編寫測試類,通過MyBatis實現查詢功能
public class MybatisDemo3 { public static void main(String[] args) throws IOException { //加載mybatis的核心配置文件,獲取sqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //獲取sqlSession工廠對象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //獲取sqlSession對象,來執(zhí)行sql SqlSession sqlSession = sqlSessionFactory.openSession(); //獲取BrandMapper接口的代理對象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); List<Brand> brands =brandMapper.selAll(); System.out.println(brands); //釋放資源 sqlSession.close(); } }
一頓操作下來好像也沒有簡單多少,其實不然,初次編寫的配置文件都是持久化的屬性,以后的變更只需要在接口里寫出相應的方法即可,避免了大量的硬編碼
映射文件
sql片段與resultMap
在進行查詢操作時,為了解決SQL表中字段名稱和實體類中的屬性名不一致導致的不能自動封裝(查出為null)的問題,我們可以通過"起別名"也可以通過SQL片段來對查詢的數據進行定義修改,統(tǒng)一重復的操作
比如,在遇到表中的字段名稱不符合實體類中駝峰命名的規(guī)范時、要對查詢的數據進行范圍限定時都可以通過sql片段來實現,就像這樣:
<sql id="cut"> bracd_name as bracdName,company_name as compantName </sql> <select id="selAll" resultType="sqlTest.Brand"> select <include refid="cut"/> from mybatis; </select>
通過SQL片段實現了所謂的范圍查詢,單表重復起別名工作的統(tǒng)一,但是針對多次不同的查詢范圍我們需要多次定義SQL片段(不靈活),這也不是一件簡單的事
于是resultMap幫我們解決了這個問題:
<resultMap id="rm" type="brand"> <result column="bracd_name" property="bracdName"/> <result column="company_name" property="companyName"/> </resultMap> <select id="selAll" resultMap="rm"> select * from mybatis; </select>
通過id來設置唯一標識,type來指定映射類型,把表的列名column和實體類的屬性名通過result進行映射,在我們的SQL中直接查詢所有即可,進行不同的查詢操作也不需要重復定義片段或者取別名,就變得十分簡單
MyBatis的增刪改查
1.添加操作
編寫Mapper接口,寫出添加的方法:
void add(Brand brand);
配置SQL映射文件:
<mapper> <insert id="add" > insert into mybatis(bracd_name,company_name,ordered,description,status) values (#{bracdName},#{companyName},#{ordered},#{description},#{status}); </insert> </mapper>
執(zhí)行方法:
SqlSession sqlSession = sqlSessionFactory.openSession(); BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); brandMapper.add(brand);
2.修改操作
編寫Mapper接口,寫出修改的方法:
void update(Brand brand);
編寫映射文件:
<update id="update" parameterType="sqlTest.Brand"> update mybatis set bracd_name=#{bracdName},company_name=#{companyName},ordered=#{ordered},description=#{description},status=#{status} </update>
執(zhí)行方法:
SqlSession sqlSession = sqlSessionFactory.openSession(); BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); int count= brandMapper.update(brand);
3.刪除操作
編寫Mapper接口,寫出刪除方法:
void deleteid(Brand brand);
編寫映射文件:
<delete id="deleteid" parameterType="brand"> delete from mybatis where id=#{id} </delete>
執(zhí)行方法:
SqlSession sqlSession = sqlSessionFactory.openSession(); BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); int count= brandMapper.deleteid(id);
以上就是對MyBtias的初識,后面還有很多高級功能等待整理
到此這篇關于MyBatisPlus框架簡介及入門案例詳解的文章就介紹到這了,更多相關MyBatisPlus案例內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot Shiro 權限注解不起作用的解決方法
本文主要介紹了SpringBoot Shiro 權限注解不起作用的解決方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07SpringBoot中mapper.xml文件存放的兩種實現位置
這篇文章主要介紹了SpringBoot中mapper.xml文件存放的兩種實現位置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01