springboot+jdbctemplate方式/spingboot+mybatis方式与0行代码写服务的差异,不看后悔死
视频晚上录
- 接口工作量对比(这里只对比最精简模式)
- 1.springboot+jdbctemplate
- 1.springboot+mybatis
- 0行代码写服务
- 开发环境搭建
- 演示项目技术点
- Springboot项目gradle构建
- Springboot+jdbctemplate集成
- Springboot+mybatis集成
- Swagger集成
- 创建商品表
- 分别用以上两种方式编写接口
- springboot+jdbctemplate方式
- 1. 创建实体(省略set get方法)
- 3. 创建控制层
- 4. 接口测试调用
- spingboot+mybatis方式
- 1. 创建实体(省略此处和上面一样可以共用实体类)
- 2. 创建dao,这里的dao是一个接口和上面实现有点区别
- 3.创建控制层,这里也和上面一样,为了区别两个不同
- 4.配置mybatis配置mapper
- 5.配置mybatis application.yml
- 6.配置mybatis spingboot启动扫描
- 7.配置mybatis+spingboot接口测试
- springboot+jdbctemplate方式
- 用0行代码写服务实现以上接口进行对比
-
- 1.创建sql文件
-
之前看过我视频教程的同学应该已经知道了,视频今日头条更新,搜索0行代码写服务即可找到。
下面我们以最简单的方式对比一下这几个框架;
接口工作量对比(这里只对比最精简模式)
1.springboot+jdbctemplate
代码量较少。简单。维护不易,表结构变化需要重新编码打包,代码验证需要自己封装。新增接口不方便。
1.springboot+mybatis
代码量较多。相对比较复杂。表结构变化需要重新编码打包,代码验证需要自己封装。新增接口不方便。
0行代码写服务
没代码。更简单,需要会写sql即可完成简单的接口编写。表结构变化不需要重新编码打包,所有参数可以进行多维度验证。新增接口、接口变化不需要改动代码重新发布、自带分页。
开发环境搭建
1.eclipse2.java83.toncat9
演示项目技术点
1.Springboot集成swagger2.Springboot集成jdbctemplate3.Springboot集成mybatis4.Springboot跨域设置5.gradle构建6.restful数据接口
Springboot项目gradle构建
首先进入springboot官网:https://spring.io/quickstart
- 打开project
- 选择spring initialiazr
- 在打开的页面选择下图中的关键构建项目,然后在右边选择依赖
- 项目初始化完成直接下载
- 需要的依赖最终生成gradle文件如下:
plugins {id ‘org.springframework.boot’ version ‘2.2.7.RELEASE’id ‘io.spring.dependency-management’ version ‘1.0.9.RELEASE’id ‘java’}
group = ‘com.example’version = ‘0.0.1-SNAPSHOT’sourceCompatibility = ‘1.8’
configurations {developmentOnlyruntimeClasspath {extendsFrom developmentOnly}compileOnly {extendsFrom annotationProcessor}}
repositories {#这个地方一定要先改成下面的国内阿里云镜像地址,然后再导入项目,否则下载依赖的jar能把你耗死maven {url ‘http://maven.aliyun.com/nexus/content/groups/public/’}maven {url ‘http://maven.aliyun.com/nexus/content/repositories/jcenter’}mavenCentral()}
dependencies {implementation ‘org.springframework.boot:spring-boot-starter-jdbc’implementation ‘org.springframework.boot:spring-boot-starter-web’compileOnly ‘org.projectlombok:lombok’developmentOnly ‘org.springframework.boot:spring-boot-devtools’runtimeOnly ‘mysql:mysql-connector-java’annotationProcessor ‘org.projectlombok:lombok’testImplementation(‘org.springframework.boot:spring-boot-starter-test’) {exclude group: ‘org.junit.vintage’, module: ‘junit-vintage-engine’}implementation “io.springfox:springfox-swagger2:2.9.2”implementation “io.springfox:springfox-swagger-ui:2.9.2”compile group: ‘com.baomidou’, name: ‘mybatis-plus’, version: ‘3.3.1’compile group: ‘org.mybatis.spring.boot’, name: ‘mybatis-spring-boot-starter’, version: ‘2.1.2’
}
test {useJUnitPlatform()}
Springboot+jdbctemplate集成
通过以上的gradle项目构建后,springboot+jdbctemplate 已经被集成进来了集成jdbctemplate 主要需要下图的依赖
Springboot+mybatis集成
通过以上的gradle项目构建后,springboot+mybatis已经被集成进来了集成mybatis主要需要下图的依赖
Swagger集成
集成swagger相对比较简单,springboot里如果没有的话直接上阿里云的仓库找到对应的gradle版本依赖即可阿里云仓库中心地址:https://mvnrepository.com/
选择对应的gradle依赖,将代码粘贴到build.gradle文件中即可
创建商品表
CREATE TABLE goods (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '唯一编号', `name` varchar(255) DEFAULT '' COMMENT '商品名称', `price` decimal(10,2) DEFAULT '0.00' COMMENT '商品价格', `pic` varchar(255) DEFAULT '' COMMENT '图片文件名', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
``
分别用以上两种方式编写接口
这里我们只用最简单的方式完成所有接口编写,接口主要包括商品增删改查和列表检索
springboot+jdbctemplate方式
1. 创建实体(省略set get方法)
@Componentpublic class GoodsDo {/** * 商品id */private Long id;/** * 商品名称 */private String name;/** * 商品价格 */private String price;/** * 商品图片 */```} ### 2. 创建dao ```bash@Repositorypublic class GoodsDao {@Autowiredprivate JdbcTemplate jdbcTemplate;/** * 新增 * @return */public int insert(GoodsDo goods) {return jdbcTemplate.update("insert into goods(name,price,pic)values(?,?,?)", goods.getName(), goods.getPrice(),goods.getPic());}/** * 删除 */public void delete(Long id) {jdbcTemplate.update("delete from goods where id =?", id);}/** * 更新 */public void update(GoodsDo goods) {jdbcTemplate.update("update goods set name=?,price=?,pic=? where id=?", goods.getName(), goods.getPrice(),goods.getPic(), goods.getId());}/** * 按id查询 */public GoodsDo getById(Long id) {return jdbcTemplate.queryForObject("select * from goods where id=?", new RowMapper() {@Overridepublic GoodsDo mapRow(ResultSet rs, int rowNum) throws SQLException {GoodsDo goods = new GoodsDo();goods.setId(rs.getLong("id"));goods.setName(rs.getString("name"));goods.setPrice(rs.getString("price"));goods.setPic(rs.getString("pic"));return goods;}}, id);}/** * 查询商品列表 */public List getList() {return jdbcTemplate.query("select * from goods", new RowMapper() {@Overridepublic GoodsDo mapRow(ResultSet rs, int rowNum) throws SQLException {GoodsDo goods = new GoodsDo();goods.setId(rs.getLong("id"));goods.setName(rs.getString("name"));goods.setPrice(rs.getString("price"));goods.setPic(rs.getString("pic"));return goods;}});}}
3. 创建控制层
/** * 商品控制器类 */@Api(tags= "商品")@RequestMapping(name = "jdbc",path = "jdbc")@RestControllerpublic class GoodsController {@Autowiredprivate GoodsDao goodsService;@ApiOperation(value = "根据id获取商品信息") // 接口文档显示内容@GetMapping("/goods/{id}")public GoodsDo getOne(@PathVariable("id") long id) {return goodsService.getById(id);}@ApiOperation(value = "获取商品列表") // 接口文档显示内容@GetMapping("/goods")public List getList() {return goodsService.getList();}@ApiOperation(value = "新增商品") // 接口文档显示内容@PostMapping("/goods")public Object add(@RequestBody GoodsDo goods) {return goodsService.insert(goods);}@ApiOperation(value = "根据id修改商品信息") // 接口文档显示内容@PutMapping("/goods/{id}")public void update(@PathVariable("id") long id, @RequestBody GoodsDo goods) {goods.setId(id);goodsService.update(goods);}@ApiOperation(value = "根据id删除商品") // 接口文档显示内容@DeleteMapping("/goods/{id}")public void delete(@PathVariable("id") long id) {goodsService.delete(id);}}
4. 接口测试调用
此处可以看到通过jdbc实现的5个接口已经实现了
spingboot+mybatis方式
1. 创建实体(省略此处和上面一样可以共用实体类)
2. 创建dao,这里的dao是一个接口和上面实现有点区别
@Repositorypublic interface GoodsMybatisDao {/** * 新增商品 */public int insert(GoodsDo Goods);/** * 删除商品(根据id) */public int delete(Long id);/** * 修改商品信息(根据id修改其他属性值) */public int update(GoodsDo Goods);/** * 查询商品信息(根据id查询单个商品信息) */public GoodsDo selectOne(Long id);/** * 查询商品列表 */public List selectAll();}
3.创建控制层,这里也和上面一样,为了区别两个不同
/** * 商品控制器类 */@Api(tags= "商品")@RequestMapping(name = "mybatis",path = "mybatis")@RestControllerpublic class GoodsMybatisController {@Autowiredprivate GoodsMybatisDao goodsService;@ApiOperation(value = "根据id获取商品信息") // 接口文档显示内容@GetMapping("/goods/{id}")public GoodsDo getOne(@PathVariable("id") long id) {return goodsService.selectOne(id);}@ApiOperation(value = "获取商品列表") // 接口文档显示内容@GetMapping("/goods")public List getList() {return goodsService.selectAll();}@ApiOperation(value = "新增商品") // 接口文档显示内容@PostMapping("/goods")public Object add(@RequestBody GoodsDo goods) {return goodsService.insert(goods);}@ApiOperation(value = "根据id修改商品信息") // 接口文档显示内容@PutMapping("/goods/{id}")public void update(@PathVariable("id") long id, @RequestBody GoodsDo goods) {goods.setId(id);goodsService.update(goods);}@ApiOperation(value = "根据id删除商品") // 接口文档显示内容@DeleteMapping("/goods/{id}")public void delete(@PathVariable("id") long id) {goodsService.delete(id);}}
4.配置mybatis配置mapper
<?xml version="1.0" encoding="UTF-8" ?>insert into goods (name,price,pic) values (#{name},#{price},#{pic})delete from goods where id=#{id}update goods set name=#{name},price=#{price},pic=#{pic} where id=#{id}select from goods where id = #{id}select from goodsid,name,price,pic
5.配置mybatis application.yml
mybatis: type-aliases-package: com.example.springboot.modelmapper-locations: classpath:mapper/**/*.xml configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6.配置mybatis spingboot启动扫描
import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication@EnableSwagger2 // 启用Swagger2功能@MapperScan(“com.example.springboot.mybatis”) // 指定MyBatis扫描的包,以便将数据访问接口注册为beanpublic class SpringbootApplication {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args);}
}
7.配置mybatis+spingboot接口测试
用0行代码写服务实现以上接口进行对比
1.创建sql文件
select===select id ,name ,price ,pic from goods where 1=1 @byid and id =#byid#@byIn_id and id in(#byIn_id#)@byname and name=#byname#@byprice and price=#byprice#@bypic and pic=#bypic#;selectOneByid===select id ,name ,price ,pic from goods where 1=1 @byid and id =#byid#@byIn_id and id in(#byIn_id#)@byname and name=#byname#@byprice and price=#byprice#@bypic and pic=#bypic#;delete===delete from goods where 1=1 @byid and id =#byid#@byIn_id and id in(#byIn_id#)@byname and name=#byname#@byprice and price=#byprice#@bypic and pic=#bypic#;insert===insert ignore into goods(name,price,pic) VALUES(#name#,#price#,#pic#);update===update goods set @id id=#id#,@name name=#name#,@price price=#price#,@pic pic=#pic# where 1=1 @byid and id =#byid#@byIn_id and id in(#byIn_id#)@byname and name=#byname#@byprice and price=#byprice#@bypic and pic=#bypic#
2.接口测试(自动生成)
需要代码的同学评论区留言,需要加入0行代码写服务的开发者或学习的同学,加我私信。