详解Spring Boot中MyBatis的使用方法
orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动态sql的mybatis,两者各有特点,在企业级系统开发中可以根据需求灵活使用。发现一个有趣的现象:传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis。
hibernate特点就是所有的sql都用Java代码来生成,不用跳出程序去写(看)sql,有着编程的完整性,发展到最顶端就是springdatajpa这种模式了,基本上根据方法名就可以生成对应的sql了。
mybatis初期使用比较麻烦,需要各种配置文件、实体类、dao层映射关联、还有一大推其它配置。当然mybatis也发现了这种弊端,初期开发了generator可以根据表结果自动生产实体类、配置文件和dao层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理dao层和配置文件等,发展到最顶端就是今天要讲的这种模式了,mybatis-spring-boot-starter就是springboot+mybatis可以完全注解不用配置文件,也可以简单配置轻松上手。
mybatis-spring-boot-starter
官方说明:MyBatisSpring-Boot-StarterwillhelpyouuseMyBatiswithSpringBoot
其实就是myBatis看springboot这么火热也开发出一套解决方案来凑凑热闹,但这一凑确实解决了很多问题,使用起来确实顺畅了许多。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 DemoprojectforSpringBoot 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
Springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了。
3.编写测试实体类DemoInfo
packagecom.winner.po; publicclassDemoInfo{ privateIntegerid; privateStringname; privateStringpassword; publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetPassword(){ returnpassword; } publicvoidsetPassword(Stringpassword){ this.password=password; } }
4.编写Mapper接口
packagecom.winner.mapper; importcom.winner.po.DemoInfo; publicinterfaceDemoInfoMapper{ DemoInfoqueryById(Integerid); }
在启动类中使用@MapperScan注解扫描mapper接口
importorg.mybatis.spring.annotation.MapperScan; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.winner.mapper") publicclassSpringBootMybatisApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(SpringBootMybatisApplication.class,args); } }
5.编写配置文件
DemoInfoMapper.xml
id,name,password select fromdemo_info whereid=#{id}
mybatis-config.xml
注意,需要在application.properties新增以下配置
mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
指定了mybatis基础配置文件和实体类映射文件的地址
6.编写DemoInfoService
@Service publicclassDemoInfoService{ @Resource privateDemoInfoMapperdemoInfoMapper; DemoInfoqueryById(Integerid){ DemoInfodemoInfo=demoInfoMapper.queryById(id); returndemoInfo; } }
7.编写DemoInfoController
@RestController publicclassDemoInfoController{ @Resource privateDemoInfoServicedemoInfoService; @RequestMapping("/{id}") DemoInfoqueryById(@PathVariableIntegerid){ DemoInfodemoInfo=demoInfoService.queryById(id); returndemoInfo; } }
运行访问:http://127.0.0.1:8080/1,运行结果:
{"id":1,"name":"zhangsan","password":"123456"}
总结
以上所述是小编给大家介绍的SpringBoot中MyBatis的使用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!