当前位置: 首页 > news >正文

python h5网站开发python语言基础

python h5网站开发,python语言基础,国内最新军事新闻,wordpress 提速插件目录 一、Mybatis插件简介#x1f959;二、工程创建及前期准备工作#x1f96b;实现代码配置文件 三、插件核心代码实现#x1f357;四、测试#x1f953; 一、Mybatis插件简介#x1f959; Mybatis插件运行原理及自定义插件_简述mybatis的插件运行原理,以及如何编写一个… 目录 一、Mybatis插件简介二、工程创建及前期准备工作实现代码配置文件 三、插件核心代码实现四、测试 一、Mybatis插件简介 Mybatis插件运行原理及自定义插件_简述mybatis的插件运行原理,以及如何编写一个插件-CSDN博客 MyBatis 是一款优秀的持久层框架它简化了数据库操作过程提供了强大的 SQL 映射功能。MyBatis 插件是用来扩展 MyBatis 框架功能的工具可以通过插件来定制和增强 MyBatis 的功能。 MyBatis 插件可以用来实现一些自定义的功能比如拦截 SQL 语句、修改 SQL 语句、添加新的功能等。通过插件我们可以在 MyBatis 框架的各个阶段进行干预和扩展从而实现更灵活、更强大的功能。 通常情况下编写一个 MyBatis 插件需要实现 MyBatis 提供的接口并在配置文件中注册插件。Mybatis只支持针对ParameterHandler、ResultSetHandler、StatementHandler、Executor这4种接口 总的来说MyBatis 插件是一种扩展机制可以让我们更好地定制和增强 MyBatis 框架的功能使得我们能够更好地适应各种不同的业务需求。 二、工程创建及前期准备工作 以下都为前期准备工作可直接略过插件核心实现代码在第三节 创建test数据库 SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS 0; DROP TABLE IF EXISTS user; CREATE TABLE user (id int NOT NULL AUTO_INCREMENT,username varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,password varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,create_time datetime NULL DEFAULT NULL,update_time datetime NULL DEFAULT NULL,PRIMARY KEY (id) USING BTREE ) ENGINE InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ROW_FORMAT Dynamic;SET FOREIGN_KEY_CHECKS 1; 实现代码 本实验省略了Controller和Service User.java package com.example.mybatisplugin.entity;import com.example.mybatisplugin.anno.FiledFill;import java.io.Serial; import java.time.LocalDateTime; import java.util.Date; import java.io.Serializable;/*** (User)实体类** author makejava* since 2024-03-11 14:41:35*/ public class User implements Serializable {Serialprivate static final long serialVersionUID 813676794892349198L;private Integer id;private String username;private String password;FiledFill(fill FiledFill.FillType.INSERT)private Date createTime;FiledFill(fill FiledFill.FillType.INSERT_UPDATE)private Date updateTime;//省略了getter和setter方法Overridepublic String toString() {return User{ id id , username username \ , password password \ , createTime createTime , updateTime updateTime };} } 其中FiledFill注解是自定义注解文章后面会写到可暂时不用添加 UserDao.java Mapper public interface UserDao {/*** 新增数据** param user 实例对象* return 影响行数*/int insert(User user);/*** 修改数据** param user 实例对象* return 影响行数*/int update(User user);}UserDao.xml ?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.mybatisplugin.dao.UserDaoresultMap typecom.example.mybatisplugin.entity.User idUserMapresult propertyid columnid jdbcTypeINTEGER/result propertyusername columnusername jdbcTypeVARCHAR/result propertypassword columnpassword jdbcTypeVARCHAR/result propertycreateTime columncreate_time jdbcTypeTIMESTAMP/result propertyupdateTime columnupdate_time jdbcTypeTIMESTAMP//resultMap!--新增所有列--insert idinsert keyPropertyid useGeneratedKeystrueinsert into user(username,password,create_time,update_time)values (#{username},#{password},#{createTime},#{updateTime})/insert!--通过主键修改数据--update idupdateupdate usersetif testusername ! null and username ! username #{username},/ifif testpassword ! null and password ! password #{password},/ifif testcreateTime ! nullcreate_time #{createTime},/ifif testupdateTime ! nullupdate_time #{updateTime},/if/setwhere id #{id}/update /mapper配置文件 application.yaml 数据库密码记得改成自己的 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: *****url: jdbc:mysql://localhost:3306/test?useUnicodetruecharacterEncodingUTF-8useSSLfalseserverTimezoneAsia/Shanghai server:port: 8080 mybatis:config-location: classpath:mybatis-config.xmlmapper-locations: classpath:mapper/*.xmlmybatis-config.xml ?xml version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC-//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configurationsettings!-- 设置驼峰标识 --setting namemapUnderscoreToCamelCase valuetrue/!-- 打印SQL语句 --setting namelogImpl valueSTDOUT_LOGGING//settings /configuration三、插件核心代码实现 自定义注解FiledFill package com.example.mybatisplugin.anno;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** Author YZK* Date 2024/3/11*/ Target(ElementType.FIELD) Retention(RetentionPolicy.RUNTIME) public interface FiledFill {enum FillType {INSERT, INSERT_UPDATE,}FillType fill(); }插件核心代码 package com.example.mybatisplugin.plugin;import com.example.mybatisplugin.anno.FiledFill; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Signature;import java.lang.reflect.Field; import java.util.Arrays; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger;/*** Author YZK* Date 2024/3/11* Desc*/ Intercepts({Signature(type Executor.class,method update,args {MappedStatement.class, Object.class})}) public class MybatisAutoFill implements Interceptor {private static final Logger LOGGER Logger.getLogger(MybatisAutoFill.class.getName());public Object intercept(Invocation invocation) throws Throwable {MappedStatement mappedStatement (MappedStatement) invocation.getArgs()[0];//获取操作类型INSERT和UPDATESqlCommandType sqlCommandType mappedStatement.getSqlCommandType();//拿到SQL中传入的对象Object obj invocation.getArgs()[1];//获取其字节对象Class? clazz obj.getClass();//获取User类声明的所有字段Field[] fields clazz.getDeclaredFields();//通过区分SQL的操作来进行不同的字段填充if (sqlCommandType SqlCommandType.INSERT) {//是INSERT操作的话就同时填充createTime和updateTime字段fillInsertFields(obj, fields);} else if (sqlCommandType SqlCommandType.UPDATE) {//是updateTime字段的话就只填充updateTime字段fillUpdateFields(obj, fields);}return invocation.proceed();}private void fillInsertFields(Object obj, Field[] fields) {Arrays.stream(fields)//过滤出所有带有FiledFill注解的字段.filter(field - field.isAnnotationPresent(FiledFill.class)).forEach(field - {try {//对字段进行填充setFieldValue(obj, field);} catch (IllegalAccessException e) {LOGGER.log(Level.SEVERE, 字段填充错误, e);}});}private void fillUpdateFields(Object obj, Field[] fields) {Arrays.stream(fields)//过滤出所有带有FiledFill注解的字段以及注解值为INSERT_UPDATE的字段.filter(field - field.isAnnotationPresent(FiledFill.class) field.getAnnotation(FiledFill.class).fill() FiledFill.FillType.INSERT_UPDATE).forEach(field - {try {//对字段进行填充setFieldValue(obj, field);} catch (IllegalAccessException e) {LOGGER.log(Level.SEVERE, 字段填充错误, e);}});}private void setFieldValue(Object obj, Field field) throws IllegalAccessException {//填充字段field.setAccessible(true);field.set(obj, new Date());} }mybatis-condig.xml配置文件中进行配置将自定义的插件注册 ?xml version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC-//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configurationsettings!-- 设置驼峰标识 --setting namemapUnderscoreToCamelCase valuetrue/!-- 打印SQL语句 --setting namelogImpl valueSTDOUT_LOGGING//settingspluginsplugin interceptorcom.example.mybatisplugin.plugin.MybatisAutoFill//plugins /configuration 四、测试 插入操作 Test void contextLoads() {User user new User();user.setUsername(笑的像个child);user.setPassword(123456);userDao.insert(user); }控制台打印的SQL 进行插入操作时create_time和update_time字段被同时填充 更新操作 Testvoid contextLoads() {User user new User();user.setId(33);user.setUsername(笑的像个child);user.setPassword(12345678);userDao.update(user);}控制台打印的SQL 进行更新时操作时update_time字段被填充 进行删除操作时如果是硬删除则记录被删除软删除时同样是更新操作字段也会被自动填充。 本插件还有许多需要完善的地方只是自动填充的简单实现如有需要可以自己完善。
http://www.dnsts.com.cn/news/130178.html

