引入 mybatis
maven依赖引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
application.yml
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.itcast.mybatis.entity
mapper-locations: classpath:mybatis/mapper/*.xml
文件目录对应的位置
引入mybatis-plus
application.yml
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志
map-underscore-to-camel-case: true
# 该配置就是将带有下划线的表字段映射为驼峰格式的实体类属性
type-aliases-package: com.itcast.mybatis.entity
mapper-locations: classpath:mybatis/mapper/*.xml
mapper
@Mapper
public interface TeacherMapper extends BaseMapper<ScTeacher> {
}
service
public interface TeacherService extends IService<ScTeacher> {
List<ScTeacher> getTeacherList();
}
serviceImpl
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, ScTeacher> implements TeacherService {
@Autowired
private TeacherMapper teacherMapper;
@Override
public List<ScTeacher> getTeacherList() {
return teacherMapper.selectList(null);
}
}