Springboot整合MyBatis
感谢大佬:
spring boot框架mybatis.mapper-locations配置问题详解_MarkZP-CSDN博客
- 非spring官方写的用框架名开头
@mapper
是给mybatis看的,@repository
是给spring扫描组件注册到IOC容器里面用的
1、配置数据库信息:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=GMT
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource #自定义数据眼
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
type-aliases-package: com.pojo
#若 mapper文件要和接口放一起,则不配置下列:
# mapper-locations: classpath:mybatis/mapper/*.xml
2.报错:
not set url:
明明已经在yaml
配置 url,未识别,可能是 maven配置资源过滤问题
pom.xml
配置:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<!--by kuang shen -->
或:(都尝试一下)
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
- 未识别返回类型:
原因: 未添加:
mybatis:
type-aliases-package: com.pojo
mapper-locations: classpath:com/dao/*.xml
注意:
-
此处 我是在接口所在包中加入接口:
- 故
mapper-locations:
尽量去掉!!
- 故
- mapper找不到:
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
我写了全名反而找不到,奇奇怪怪
- 还有一件事:
接口名字要与xml文件名字保持一致!
2、编写 pojo 实体类和Mapper 接口
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer Did;
private String DepartmentName;
}
@Mapper
-
*表示本类是一个 MyBatis 的 Mapper。
-
对应的xxxMapper.xml就是来实现这个Mapper。*
@Mapper
@Repository
public interface DepartmentMapper {
int addDepartment(Department Department);
int deleteDepartment(Integer id);
int updateDepartment(Department Department);
List<Department> findAllDepartment();
Department findDepartmentbyid(Integer id);
}
3、 controller
@Controller
public class Indexcontroller {
@Autowired
private DepartmentMapper departmentMapper;
@RequestMapping("/findAllDepartment")
@ResponseBody
public List<Department> findAllDepartment(Model model){
List<Department> list = departmentMapper.findAllDepartment();
for (Department department : list) {
System.out.println(department);
}
return list;
}
}
结果:
终于出来了,好累