配置数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
data-password: root
data-username: root
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
password: root
username: root
整合mybatis
mybatis:
type-aliases-package: com.xcz.myblog.entity#扫描实体
mapper-locations: classpath:mapper/*.xml#扫描xml文件
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句到控制台
静态资源引入问题
在application.yml设置
mvc:
static-path-pattern: /static/**
view:
suffix: .html
端口和默认访问路径设置
server:
port: 8081
servlet:
context-path: /xcz #访问路径
扫描mapper
使用注解
@Mapper
@Repository
或者
基于注解的开发也有其他手段帮助 Mybatis 找到 mapper,那就是 @MapperScan 注解,可以在启动类上添加该注解,自动扫描包路径下的所有接口。
@SpringBootApplication
@MapperScan("com.xcz.mtblog.mapper")
public class UserEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(UserEurekaClientApplication.class, args);
@Mapper 一定要有,否则 Mybatis 找不到 mapper。
@Repository 可有可无,可以消去依赖注入的报错信息。
@MapperScan 可以替代 @Mapper。