springboot下使用mybatis的方法
使用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)類中添加@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類上面添加注解@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 是查詢類的注解,所有的查詢均使用這個(gè) @Result 修飾返回的結(jié)果集,關(guān)聯(lián)實(shí)體類屬性和數(shù)據(jù)庫(kù)字段一一對(duì)應(yīng),如果實(shí)體類屬性和數(shù)據(jù)庫(kù)屬性名保持一致,就不需要這個(gè)屬性來(lái)修飾。 @Insert 插入數(shù)據(jù)庫(kù)使用,直接傳入實(shí)體類會(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>
編寫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ì)大家有所幫助!
相關(guān)文章
springboot應(yīng)用服務(wù)啟動(dòng)事件的監(jiān)聽(tīng)實(shí)現(xiàn)
本文主要介紹了springboot應(yīng)用服務(wù)啟動(dòng)事件的監(jiān)聽(tīng)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
將RestTemplate的編碼格式改為UTF-8,防止亂碼問(wèn)題
這篇文章主要介紹了將RestTemplate的編碼格式改為UTF-8,防止亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
myeclipse安裝Spring Tool Suite(STS)插件的方法步驟
這篇文章主要介紹了myeclipse安裝Spring Tool Suite(STS)插件的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
spring的xml文件打開(kāi)沒(méi)有namespace等操作選項(xiàng)的解決方案
這篇文章主要介紹了spring的xml文件打開(kāi)沒(méi)有namespace等操作選項(xiàng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
spring cloud zuul 與 sentinel的結(jié)合使用操作
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結(jié)合使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

