内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

springboot與mybatis整合的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹了springboot與mybatis整合的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的高陵網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

整合MyBatis

新建Spring Boot項目,或以Chapter1為基礎(chǔ)來操作

pom.xml中引入依賴

  • 這里用到spring-boot-starter基礎(chǔ)和spring-boot-starter-test用來做單元測試驗證數(shù)據(jù)訪問

  • 引入連接mysql的必要依賴mysql-connector-java

  • 引入整合MyBatis的核心依賴mybatis-spring-boot-starter

  • 這里不引入spring-boot-starter-jdbc依賴,是由于mybatis-spring-boot-starter中已經(jīng)包含了此依賴

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.3.2.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.1.1</version>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.21</version>
 </dependency>
</dependencies>

同之前介紹的使用jdbc和spring-data連接數(shù)據(jù)庫一樣,在application.properties中配置mysql的連接配置

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

同其他Spring Boot工程一樣,簡單且簡潔的的完成了基本配置,下面看看如何在這個基礎(chǔ)下輕松方便的使用MyBatis訪問數(shù)據(jù)庫。

使用MyBatis

在Mysql中創(chuàng)建User表,包含id(BIGINT)、name(INT)、age(VARCHAR)字段。同時,創(chuàng)建映射對象User

public class User {
  private Long id;
  private String name;
  private Integer age;
  // 省略getter和setter
}

創(chuàng)建User映射的操作UserMapper,為了后續(xù)單元測試驗證,實現(xiàn)插入和查詢操作

@Mapper
public interface UserMapper {
  @Select("SELECT * FROM USER WHERE NAME = #{name}")
  User findByName(@Param("name") String name);
  @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
  int insert(@Param("name") String name, @Param("age") Integer age);
}

創(chuàng)建Spring Boot主類

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

創(chuàng)建單元測試

測試邏輯:插入一條name=AAA,age=20的記錄,然后根據(jù)name=AAA查詢,并判斷age是否為20
測試結(jié)束回滾數(shù)據(jù),保證測試單元每次運行的數(shù)據(jù)環(huán)境獨立

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
 @Autowired
 private UserMapper userMapper;
 @Test
 @Rollback
 public void findByName() throws Exception {
 userMapper.insert("AAA", 20);
 User u = userMapper.findByName("AAA");
 Assert.assertEquals(20, u.getAge().intValue());
 }
}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“springboot與mybatis整合的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)頁題目:springboot與mybatis整合的示例分析-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://www.rwnh.cn/article18/csjdgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、網(wǎng)站排名、網(wǎng)站收錄、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
郁南县| 江永县| 绵竹市| 云霄县| 涿鹿县| 南丹县| 华蓥市| 新民市| 宜春市| 奇台县| 三亚市| 江陵县| 泰州市| 大竹县| 久治县| 武邑县| 饶平县| 皮山县| 平南县| 金华市| 安龙县| 惠东县| 陇西县| 和平区| 麦盖提县| 同德县| 拜泉县| 清新县| 远安县| 广河县| 孟津县| 中宁县| 清丰县| 盐池县| 丹江口市| 定日县| 平和县| 莲花县| 甘孜| 铁岭市| 台前县|