相关文章:

  • 轻量服务器wordpress网站怎样做seo
  • 企业网站模板文件管理北京景点
  • 搭建网站建设互联网行业还能支撑多久
  • 百度推广进入后是别的网站 说是服务器问题长沙好的设计公司
  • 旅游网站模板下载南京百度seo排名
  • 泰安商城网站开发设计免费seo软件推荐
  • 知名高校网站建设做家政网站公司简介
  • 推荐一个做照片书的网站郑州营销网站公司地址
  • 连云港市网站建设广告设计专业就业前景怎么样
  • 建电商网站wordpress 模板 小工具
  • 网站后台登陆不了上海网站建设公司指南
  • pc端网站转手机站怎么做写字楼租赁
  • 加强网站队伍建设WordPress博客模板慢
  • 广东移动宽带官网站wordpress jetpack插件
  • 万网主机 网站访问网络推广哪个网站好
  • vip视频网站如何做天元建设集团有限公司总裁赵纪峰
  • 网站系统建站做外链的博客网站
  • 大丰网站建设找哪家好百度网页电脑版入口
  • 手机网站导航代码启动wordpress mu
  • 东莞好的网站建设效果百度行业网站怎么做
  • iis默认网站在哪里北京到安阳火车票
  • 网站栏目设计优化方案深圳市手机网站建设品牌
  • 手机网站活动策划方案公司做网站买服务器多少钱
  • 做网站商标分类学编程在哪里学比较正规
  • 简洁风网站营口市城乡住房建设局网站
  • 学校网站首页设计h5高端网站建设
  • 网站上做扫一扫驻马店网站建设维护
  • 果洛电子商务网站建设网站加载优化
  • html网站 怎么做seo汽车之家app下载
  • 甘肃平凉建设局网站桂林两江四湖夜游时间