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

springboot與mybatis整合實(shí)例詳解

 更新時(shí)間:2017年11月15日 10:34:18   作者:翟永超  
這篇文章主要為大家詳細(xì)介紹了springboot與mybatis整合實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近項(xiàng)目原因可能會繼續(xù)開始使用MyBatis,已經(jīng)習(xí)慣于spring-data的風(fēng)格,再回頭看xml的映射配置總覺得不是特別舒服,接口定義與映射離散在不同文件中,使得閱讀起來并不是特別方便。

Spring中整合MyBatis就不多說了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步驟。搜了一下Spring Boot整合MyBatis的文章,方法都比較老,比較繁瑣。查了一下文檔,實(shí)際已經(jīng)支持較為簡單的整合與使用。下面就來詳細(xì)介紹如何在Spring Boot中整合MyBatis,并通過注解方式實(shí)現(xiàn)映射。

整合MyBatis

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

pom.xml中引入依賴

  • 這里用到spring-boot-starter基礎(chǔ)和spring-boot-starter-test用來做單元測試驗(yàn)證數(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工程一樣,簡單且簡潔的的完成了基本配置,下面看看如何在這個(gè)基礎(chǔ)下輕松方便的使用MyBatis訪問數(shù)據(jù)庫。

使用MyBatis

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

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

創(chuàng)建User映射的操作UserMapper,為了后續(xù)單元測試驗(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)建單元測試

測試邏輯:插入一條name=AAA,age=20的記錄,然后根據(jù)name=AAA查詢,并判斷age是否為20
測試結(jié)束回滾數(shù)據(jù),保證測試單元每次運(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());
 }
}

完整示例Chapter3-2-7

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

相關(guān)文章

最新評論