springboot与mybatis整合的方法

这篇文章主要介绍“springboot与mybatis整合的方法”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springboot与mybatis整合的方法”文章能帮助大家解决问题。

创新互联公司是一家专注于网站建设、成都网站建设和内江机房主机托管的网络公司,有着丰富的建站经验和案例。

整合MyBatis

新建Spring Boot项目,或以Chapter1为基础来操作

pom.xml中引入依赖

  • 这里用到spring-boot-starter基础和spring-boot-starter-test用来做单元测试验证数据访问

  • 引入连接MySQL的必要依赖mysql-connector-java

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

  • 这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖

<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连接数据库一样,在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工程一样,简单且简洁的的完成了基本配置,下面看看如何在这个基础下轻松方便的使用MyBatis访问数据库。

使用MyBatis

在Mysql中创建User表,包含id(BIGINT)、name(INT)、age(VARCHAR)字段。同时,创建映射对象User

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

创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作

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

创建Spring Boot主类

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

创建单元测试

测试逻辑:插入一条name=AAA,age=20的记录,然后根据name=AAA查询,并判断age是否为20
测试结束回滚数据,保证测试单元每次运行的数据环境独立

@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整合的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。

文章名称:springboot与mybatis整合的方法
浏览路径:https://www.cdcxhl.com/article38/pgigpp.html

成都网站建设公司_创新互联,为您提供品牌网站建设面包屑导航手机网站建设虚拟主机软件开发品牌网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司