欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解SpringBoot 快速整合MyBatis(去XML化)

 更新時間:2017年10月09日 10:40:48   作者:yizhiwazi  
本篇文章主要介紹了詳解SpringBoot 快速整合MyBatis(去XML化),非常具有實用價值,需要的朋友可以參考下

序言:

此前,我們主要通過XML來書寫SQL和填補(bǔ)對象映射關(guān)系。在SpringBoot中我們可以通過注解來快速編寫SQL并實現(xiàn)數(shù)據(jù)訪問。(僅需配置:mybatis.configuration.map-underscore-to-camel-case=true)。為了方便大家,本案例提供較完整的層次邏輯SpringBoot+MyBatis+Annotation

具體步驟

1. 引入依賴

在pom.xml 引入ORM框架(Mybaits-Starter)和數(shù)據(jù)庫驅(qū)動(MySQL-Conn)的依賴。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <!--繼承信息 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M4</version>
    <relativePath/>
  </parent>

  <!--依賴管理 -->
  <dependencies>
    <dependency> <!--添加Web依賴 -->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency> <!--添加Mybatis依賴 -->
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency><!--添加MySQL驅(qū)動依賴 -->
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency><!--添加Test依賴 -->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

2. 添加數(shù)據(jù)源

在application.yml 添加數(shù)據(jù)源,以及開啟Mybaits的駝峰映射功能。

spring:
 datasource:
  url: jdbc:mysql://localhost:3306/socks?useSSL=false
  username: root
  password: root
  driver-class-name: com.mysql.jdbc.Driver

mybatis:
 configuration:
  map-underscore-to-camel-case: true #開啟駝峰映射

3. 編寫數(shù)據(jù)層代碼

// POJO類如下:
public class User {
  private String userId;
  private String username;
  private String password;
  // Getters & Setters ..
}
// 數(shù)據(jù)層代碼如下:
public interface UserMapper {

  @Select("select * from t_user where 1=1")
  List<User> list();

  @Select("select * from t_user where username like #{username}")
  List<User> findByUsername(String username);

  @Select("select * from t_user where user_id like #{userId}")
  User getOne(String userId);

  @Delete("delete from t_user where user_id like #{userId}")
  int delete(String userId);
}

4. 添加數(shù)據(jù)庫記錄

在Navicat 連接本地數(shù)據(jù)庫,隨便打開查詢窗口,復(fù)制下面這段腳本,點擊執(zhí)行即可。

DROP DATABASE IF EXISTS `socks`;
CREATE DATABASE `socks`;
USE `SOCKS`;
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
 `USER_ID` varchar(50) ,
 `USERNAME` varchar(50) ,
 `PASSWORD` varchar(50) 
) ;

INSERT INTO `t_user` VALUES ('1', 'admin', 'admin');
INSERT INTO `t_user` VALUES ('2', 'yizhiwazi', '123456');

5. 啟動項目

@SpringBootApplication
@MapperScan("com.hehe.mapper") //掃描Mapper接口
public class MybatisApplication {

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

6. 單元測試

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisApplicationTest {

  @SuppressWarnings("all")
  @Autowired
  UserMapper userMapper;

  @Test
  public void test_db() {
    //開始進(jìn)行測試
    assertThat(userMapper.list().size()).isGreaterThan(1);
    assertThat(userMapper.getOne("1")).isNotEqualTo(null);
    assertThat(userMapper.getOne("xxx")).isEqualTo(null);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring的13個經(jīng)典面試題

    spring的13個經(jīng)典面試題

    Spring框架是一個開放源代碼的J2EE應(yīng)用程序框架,是針對bean的生命周期進(jìn)行管理的輕量級容Spring解決了開發(fā)者在J2EE開發(fā)中遇到的許多常見的問題,我們這篇文章就來了解一下spring的面試題
    2021-06-06
  • SpringCloud OpenFeign Post請求400錯誤解決方案

    SpringCloud OpenFeign Post請求400錯誤解決方案

    這篇文章主要介紹了SpringCloud OpenFeign Post請求400錯誤解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • SpringMVC?RESTFul實現(xiàn)列表功能

    SpringMVC?RESTFul實現(xiàn)列表功能

    這篇文章主要為大家介紹了SpringMVC?RESTFul實現(xiàn)列表功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 解決java.lang.IllegalArgumentException異常問題

    解決java.lang.IllegalArgumentException異常問題

    這篇文章主要介紹了解決java.lang.IllegalArgumentException異常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Spring Boot部署到Tomcat過程中遇到的問題匯總

    Spring Boot部署到Tomcat過程中遇到的問題匯總

    這篇文章主要給大家分享了關(guān)于Spring Boot部署到Tomcat過程中遇到的一些問題,文中將解決的方法介紹非常詳細(xì),對同樣遇到這個問題的朋友具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 全網(wǎng)最詳細(xì)Hutool工具詳解

    全網(wǎng)最詳細(xì)Hutool工具詳解

    Hutool的目標(biāo)是使用一個工具方法代替一段復(fù)雜代碼,從而最大限度的避免“復(fù)制粘貼”代碼的問題,徹底改變我們寫代碼的方式。這篇文章主要介紹了全文最詳細(xì)Hutool工具詳解,需要的朋友可以參考下
    2021-12-12
  • UrlDecoder和UrlEncoder使用詳解_動力節(jié)點Java學(xué)院整理

    UrlDecoder和UrlEncoder使用詳解_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了UrlDecoder和UrlEncoder使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • MySql多表查詢 事務(wù)及DCL

    MySql多表查詢 事務(wù)及DCL

    這篇文章主要介紹了MySql多表查詢 、事務(wù)、DCL的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • Java?方法的定義與調(diào)用詳解

    Java?方法的定義與調(diào)用詳解

    在java中,方法就是用來完成解決某件事情或?qū)崿F(xiàn)某個功能的辦法。方法實現(xiàn)的過程中,會包含很多條語句用于完成某些有意義的功能——通常是處理文本,控制輸入或計算數(shù)值,這篇文章我們來探究一下方法的定義與調(diào)用
    2022-04-04
  • JAVA對象分析之偏向鎖、輕量級鎖、重量級鎖升級過程

    JAVA對象分析之偏向鎖、輕量級鎖、重量級鎖升級過程

    這篇文章主要介紹了JAVA對象分析之偏向鎖、輕量級鎖、重量級鎖升級過程,又對這方面感興趣的同學(xué)可以跟著一起研究下
    2021-02-02

最新評論