springboot simple(4)springboot 数据持久化

ORM(object-relational mapping)对象关系映射将java的普通对象通过ORM持久化到数据库中,有不同的框架。主要的框架包括jdbcTemplate、JPA和Mybatis。
Mybatis:在Sql语句编写方面则更灵活自由,是面向关系的。
Mybatis官网来维护,artifactId是mybatis-spring-boot-starter,以mybatis开头。
在这里主要介绍Mybatis的入门。

1 Mybatis Springboot

第1步:POM文件引入:

<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>${mysql-connector}</version>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>${mybatis-spring-boot}</version>
	</dependency>

第2步:配置application.properties:

##数据源配置
spring.datasource.url=jdbc:mysql://192.168.0.1:3306/db_springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=test
spring.datasource.password=123456
#Mybatis 配置
mybatis.typeAliasesPackage=org.feidao.chapter41.model
mybatis.mapperLocations=classpath:mapper/*.xml

1.1 查询验证

首先执行localhost:8080/mybatis/select查询:
在这里插入图片描述

1.2 插入验证

执行localhost:8080/ mybatis/add增加一个英雄:
在这里插入图片描述
然后,执行localhost:8080/mybatis/select查询验证增加英雄是否成功:
在这里插入图片描述

1.3 修改验证

执行localhost:8080/mybatis/update修改英雄:
在这里插入图片描述
然后,执行localhost:8080/mybatis/select查询验证更新英雄是否成功:
在这里插入图片描述

1.4 删除验证

执行localhost:8080/mybatis/update修改英雄:
在这里插入图片描述
然后,执行localhost:8080/mybatis/select查询验证删除英雄是否成功:
在这里插入图片描述
代码详见https://gitee.com/alifeidao/springboot-simple/tree/master/
chapter4-1


教程列表
springboot simple(0) springboot简介
springboot simple(1) springboot Helloworld
springboot simple(2) springboot Starter
springboot simple(3 )springboot Web开发
springboot simple(4)springboot 数据持久化
springboot simple (5) springboot Nosql
springboot simple (6) springboot mqtt
springboot simple (7) springboot thrift
springboot simple (8) springboot kafka
springboot simple (9) springboot jpa(Hibernate)
springboot simple (10) springboot protobuf
springboot simple (11) springboot protostuff
springboot simple (12) springboot RabbitMQ

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值