TMC7300+PIC18F85K22驱动有刷直流电机方案解析
2026/7/11 3:30:22
mybatis-spring-boot-starter 2.xmybatis-spring-boot-starter 3.xxml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> </parent>xml
<!-- web 项目可选 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JDBC 底层,自带HikariCP连接池 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- MyBatis SpringBoot整合启动器(关键) --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 可选:Druid连接池(替换默认Hikari) --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>spring-boot-starter-jdbc:包含spring-jdbc+HikariCP 连接池mybatis-spring-boot-starter:自动整合mybatis+mybatis-spring,无需手动导入SqlSessionFactoryBean创建。MapperScannerConfigurer自动生成 Mapper 代理对象,无需手动 getSqlSession。配置文件数据源(连接池) → DataSource → SqlSessionFactoryBean → Mapper扫描 → Mapper代理Bean注入Service
yaml
# 数据库数据源 + Hikari连接池配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: root # Hikari连接池参数 hikari: # 最小空闲连接 minimum-idle: 5 # 最大连接数 maximum-pool-size: 10 # 连接超时时间,毫秒 connection-timeout: 30000 # 空闲连接超时 idle-timeout: 600000 # 连接最长生命周期 max-lifetime: 1800000 # MyBatis配置 mybatis: # mapper映射文件路径 mapper-locations: classpath:mapper/*.xml # 实体类别名包,xml中可直接写类名不用全限定名 type-aliases-package: com.nzh.springbootdemo.entity configuration: # 开启SQL日志打印 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启驼峰命名自动转换 user_name → userName map-underscore-to-camel-case: trueyaml
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: # 初始连接数 initial-size: 5 # 最小空闲 min-idle: 5 # 最大连接 max-active: 20 # 获取连接等待超时 max-wait: 60000 # 监控页面 stat-view-servlet: enabled: true login-username: admin login-password: 123456java
运行
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // 扫描Mapper接口包,自动生成代理对象交给Spring管理 @MapperScan("com.nzh.springbootdemo.mapper") @SpringBootApplication public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); } }@MapperScan等价于旧版MapperScannerConfigurer,解决你报错里的 ClassPathMapperScanner 初始化逻辑。
java
运行
package com.nzh.springbootdemo.entity; public class User { private Integer id; private String userName; private String password; // getter/setter }java
运行
package com.nzh.springbootdemo.mapper; import com.nzh.springbootdemo.entity.User; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserMapper { List<User> selectAll(); User selectById(@Param("id") Integer id); }xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.nzh.springbootdemo.mapper.UserMapper"> <select id="selectAll" resultType="User"> select id, user_name, password from user </select> <select id="selectById" resultType="User"> select id, user_name, password from user where id = #{id} </select> </mapper>java
运行
@Service public class UserService { // Spring自动注入Mapper代理对象 @Autowired private UserMapper userMapper; public List<User> list(){ return userMapper.selectAll(); } }java
运行
@Configuration @MapperScan("com.nzh.mapper") public class MyBatisConfig { // 1. 创建Hikari连接池数据源 @Bean public DataSource dataSource(){ HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/test_db"); config.setUsername("root"); config.setPassword("root"); config.setMinimumIdle(5); config.setMaximumPoolSize(10); return new HikariDataSource(config); } // 2. 创建SqlSessionFactory @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); // 指定mapper文件路径 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapper/*.xml")); // 实体类别名 factoryBean.setTypeAliasesPackage("com.nzh.entity"); return factoryBean.getObject(); } // 3. 可选:SqlSessionTemplate(手动操作sqlSession) @Bean public SqlSession sqlSession(SqlSessionFactory factory){ return new SqlSessionTemplate(factory); } }maximum-pool-size:根据 MySQL 最大连接数配置,MySQL 默认 151,一般设 10~50minimum-idle:常驻空闲连接,避免高峰期临时创建连接connection-timeout:获取连接等待时长,超时抛异常,防止线程卡死mybatis-spring-boot-starter必须用 2.x 版本。@MapperScan,或扫描包路径写错。mapper-locations路径配置错误,或 XML namespace 和 Mapper 接口全类名不一致。maximum-pool-size并检查代码是否有未关闭流。map-underscore-to-camel-case: true,数据库下划线字段无法映射实体驼峰属性。@MapperScan扫描所有 Mapper 接口,基于动态代理生成实现类,注册为 Spring Bean