这篇文章将为大家详细讲解有关怎么在Spring Boot中使用MyBatis,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动态sql的mybatis,两者各有特点,在企业级系统开发中可以根据需求灵活使用。发现一个有趣的现象:传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis。
hibernate特点就是所有的sql都用Java代码来生成,不用跳出程序去写(看)sql,有着编程的完整性,发展到最顶端就是spring data jpa这种模式了,基本上根据方法名就可以生成对应的sql了。
mybatis初期使用比较麻烦,需要各种配置文件、实体类、dao层映射关联、还有一大推其它配置。当然mybatis也发现了这种弊端,初期开发了generator可以根据表结果自动生产实体类、配置文件和dao层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理dao层和配置文件等,发展到最顶端就是今天要讲的这种模式了,mybatis-spring-boot-starter就是springboot+mybatis可以完全注解不用配置文件,也可以简单配置轻松上手。
mybatis-spring-boot-starter
官方说明:MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot
其实就是myBatis看spring boot这么火热也开发出一套解决方案来凑凑热闹,但这一凑确实解决了很多问题,使用起来确实顺畅了许多。mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解解决一切问题,一种是简化后的老传统。
当然任何模式都需要首先引入mybatis-spring-boot-starter的pom文件,现在最新版本是1.1.1
org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1
无配置文件注解版
就是一切使用注解搞定。
实在是不喜欢把sql用注解的形式写在java类中的形式,所以就忽略吧...
下面讨论一下xml版本
1 添加相关maven依赖
4.0.0 com.example spring-boot-mybatis 0.0.1-SNAPSHOT jar spring-boot-mybatis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin true
2.在applications.properties配置文件中添加MySQL的配置
spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = root spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10
Spring boot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了。
3.编写测试实体类DemoInfo
package com.winner.po;
public class DemoInfo {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}4.编写Mapper接口
package com.winner.mapper;
import com.winner.po.DemoInfo;
public interface DemoInfoMapper {
DemoInfo queryById(Integer id);
}在启动类中使用@MapperScan注解扫描mapper接口
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.winner.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}5.编写配置文件
DemoInfoMapper.xml
id, name, password
mybatis-config.xml
注意,需要在application.properties新增以下配置
mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
指定了mybatis基础配置文件和实体类映射文件的地址
6.编写DemoInfoService
@Service
public class DemoInfoService {
@Resource
private DemoInfoMapper demoInfoMapper;
DemoInfo queryById(Integer id){
DemoInfo demoInfo = demoInfoMapper.queryById(id);
return demoInfo;
}
}7.编写DemoInfoController
@RestController
public class DemoInfoController {
@Resource
private DemoInfoService demoInfoService;
@RequestMapping("/{id}")
DemoInfo queryById(@PathVariable Integer id){
DemoInfo demoInfo = demoInfoService.queryById(id);
return demoInfo;
}
}运行访问:http://127.0.0.1:8080/1,运行结果:
{"id":1,"name":"zhangsan","password":"123456"}关于怎么在Spring Boot中使用MyBatis就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网站名称:怎么在SpringBoot中使用MyBatis-创新互联
网站路径:http://www.scyingshan.cn/article/codois.html


咨询
建站咨询
