springboot下使用mybatis的方法
使用mybatis-spring-boot-starter即可。 簡單來說就是mybatis看見spring boot這么火,于是搞出來mybatis-spring-boot-starter這個解決方案來與springboot更好的集成
詳見
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會自動加載spring.datasource.*相關(guān)配置,數(shù)據(jù)源就會自動注入到sqlSessionFactory中,sqlSessionFactory會自動注入到Mapper中,對了你一切都不用管了,直接拿起來使用就行了。
mybatis.type-aliases-package=com.test.demo.model
這個配置用來指定bean在哪個包里,避免存在同名class時找不到bean
在啟動類中添加@MapperScan指定dao或者mapper包的位置,可以用 {"",""}的形式指定多個包
@SpringBootApplication
@MapperScan("com.test.demo.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
或者直接在Mapper類上面添加注解@Mapper也可以指定mapper,建議使用上面這種,給每個mapper加個注解挺麻煩不說,如果是dao的包,還是要用@MapperScan來指定位置
接下來,可以用注解模式開發(fā)mapper,或者用xml模式開發(fā)
注解模式
@Mapper
public interface CityMapper {
@Select("select * from city where state = #{state}")
City findByState(@Param("state") String state);
}
@Select 是查詢類的注解,所有的查詢均使用這個 @Result 修飾返回的結(jié)果集,關(guān)聯(lián)實體類屬性和數(shù)據(jù)庫字段一一對應(yīng),如果實體類屬性和數(shù)據(jù)庫屬性名保持一致,就不需要這個屬性來修飾。 @Insert 插入數(shù)據(jù)庫使用,直接傳入實體類會自動解析屬性到對應(yīng)的值 @Update 負(fù)責(zé)修改,也可以直接傳入對象 @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);
}
對應(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的方法,希望對大家有所幫助!
相關(guān)文章
springboot應(yīng)用服務(wù)啟動事件的監(jiān)聽實現(xiàn)
本文主要介紹了springboot應(yīng)用服務(wù)啟動事件的監(jiān)聽實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
將RestTemplate的編碼格式改為UTF-8,防止亂碼問題
這篇文章主要介紹了將RestTemplate的編碼格式改為UTF-8,防止亂碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
myeclipse安裝Spring Tool Suite(STS)插件的方法步驟
這篇文章主要介紹了myeclipse安裝Spring Tool Suite(STS)插件的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
spring的xml文件打開沒有namespace等操作選項的解決方案
這篇文章主要介紹了spring的xml文件打開沒有namespace等操作選項的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
spring cloud zuul 與 sentinel的結(jié)合使用操作
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結(jié)合使用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

