溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

springboot下使用mybatis的方法

發(fā)布時(shí)間:2020-08-24 15:00:08 來(lái)源:腳本之家 閱讀:145 作者:mrr 欄目:編程語(yǔ)言

使用mybatis-spring-boot-starter即可。 簡(jiǎn)單來(lái)說(shuō)就是mybatis看見(jiàn)spring boot這么火,于是搞出來(lái)mybatis-spring-boot-starter這個(gè)解決方案來(lái)與springboot更好的集成

詳見(jiàn)

http://www.mybatis.org/spring/zh/index.html

引入mybatis-spring-boot-starter的pom文件

<dependency>  
  <groupId>org.mybatis.spring.boot</groupId>  
  <artifactId>mybatis-spring-boot-starter</artifactId>  
  <version>1.1.1</version>  
</dependency>

application.properties 添加相關(guān)配置

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = mysql

springboot會(huì)自動(dòng)加載spring.datasource.*相關(guān)配置,數(shù)據(jù)源就會(huì)自動(dòng)注入到sqlSessionFactory中,sqlSessionFactory會(huì)自動(dòng)注入到Mapper中,對(duì)了你一切都不用管了,直接拿起來(lái)使用就行了。

mybatis.type-aliases-package=com.test.demo.model

這個(gè)配置用來(lái)指定bean在哪個(gè)包里,避免存在同名class時(shí)找不到bean

在啟動(dòng)類(lèi)中添加@MapperScan指定dao或者mapper包的位置,可以用 {"",""}的形式指定多個(gè)包

@SpringBootApplication
@MapperScan("com.test.demo.dao")
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

或者直接在Mapper類(lèi)上面添加注解@Mapper也可以指定mapper,建議使用上面這種,給每個(gè)mapper加個(gè)注解挺麻煩不說(shuō),如果是dao的包,還是要用@MapperScan來(lái)指定位置

接下來(lái),可以用注解模式開(kāi)發(fā)mapper,或者用xml模式開(kāi)發(fā)

注解模式

@Mapper
public interface CityMapper {
  @Select("select * from city where state = #{state}")
  City findByState(@Param("state") String state);
}

@Select 是查詢(xún)類(lèi)的注解,所有的查詢(xún)均使用這個(gè) @Result 修飾返回的結(jié)果集,關(guān)聯(lián)實(shí)體類(lèi)屬性和數(shù)據(jù)庫(kù)字段一一對(duì)應(yīng),如果實(shí)體類(lèi)屬性和數(shù)據(jù)庫(kù)屬性名保持一致,就不需要這個(gè)屬性來(lái)修飾。 @Insert 插入數(shù)據(jù)庫(kù)使用,直接傳入實(shí)體類(lèi)會(huì)自動(dòng)解析屬性到對(duì)應(yīng)的值 @Update 負(fù)責(zé)修改,也可以直接傳入對(duì)象 @delete 負(fù)責(zé)刪除 了解更多注解參考這里

http://www.mybatis.org/mybatis-3/zh/java-api.html

xml模式

xml模式保持映射文件的老傳統(tǒng),application.properties需要新增

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

指定mybatis的映射xml文件位置 此外,還可以指定mybatis的配置文件,如果需要增加mybatis的一些基礎(chǔ)配置,可以增加下面的配置

mybatis.config-locations=classpath:mybatis/mybatis-config.xml

指定mybatis基礎(chǔ)配置文件

mybatis-config.xml可以添加一些mybatis基礎(chǔ)的配置,例如

<configuration>
  <typeAliases>
    <typeAlias alias="Integer" type="java.lang.Integer" />
    <typeAlias alias="Long" type="java.lang.Long" />
    <typeAlias alias="HashMap" type="java.util.HashMap" />
    <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
    <typeAlias alias="ArrayList" type="java.util.ArrayList" />
    <typeAlias alias="LinkedList" type="java.util.LinkedList" />
  </typeAliases>
</configuration>

編寫(xiě)Dao層的代碼

public interface CityDao {
  public City selectCityByState(String State);
}

對(duì)應(yīng)的xml映射文件

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.demo.dao.CityDao">
  <select id="selectCityByState" parameterType="String" resultType="City">
    select * from city where state = #{state}
  </select></mapper>

總結(jié)

以上所述是小編給大家介紹的springboot下使用mybatis的方法,希望對(duì)大家有所幫助!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI