成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

springboot與mybatis整合的方法

這篇文章主要介紹“springboot與mybatis整合的方法”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“springboot與mybatis整合的方法”文章能幫助大家解決問(wèn)題。

創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)和內(nèi)江機(jī)房主機(jī)托管的網(wǎng)絡(luò)公司,有著豐富的建站經(jīng)驗(yàn)和案例。

整合MyBatis

新建Spring Boot項(xiàng)目,或以Chapter1為基礎(chǔ)來(lái)操作

pom.xml中引入依賴

  • 這里用到spring-boot-starter基礎(chǔ)和spring-boot-starter-test用來(lái)做單元測(cè)試驗(yàn)證數(shù)據(jù)訪問(wèn)

  • 引入連接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ù)庫(kù)一樣,在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工程一樣,簡(jiǎn)單且簡(jiǎn)潔的的完成了基本配置,下面看看如何在這個(gè)基礎(chǔ)下輕松方便的使用MyBatis訪問(wèn)數(shù)據(jù)庫(kù)。

使用MyBatis

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

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

創(chuàng)建User映射的操作UserMapper,為了后續(xù)單元測(cè)試驗(yàn)證,實(shí)現(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)建單元測(cè)試

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

@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());
 }
}

關(guān)于“springboot與mybatis整合的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

文章名稱:springboot與mybatis整合的方法
瀏覽路徑:http://jinyejixie.com/article38/pgigpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)、軟件開(kāi)發(fā)品牌網(wǎng)站制作

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
兰西县| 绵阳市| 桦川县| 特克斯县| 仪陇县| 大厂| 大厂| 澄江县| 太白县| 惠州市| 桃江县| 瑞金市| 公主岭市| 葵青区| 邹平县| 呼玛县| 印江| 息烽县| 东台市| 潞西市| 翼城县| 新津县| 通道| 海门市| 夹江县| 塔城市| 虞城县| 绵竹市| 江永县| 大英县| 雅江县| 马尔康县| 藁城市| 滦平县| 互助| 克拉玛依市| 新宁县| 曲松县| 东安县| 龙泉市| 阿合奇县|