詳解MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn))
這篇文章我們來簡述一下MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn)方法)。
XML
1.創(chuàng)建接口
創(chuàng)建數(shù)據(jù)層(Dao)接口,使用@Mapper注解(交由Spring掌管這個類,這樣才能讓其幫助我們寫操作數(shù)據(jù)庫的方法)。為了演示我創(chuàng)建一個StudentInfoDao接口:
@Mapper
public interface StudentInfoDao {
}2.配置XML路徑
配置路徑是為了MyBatis能找到我們寫的XML文件
以下是yml的配置方法:
mybatis: mapper-locations: classpath:mapper/*Mapper*.xml #這里的*是通配符,表示模糊匹配
如果要用properties只需要把:改成.及=即可。
3.配置XML模板
我們要在對應(yīng)的文件夾下創(chuàng)建XML文件(像我上面的代碼就是放在resource文件夾的mapper文件夾內(nèi),注意,名字要和自己寫的文件名對應(yīng))如圖:

然后根據(jù)模板代碼把我們寫的類和XML匹配上,代碼如下:
<?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.mybatis.demomybatis.demos.DAO.StudentInfoDao">
在namespace的值中寫下需要匹配類的全限定類名。這樣我們整個模板就創(chuàng)建好了,我們只需要在這個XML文件中寫下我們需要的代碼即可,接下來我來教大家如何執(zhí)行簡單的SQL語句(非動態(tài)(動態(tài)的我會在以后的博客里說明))。
4.MyBatis使用SQL語句
1.insert
先在StudentInfoDao中寫一個insert方法,方法名隨意,為了方便我直接取名為insert
@Mapper
public interface StudentInfoDao {
void insert();
}然后轉(zhuǎn)換到XML文件,我們使用<insert></insert>標(biāo)簽,設(shè)置insert標(biāo)簽屬性id為我們類中對應(yīng)的方法名。在標(biāo)簽中寫下SQL語句,因為insert要傳入?yún)?shù),為了方便,我們直接傳一個類進去,MyBatis會自動幫我們把這個類轉(zhuǎn)化成類內(nèi)部的參數(shù)。我們這里傳入StudentInfo類。然后每個參數(shù)用#{參數(shù)名}的形式輸入
<insert id="insert">
insert into mybatis(user_name,password,age,gender,phone)
values(#{username},#{password},#{age},#{gender},#{phone})
</insert>@Mapper
public interface StudentInfoDao2 {
void insert(StudentInfo studentInfo);
}@Data
public class StudentInfo {
private Integer id;
private String username;
private String password;
private Integer age;
private Integer gender;
private String phone;
private String deleteFlag;
private Date createTime;
private Date updateTime;
}這里的@Data是lombok里的注解,可以自動幫我們寫getter,setter等方法,如果想要詳細了解可以去官方看看官方文檔,我們這里不多說,不是文章重點。
經(jīng)過上述步驟之后我們MyBatis使用insert語句就算是大功告成了。我們測試以下結(jié)果是否正確。
@SpringBootTest
class StudentInfoDaoTest {
@Autowired
private StudentInfoDao studentInfoDao2;
@Test
void insert() {
StudentInfo studentInfo=new StudentInfo();
studentInfo.setPassword("12321");
studentInfo.setAge(12);
studentInfo.setPhone("972893");
studentInfo.setGender(0);
studentInfo.setUsername("zm");
studentInfoDao.insert(studentInfo);
}
}

顯然我們的insert語句執(zhí)行成功。
2.update
會使用insert之后update,delete都幾乎一樣,我們這里再講一個update。和前面一樣,創(chuàng)建方法,這次我們使用<update></update>標(biāo)簽,id設(shè)置為對應(yīng)方法名,在標(biāo)簽中寫下update的sql語句。
@Mapper
public interface StudentInfoDao {
void update(StudentInfo studentInfo);
}<update id="update">
update mybatis set password=#{password},age=#{age} where user_name=#{username}
</update>@Test
void update() {
StudentInfo studentInfo=new StudentInfo();
studentInfo.setUsername("zm");
studentInfo.setAge(18);
studentInfo.setPassword("99292");
studentInfoDao2.update(studentInfo);
}
可以看到之前我們寫的數(shù)據(jù)發(fā)生了變化。
3.select
select和上述的3個SQL有所不同,insert,update,delete返回的都是整數(shù),表示數(shù)據(jù)庫受到影響的行數(shù),所以返回值肯定是Integer,這毋庸置疑。但是select就不一樣了,它的返回值可以是一行數(shù)據(jù)也可以是多行數(shù)據(jù),所以我們不僅僅要設(shè)置它的id還需要設(shè)置它的返回類型resultType。
<select id="select" resultType="com.mybatis.demomybatis.demos.model.StudentInfo">
</select>resultType中填的類是返回類的全限定類名。這個設(shè)置完之后其余的內(nèi)容和insert那三個也幾乎沒什么區(qū)別。還是創(chuàng)建方法,寫上sql語句即可。
@Mapper
public interface StudentInfoDao {
List<StudentInfo> select();//傳參和上面一樣,使用用對象即可,我們這里為了方便就不傳參了
}<select id="select" resultType="com.mybatis.demomybatis.demos.model.StudentInfo">
select * from mybatis
</select> void select() {
System.out.println(studentInfoDao.select());
}

細心的哥們兒會發(fā)現(xiàn)我們有些參數(shù)的值是null ,并不能和數(shù)據(jù)庫相匹配。這是因為一般數(shù)據(jù)庫的命名習(xí)慣和Java的不同,Java采用小駝峰,MySQL采用蛇形命名法,所以StudentInfo類的參數(shù)和MySQL中的字段名對不上,導(dǎo)致MyBatis無法映射到類的每個參數(shù)中。那我們該怎么解決這個問題呢?這里我來提供3個方法。
4.解決Mybatis參數(shù)映射不到的問題
1.改變SQL語句
我們把select * from mybatis改成select user_name as username,delete_flag as deleteFlag,create_time as createTime,uptate_time as uptatetime from mybatis即可。
2.指定映射關(guān)系
我們可以用resultMap來指定映射關(guān)系,只需要在xml任何一個位置寫一個由<resultMap></resultMap>標(biāo)簽寫的一組指定映射關(guān)系然后再由select標(biāo)簽的resultMap屬性接受即可。內(nèi)部使用<result></result>標(biāo)簽及column,property屬性形容一對映射關(guān)系。如以下代碼。
<resultMap id="StudentMap" type="com.mybatis.demomybatis.demos.model.StudentInfo">
<result column="user_name" property="username"></result>#這里我們?yōu)榱朔奖阒粚懸粋€對應(yīng)關(guān)系
</resultMap> <select id="select" resultMap="StudentMap">
select * from mybatis
</select>引用方法就是填入resultMap的id。這樣Java就可以返回正確的內(nèi)容了。
3.使用配置文件直接把對應(yīng)的蛇形命名法轉(zhuǎn)化為小駝峰
映射沒對應(yīng)上的主要原因就是命名規(guī)則不同,但是表達的內(nèi)容是相同的,所以MyBatis幫我們實現(xiàn)了命名規(guī)則自動轉(zhuǎn)化。只要使用了這個配置我們就不需要進行任何操作就可以讓類屬性和SQL字段對應(yīng)上。yml配置如下:
mybatis:
configuration: # 配置打印 MyBatis 執(zhí)行的 SQL
map-underscore-to-camel-case: true #自動駝峰轉(zhuǎn)換這個配置同樣適配于用注解方式寫SQL。那下面我們來講講注解是怎么寫SQL的吧。
注解
1.創(chuàng)建接口
和XML一樣,也得創(chuàng)建接口:
@Mapper
public interface StudentInfoDao {
}2.寫方法
因為所有方法幾乎都一樣,所以我們只講insert方法和select
insert
直接再方法上加上@Insert(“SQL語句”)即可:
@Insert("insert into mybatis(user_name,password,age,gender,phone) values(#{username},#{password},#{age}," +
"#{gender},#{phone})")
public Integer insert(StudentInfo studentInfo);非常簡單。
select
在XML中我們也說了select因為返回參數(shù)的問題所以和其他的語句有所不同,注解SQL語句上是相同的。我們要改變類和SQL字段的映射關(guān)系也有三種方法,分別是改變SQL語句,指定映射關(guān)系,使用配置文件直接把對應(yīng)的蛇形命名法轉(zhuǎn)化為小駝峰。第一種和最后一種和XML實現(xiàn)方式一樣,我們這里只講如何指定映射關(guān)系。
指定映射關(guān)系
我們使用@Results注解,在注解中是@Result注解的集合,我們在@Result中寫映射關(guān)系即可,如以下代碼:
@Select("select * from mybatis")
@Results(id = "re",value = {
@Result(column = "uptate_time",property = "updateTime"),
@Result(column = "create_time",property = "createTime"),
@Result(column = "delete_flag",property = "deleteFlag")
})
public List<StudentInfo> select();這里的id字段就是給這個result集合取個名,以后若要使用就不需要再寫這么一大串集合,直接使用@ResultMap注解,里面填上集合id即可。
@Select("select * from mybatis")
@ResultMap("id")
public List<StudentInfo> select();
因為注解真的很簡單,所以我們就只需要講這么一點,但是不是說xml復(fù)雜就是不好用,它在動態(tài)SQL方面比注解方式簡單好多好多倍,在以后的文章中我再和大家提。
總結(jié)
XML和注解的本質(zhì)都是寫一個接口,然后告訴MyBatis這個注解需要實現(xiàn)說明樣的功能,關(guān)鍵信息是什么,MyBatis就會配合Spring幫我們實現(xiàn)這個方法供我們使用,這真的比原生JDBC好用許多,這也體現(xiàn)了封裝的優(yōu)越性,讓我們省去了建立連接,關(guān)閉連接等各種重復(fù)又繁瑣的操作。
以上就是詳解MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn))的詳細內(nèi)容,更多關(guān)于MyBatis XML實現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java網(wǎng)絡(luò)編程UDP實現(xiàn)多線程在線聊天
這篇文章主要為大家詳細介紹了Java網(wǎng)絡(luò)編程UDP實現(xiàn)多線程在線聊天,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
關(guān)于BigDecimal類型數(shù)據(jù)的絕對值和相除求百分比
這篇文章主要介紹了關(guān)于BigDecimal類型數(shù)據(jù)的絕對值和相除求百分比,Java在java.math包中提供的API類BigDecimal,用來對超過16位有效位的數(shù)進行精確的運算,需要的朋友可以參考下2023-07-07
Java中使用Thread類和Runnable接口實現(xiàn)多線程的區(qū)別
這篇文章主要介紹了使用Thread類和Runnable接口實現(xiàn)多線程的區(qū)別,本文給大家介紹了兩種實現(xiàn)方式的步驟,除了以上兩種多線程實現(xiàn)方式,還可以使用 Callable 接口實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07

