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