本文代码链接:https://github.com/SsRong/springboot-ssrtest03
1、传统我们使用JDBC来实现数据库和业务逻辑的连接。但是有过使用jdbc经验的同志会知道,我们通常会在代码中加上getconnect类以及dao类,在这一系列代码中我们会发现他们的代码冗余度会很大,而且还要我们手动的输入SQL语句当做参数传输,经常会因为SQL语句的一些符合之类的错误导致程序报错。所以mybatis的作为一个持久层的框架,它把jdbc进行了一些封装,并且也支持定制化SQL。简单点说就是我们使用mybatis,就只需要在model层(或者叫entity层),mapper层(或者叫dao层),service层以及controller层进行相应的功能程序分配便能极大的简化我们连接数据库时的工作。
2、理解model层(或者叫entity层),mapper层(或者叫dao层),service层以及controller层都在担任什么角色?起到什么作用?他们之间的交互逻辑是什么样子的?
2.1、controller层整合完servlet后,在网页端接收到参数,并将参数提交到service层进行处理;
2.2、业务层和数据层的完美分离
2.3、model层/entity层,基本每个层都会 import com.ssrtest03.entity.User。这样也就避免了重复定义和代码的耦合
3、搭建项目,操作步骤
3.1、
3.2、
3.3、
3.4、配置你的数据库信息
###datasource尽量不要去修改
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.5、导入的包依赖
<dependencies>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter </artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-test </artifactId>
<scope> test </scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId> mysql </groupId>
<artifactId> mysql-connector-java </artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>