SpringBoot (四) :優(yōu)雅的使用 mybatis

由于項(xiàng)目中使用mybatis比較多,并且mybatis這種半orm形式的持久層框簡(jiǎn)單又不失可控性,所以這一章簡(jiǎn)單講一下springboot與mybatis的集成。

mybatis-spring-boot-starter

官方說(shuō)明:MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot
Springboot整合mybatis主要有兩種方案,一種是使用注解解決,另一種是簡(jiǎn)化后的傳統(tǒng)方式。
當(dāng)然兩種方式首先都得添加pom依賴(lài),這是必不可少的

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

無(wú)配置文件注解

即所有問(wèn)題都通過(guò)注解解決,這也是我喜歡的方式

1、添加pom文件

<!-- mybatis相關(guān)-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

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

mybatis.mapperLocations=classpath*:/mapper/*.xml

在啟動(dòng)類(lèi)中添加對(duì)mapper包掃描@MapperScan。

@MapperScan(basePackages = {"com.liangliang.lessons.mapper"})
@SpringBootApplication
public class LessonsApplication {

    public static void main(String[] args) {
        SpringApplication.run(LessonsApplication.class, args);
    }
}

3、開(kāi)發(fā)mapper

@Mapper
public interface UserMapper {
    List<UserEntity> getAll();

    UserEntity getOne(Long id);

    void insert(UserEntity user);

    void update(UserEntity user);

    void delete(Long id);
}

為了更接近生產(chǎn)我特地將user_sex、nick_name兩個(gè)屬性在數(shù)據(jù)庫(kù)加了下劃線(xiàn)和實(shí)體類(lèi)屬性名不一致,另外user_sex使用了枚舉。

4、使用

上面三步就基本完成了相關(guān)dao層開(kāi)發(fā),使用的時(shí)候當(dāng)作普通的類(lèi)注入進(jìn)入就可以了。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Autowired
    UserMapper userMapper;
    @Test
    public void getAll() throws Exception {
        System.out.println("----------userMapper:"+userMapper);
        userMapper.getOne(1l);
    }

    @Test
    public void getOne() throws Exception {
    }

    @Test
    public void insert() throws Exception {
    }

    @Test
    public void update() throws Exception {
    }

    @Test
    public void delete() throws Exception {
    }

}

到此,單元測(cè)試完成,controller中寫(xiě)法在代碼中有詳細(xì)的注解,直接使用即可,對(duì)于另一種在mapper中寫(xiě)sql注解的方式,這里不做說(shuō)明,這種做法對(duì)代碼侵入性太高,不建議使用,網(wǎng)上也有相應(yīng)教程,感興趣的小伙伴可以自行寫(xiě)。
同樣代碼地址在
https://github.com/liangliang1259/daily-lessons.git 中的項(xiàng)目lessons-4

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,283評(píng)論 6 342
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,677評(píng)論 19 139
  • 這兩天啟動(dòng)了一個(gè)新項(xiàng)目因?yàn)轫?xiàng)目組成員一直都使用的是mybatis,雖然個(gè)人比較喜歡jpa這種極簡(jiǎn)的模式,但是為了項(xiàng)...
    極樂(lè)君閱讀 1,382評(píng)論 2 26
  • Spring 技術(shù)筆記Day 1 預(yù)熱知識(shí)一、 基本術(shù)語(yǔ)Blob類(lèi)型,二進(jìn)制對(duì)象Object Graph:對(duì)象圖...
    OchardBird閱讀 1,080評(píng)論 0 2
  • 勇者不懼,智者不惑 ——記薪火傳承工程主講周士忠老師 10月9日晚,我校薪火傳承工程進(jìn)行時(shí)!本次活動(dòng)由我校周士忠老...
    煜苅千歌閱讀 510評(píng)論 0 0

友情鏈接更多精彩內(nèi)容