Springboot 自学(二):持久层框架 mybatis-plus篇(注解版)

mac2024-04-13  53

springboot 链接数据库时,个人一般都会采用较为方便的mybatis-plus的注解版,配置简便,使用简单。

添加pom文件引用
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version>#版本,可自行选择 </dependency>
配置yml文件
#mysql spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC username: root password: xxxxxx #oracle driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@//localhost:1521/orcl2 username: xxxxxx password: xxxxxx #mybatis-plus 注解版配置 mybatis-plus: global-config: field-strategy: 2 db-column-underline: true refresh-mapper: true db-config: id-type: auto table-prefix: t_ configuration: map-underscore-to-camel-case: true cache-enabled: false return-instance-for-empty-row: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

mybatis-plus 详细配置

#外部化xml配置 #config-location: classpath:mybatis-config.xml #指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署 #configuration-properties: classpath:mybatis/config.properties #xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置) #mapper-locations: classpath*:/mapper/*.xml #MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名 type-aliases-package: net.xinhuamm.noah.api.model.entity,net.xinhuamm.noah.api.model.dto #如果配置了该属性,则仅仅会扫描路径下以该类作为父类的域对象 #type-aliases-super-type: java.lang.Object #枚举类 扫描路径,如果配置了该属性,会将路径下的枚举类进行注入,让实体类字段能够简单快捷的使用枚举属性 #type-enums-package: com.baomidou.mybatisplus.samples.quickstart.enums #项目启动会检查xml配置存在(只在开发时候打开) check-config-location: true #SIMPLE:该执行器类型不做特殊的事情,为每个语句的执行创建一个新的预处理语句,REUSE:该执行器类型会复用预处理语句,BATCH:该执行器类型会批量执行所有的更新语句 default-executor-type: REUSE configuration: # 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射 map-underscore-to-camel-case: false # 全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为 true cache-enabled: false #懒加载 #aggressive-lazy-loading: true #NONE:不启用自动映射 PARTIAL:只对非嵌套的 resultMap 进行自动映射 FULL:对所有的 resultMap 都进行自动映射 #auto-mapping-behavior: partial #NONE:不做任何处理 (默认值)WARNING:以日志的形式打印相关警告信息 FAILING:当作映射失败处理,并抛出异常和详细信息 #auto-mapping-unknown-column-behavior: none #如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段 call-setters-on-nulls: true # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: #表名下划线命名默认true table-underline: true #id类型 id-type: auto #是否开启大写命名,默认不开启 #capital-mode: false #逻辑已删除值,(逻辑删除下有效) 需要注入逻辑策略LogicSqlInjector 以@Bean方式注入 logic-not-delete-value: 0 #逻辑未删除值,(逻辑删除下有效) logic-delete-value: 1 #数据库类型 db-type: sql_server #设置使得数据库字段强行按照java实体的骆驼式命名法大写字母前转化为下划线加小写的命名规范。 #db-column-underline: true field-strategy: NOT_NULL # 字段更新插入策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断" # IGNORED:所有字段都更新和插入 # NOT_NULL:只更新和插入非NULL值 # NOT_EMPTY:只更新和插入非NULL值且非空字符串 # DEFAULT:默认NOT_NULL #刷新mapper refresh-mapper: true
启动类
@SpringBootApplication @MapperScan("com.example.demo.**.mapper") //Mapper包扫描 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Mapper类
@Repository public interface DemoMapper { @Select("select full_name from t_user") List<String> findAll(); }
Controller类
@RestController @AllArgsConstructor //lombok 工具类 public class DemoController { DemoMapper demoMapper; @GetMapping("/test") public List<String> getAll(){ return demoMapper.findAll(); } }
分页插件配置
@Configuration public class MybatisPlusConfig { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
Mapper类
@Repository public interface DemoMapper { @Select("select full_name from t_user") List<String> findAll(Page page); //当对实体类进行分页时 参数:IPage<实体类> page //当使用服务类时 参数生成: Page<实体类> page = new Page<>(); page.setSize(X); }
Controller类
@RestController @AllArgsConstructor public class DemoController { DemoMapper demoMapper; @GetMapping("/test") public List<String> getAll(){ Page page=new Page(1,10); //1表示当前页,而10表示每页的显示显示的条目数 return demoMapper.findAll(page); } }
最新回复(0)