springboot集成mybatis进行开发

springboot集成mybatis进行开发

1.首先创建springboot项目

点击:http://start.spring.io/ 可以在线创建springboot项目
springboot集成mybatis进行开发

2.加入mybatis的pom文件

org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.4

org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime

3.编写配置文件(application.yml)

spring:
datasource:
url: jdbc:
username:
password:
driver-class-name: com.mysql.jdbc.Driver
jpa:
properties:
hibernate:
current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
dialect: org.hibernate.dialect.MySQL5Dialect
hbm2ddl:
auto: update
show-sql: ‘false’

mybatis:
mapper-locations: classpath:com/ms/mapper/*Mapper.xml (配置这个包下面的所有mapper)

4.编写实体
springboot集成mybatis进行开发

5.编写entity的mapper(@Mapper 标识此类为实体的mapper。在项目加载的时候mybatis会通过寻找此标识来加载)
springboot集成mybatis进行开发

6.编写entity的mapper.xml

springboot集成mybatis进行开发

7.编写service与impl类
springboot集成mybatis进行开发springboot集成mybatis进行开发

8.编写controller进行测试(也可以使用springboot的单元测试)
springboot集成mybatis进行开发

9.测试结果

springboot集成mybatis进行开发

好了以上就是springboot集成mybatis的全部教程。下篇讲解如何使用mybatisPlus来实现代码自动生成,让我们更加专注业务层的实现。