MyBatis的基本应用

源码地址

01.MyBatis环境搭建

  1. 添加MyBatis的坐标

            <!--mybatis坐标--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><!--mysql驱动坐标--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version><scope>runtime</scope></dependency><!--单元测试坐标--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version></dependency>
    
  2. 创建数据表

  3. 编写DO实体类

  4. 编写映射⽂件UserMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.muchfish.dao.IUserDao"><!--namespace : 名称空间:与id组成sql的唯一标识resultType: 表明返回值类型--><!--查询用户--><select id="findAll" resultType="com.muchfish.pojo.User">select * from User</select></mapper>
    
  5. 编写核⼼⽂件SqlMapConfig.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--加载外部的properties文件--><properties resource="jdbc.properties"/><!--environments:运行环境--><environments default="development"><environment id="development"><!--当前事务交由JDBC进行管理--><transactionManager type="JDBC"/><!--当前使用mybatis提供的连接池--><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments><!--引入映射配置文件--><mappers><mapper resource="UserMapper.xml"/></mappers></configuration>
    

    jdbc.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql:///mybatis
    jdbc.username=root
    jdbc.password=123456
    
  6. 编写测试类

        @Testpublic void test1() throws IOException {//1.Resources工具类,配置文件的加载,把配置文件加载成字节输入流InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");//2.解析了配置文件,并创建了sqlSessionFactory工厂SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//3.生产sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();// 默认开启一个事务,但是该事务不会自动提交//在进行增删改操作时,要手动提交事务//4.sqlSession调用方法:查询所有selectList  查询单个:selectOne 添加:insert  修改:update 删除:deleteList<User> users = sqlSession.selectList("com.muchfish.dao.IUserDao.findAll");for (User user : users) {System.out.println(user);}sqlSession.close();}
    

02.MyBatis的CRUD

  1. CRUD的API

    • sqlSession.selectList()、sqlSession.selectOne()
    • sqlSession.insert()
    • sqlSession.update()
    • sqlSession.delete()
  2. 注意问题

    • 在进行增删改操作时,要手动提交事务。

      sqlSessionFactory.openSession()默认开启一个事务,但是该事务不会自动提交

    • mapper.xml中的Sql语句中使⽤#{任意字符串}⽅式引⽤传递的单个参数

      <!--删除-->
      <delete id="deleteUser" parameterType="int">delete from user where id = #{abc}
      </delete>
      
    • sqlSession.close():释放资源

    • sqlSession.commit()

    • sqlSession.rollback()

    • sqlSessionFactory.openSession(true):事务自动提交

03.MyBatis相关配置文件

sqlMapConfig.xml

在这里插入图片描述
在这里插入图片描述

  • mapper标签

    该标签的作⽤是加载映射的,加载⽅式有如下⼏种:

    •使⽤相对于类路径的资源引⽤,例如:
    <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>•使⽤完全限定资源定位符(URL),例如:
    <mapper url="file:///var/mappers/AuthorMapper.xml"/>•使⽤映射器接⼝实现类的完全限定类名,例如:
    <mapper class="org.mybatis.builder.AuthorMapper"/>
    注意:保证接口名和xml文件名一致且包结构一致(是否包结构一直皆可,文件名可以不一致)•将包内的映射器接⼝实现全部注册为映射器,例如:
    <package name="org.mybatis.builder"/>
    注意:保证接口名和xml文件名一致且包结构一致。(是否包结构一直皆可,文件名可以不一致。测试得:文件名也必须一致)
    

XXXmapper.xml

在这里插入图片描述

04.MyBatis的Dao层代理开发方式与mappers标签测试

mappers标签测试

•使⽤映射器接⼝实现类的完全限定类名,例如:
<mapper class="com.muchfish.dao.IUserDao"/>
注意:保证接口名和xml文件名一致且包结构一致•将包内的映射器接⼝实现全部注册为映射器,例如:
<package name="com.muchfish.dao"/>
注意:保证接口名和xml文件名一致且包结构一致。

在这里插入图片描述

Dao层代理开发

Mapper 接⼝开发需要遵循以下规范:

  1. Mapper.xml⽂件中的namespace与mapper接⼝的全限定名相同
  2. Mapper接⼝⽅法名和Mapper.xml中定义的每个statement的id相同
  3. Mapper接⼝⽅法的输⼊参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
  4. Mapper接⼝⽅法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

在这里插入图片描述

05.MyBatis的多对多复杂映射

  1. DO类

    public class User {private Integer id;private String username;private String password;private String birthday;//表示用户关联的角色private List<Role> roleList = new ArrayList<>();//。。。省略getter/setter
    }    
    
  2. Mapper.xml

        <resultMap id="userRoleMap" type="com.muchfish.pojo.User"><result property="id" column="userid"></result><result property="username" column="username"></result><collection property="roleList" ofType="com.muchfish.pojo.Role"><result property="id" column="roleid"></result><result property="roleName" column="roleName"></result><result property="roleDesc" column="roleDesc"></result></collection></resultMap><select id="findAllUserAndRole" resultMap="userRoleMap">select * from user u left join sys_user_role ur on u.id = ur.useridleft join sys_role r on r.id = ur.roleid</select>
    
  3. Dao接口

    public interface IUserDao {public List<User> findAllUserAndRole();
    }
    

06.MyBatis的注解开发

  • MyBatis的常⽤注解

    • @Insert:实现新增
    • @Update:实现更新
    • @Delete:实现删除
    • @Select:实现查询
    • @Result:实现结果集封装
    • @Results:可以与@Result ⼀起使⽤,封装多个结果集
    • @One:实现⼀对⼀结果集封装
    • @Many:实现⼀对多结果集封装
  • 注解一对多查询

    public interface IOrderDao {//查询订单的同时还查询该订单所属的用户@Results({@Result(property = "id",column = "id"),@Result(property = "orderTime",column = "orderTime"),@Result(property = "total",column = "total"),@Result(property = "user",column = "uid",javaType = User.class,one=@One(select = "com.muchfish.dao.IUserDao.findUserById"))})@Select("select * from orders")public List<Order> findOrderAndUser();@Select("select * from orders where uid = #{uid}")public List<Order> findOrderByUid(Integer uid);}
    
    • 注解和xml混合使用命中同一个statementId会报错

07.MyBatis缓存

⼀级缓存

private IUserDao userMapper;
private SqlSession sqlSession;
private SqlSessionFactory sqlSessionFactory;@Before
public void before() throws IOException {InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);sqlSession = sqlSessionFactory.openSession();userMapper = sqlSession.getMapper(IUserDao.class);}@Test
public void test1() {//第⼀次查询,发出sql语句,并将查询出来的结果放进缓存中User u1 = userMapper.findUserById(1);System.out.println(u1);//第⼆次查询,由于是同⼀个sqlSession,会在缓存中查询结果//如果有,则直接从缓存中取出来,不和数据库进⾏交互User u2 = userMapper.findUserById(1);System.out.println(u2);sqlSession.close();
}

查看控制台打印情况:

在这里插入图片描述

    @Testpublic void test2(){//根据 sqlSessionFactory 产⽣ session//第⼀次查询,发出sql语句,并将查询的结果放⼊缓存中User u1 = userMapper.findUserById( 1 );System.out.println(u1);//第⼆步进⾏了⼀次更新操作, sqlSession.commit()u1.setPassword("23131");userMapper.updateUserByUserId(u1);sqlSession.commit();//第⼆次查询,由于是同⼀个sqlSession.commit(),会清空缓存信息//则此次查询也会发出sql语句User u2 = userMapper.findUserById(1);System.out.println(u2);sqlSession.close();}

查看控制台打印情况:
日志略:第⼆次查询会打印sql语句

在这里插入图片描述

  • 总结
    1. 第⼀次发起查询⽤户id为1的⽤户信息,先去找缓存中是否有id为1的⽤户信息,如果没有,从 数据库查询⽤户信息。得到⽤户信息,将⽤户信息存储到⼀级缓存中。
    2. 如果中间sqlSession去执⾏commit操作(执⾏插⼊、更新、删除),则会清空SqlSession中的 ⼀级缓存,这样做的⽬的为了让缓存中存储的是最新的信息,避免脏读。
    3. 第⼆次发起查询⽤户id为1的⽤户信息,先去找缓存中是否有id为1的⽤户信息,缓存中有,直 接从缓存中获取⽤户信息

二级缓存

在这里插入图片描述

  1. 使用二级缓存

    1. 开启⼆级缓存

      1. 在全局配置⽂件sqlMapConfig.xml⽂件中开启

        <!--开启⼆级缓存 注意<settings>标签的顺序,在<properties>标签后面-->
        <settings>
        <setting name="cacheEnabled" value="true"/>
        </settings>
        
      2. 在Mapper.xml⽂件中开启缓存

            <!--使用二级缓存--><cache></cache>
        
      3. 在DAO接口中开启缓存(可选。使用@CacheNamespace会报错。)

        @CacheNamespace
        public interface IUserDao {}
        

        @CacheNamespaceRef(IUserDao.class) 
        //@CacheNamespace
        public interface IUserDao {}
        
        • xml和对应dao接口上同时开启二级缓存会报错,此时只能使用@CacheNamespaceRef
    2. DO序列化

      public class User implements Serializable {//。。。
      }
      
    3. 测试

       @Testpublic void SecondLevelCache(){//根据 sqlSessionFactory 产⽣ sessionSqlSession sqlSession1 = sqlSessionFactory.openSession();SqlSession sqlSession2 = sqlSessionFactory.openSession();SqlSession sqlSession3 = sqlSessionFactory.openSession();String statement = "com.lagou.pojo.UserMapper.selectUserByUserld" ;IUserDao userMapper1 = sqlSession1.getMapper(IUserDao. class );IUserDao userMapper2 = sqlSession2.getMapper(IUserDao. class );IUserDao userMapper3 = sqlSession2.getMapper(IUserDao. class );//第⼀次查询,发出sql语句,并将查询的结果放⼊缓存中User u1 = userMapper1.findById( 1 );System.out.println(u1);sqlSession1.close(); //第⼀次查询完后关闭sqlSession//执⾏更新操作, commit()。注释掉此处,sqlSession2的查询会走缓存。放开此处,会走数据库
      //        u1.setUsername( "aaa" );
      //        userMapper3.updateUserByUserId(u1);
      //        sqlSession3.commit();//第⼆次查询,由于上次更新操作,缓存数据已经清空(防⽌数据脏读),这⾥必须再次发出sql语User u2 = userMapper2.findById( 1 );System.out.println(u2);sqlSession2.close();}
      
  2. useCache和flushCache

    1. useCache:开启或禁用缓存

          <select id="findById" resultType="com.muchfish.pojo.User" useCache="true">select * from user where id = #{id}</select>
      
    2. flushCache:刷新缓存

          <select id="findById" resultType="com.muchfish.pojo.User" useCache="true" flushCache="true">select * from user where id = #{id}</select>
      

二级缓存整合redis

  1. pom⽂件

            <dependency><groupId>org.mybatis.caches</groupId><artifactId>mybatis-redis</artifactId><version>1.0.0-beta2</version></dependency>
    
  2. 配置⽂件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.muchfish.dao.IUserDao"><!--namespace : 名称空间:与id组成sql的唯一标识resultType: 表明返回值类型--><!--使用二级缓存--><cache type="org.mybatis.caches.redis.RedisCache" />
    
  3. redis.properties

    redis.host=localhost
    redis.port=6379
    redis.connectionTimeout=5000
    redis.password=
    redis.database=0
    
  4. 测试

        @Testpublic void xmlSecondLevelCache(){//根据 sqlSessionFactory 产⽣ sessionSqlSession sqlSession1 = sqlSessionFactory.openSession();SqlSession sqlSession2 = sqlSessionFactory.openSession();SqlSession sqlSession3 = sqlSessionFactory.openSession();String statement = "com.lagou.pojo.UserMapper.selectUserByUserld" ;IUserDao userMapper1 = sqlSession1.getMapper(IUserDao. class );IUserDao userMapper2 = sqlSession2.getMapper(IUserDao. class );IUserDao userMapper3 = sqlSession2.getMapper(IUserDao. class );//第⼀次查询,发出sql语句,并将查询的结果放⼊缓存中User u1 = userMapper1.findById( 1 );System.out.println(u1);sqlSession1.close(); //第⼀次查询完后关闭sqlSession//执⾏更新操作, commit()。注释掉此处,sqlSession2的查询会走缓存。放开此处,会走数据库
    //        u1.setUsername( "aaa" );
    //        userMapper3.updateUserByUserId(u1);
    //        sqlSession3.commit();//第⼆次查询,由于上次更新操作,缓存数据已经清空(防⽌数据脏读),这⾥必须再次发出sql语User u2 = userMapper2.findById( 1 );System.out.println(u2);sqlSession2.close();}
  5. RedisCache实现原理

    /*** Cache adapter for Redis.** @author Eduardo Macarron*/
    public final class RedisCache implements Cache {private final ReadWriteLock readWriteLock = new DummyReadWriteLock();private String id;private static JedisPool pool;public RedisCache(final String id) {if (id == null) {throw new IllegalArgumentException("Cache instances require an ID");}this.id = id;RedisConfig redisConfig = RedisConfigurationBuilder.getInstance().parseConfiguration();pool = new JedisPool(redisConfig, redisConfig.getHost(), redisConfig.getPort(),redisConfig.getConnectionTimeout(), redisConfig.getSoTimeout(), redisConfig.getPassword(),redisConfig.getDatabase(), redisConfig.getClientName());}private Object execute(RedisCallback callback) {Jedis jedis = pool.getResource();try {return callback.doWithRedis(jedis);} finally {jedis.close();}}@Overridepublic String getId() {return this.id;}@Overridepublic int getSize() {return (Integer) execute(new RedisCallback() {@Overridepublic Object doWithRedis(Jedis jedis) {Map<byte[], byte[]> result = jedis.hgetAll(id.toString().getBytes());return result.size();}});}@Overridepublic void putObject(final Object key, final Object value) {execute(new RedisCallback() {@Overridepublic Object doWithRedis(Jedis jedis) {jedis.hset(id.toString().getBytes(), key.toString().getBytes(), SerializeUtil.serialize(value));return null;}});}@Overridepublic Object getObject(final Object key) {return execute(new RedisCallback() {@Overridepublic Object doWithRedis(Jedis jedis) {return SerializeUtil.unserialize(jedis.hget(id.toString().getBytes(), key.toString().getBytes()));}});}@Overridepublic Object removeObject(final Object key) {return execute(new RedisCallback() {@Overridepublic Object doWithRedis(Jedis jedis) {return jedis.hdel(id.toString(), key.toString());}});}@Overridepublic void clear() {execute(new RedisCallback() {@Overridepublic Object doWithRedis(Jedis jedis) {jedis.del(id.toString());return null;}});}@Overridepublic ReadWriteLock getReadWriteLock() {return readWriteLock;}@Overridepublic String toString() {return "Redis {" + id + "}";}}
    
    
    /*** Converter from the Config to a proper {@link RedisConfig}.** @author Eduardo Macarron*/
    final class RedisConfigurationBuilder {/*** This class instance.*/private static final RedisConfigurationBuilder INSTANCE = new RedisConfigurationBuilder();private static final String SYSTEM_PROPERTY_REDIS_PROPERTIES_FILENAME = "redis.properties.filename";private static final String REDIS_RESOURCE = "redis.properties";private final String redisPropertiesFilename;/*** Hidden constructor, this class can't be instantiated.*/private RedisConfigurationBuilder() {redisPropertiesFilename = System.getProperty(SYSTEM_PROPERTY_REDIS_PROPERTIES_FILENAME, REDIS_RESOURCE);}/*** Return this class instance.** @return this class instance.*/public static RedisConfigurationBuilder getInstance() {return INSTANCE;}/*** Parses the Config and builds a new {@link RedisConfig}.** @return the converted {@link RedisConfig}.*/public RedisConfig parseConfiguration() {return parseConfiguration(getClass().getClassLoader());}/*** Parses the Config and builds a new {@link RedisConfig}.** @param the*            {@link ClassLoader} used to load the*            {@code memcached.properties} file in classpath.* @return the converted {@link RedisConfig}.*/public RedisConfig parseConfiguration(ClassLoader classLoader) {Properties config = new Properties();InputStream input = classLoader.getResourceAsStream(redisPropertiesFilename);if (input != null) {try {config.load(input);} catch (IOException e) {throw new RuntimeException("An error occurred while reading classpath property '"+ redisPropertiesFilename+ "', see nested exceptions", e);} finally {try {input.close();} catch (IOException e) {// close quietly}}}RedisConfig jedisConfig = new RedisConfig();setConfigProperties(config, jedisConfig);return jedisConfig;}private void setConfigProperties(Properties properties,RedisConfig jedisConfig) {if (properties != null) {MetaObject metaCache = SystemMetaObject.forObject(jedisConfig);for (Map.Entry<Object, Object> entry : properties.entrySet()) {String name = (String) entry.getKey();String value = (String) entry.getValue();if (metaCache.hasSetter(name)) {Class<?> type = metaCache.getSetterType(name);if (String.class == type) {metaCache.setValue(name, value);} else if (int.class == type || Integer.class == type) {metaCache.setValue(name, Integer.valueOf(value));} else if (long.class == type || Long.class == type) {metaCache.setValue(name, Long.valueOf(value));} else if (short.class == type || Short.class == type) {metaCache.setValue(name, Short.valueOf(value));} else if (byte.class == type || Byte.class == type) {metaCache.setValue(name, Byte.valueOf(value));} else if (float.class == type || Float.class == type) {metaCache.setValue(name, Float.valueOf(value));} else if (boolean.class == type || Boolean.class == type) {metaCache.setValue(name, Boolean.valueOf(value));} else if (double.class == type || Double.class == type) {metaCache.setValue(name, Double.valueOf(value));} else {throw new CacheException("Unsupported property type: '"+ name + "' of type " + type);}}}}}}

    在这里插入图片描述

  6. 小结

    1. 二级缓存使用redis可以实现分布式缓存
    2. 自定义实现二级缓存,通过实现Cache接口
    3. RedisCache通过RedisConfigurationBuilder加载redis.properties中的配置
    4. RedisCache使用了JedisPool
    5. RedisCache使用了模板方法,完成putObjectgetObjectremoveObjectclear等操作

08.MyBatis插件

Mybatis插件原理

  1. 注册插件

    //XMLConfigBuilder类的parseConfiguration方法 (解析SqlMapConfig.xml时)
    private void parseConfiguration(XNode root) {try {// issue #117 read properties firstpropertiesElement(root.evalNode("properties"));Properties settings = settingsAsProperties(root.evalNode("settings"));loadCustomVfs(settings);loadCustomLogImpl(settings);typeAliasesElement(root.evalNode("typeAliases"));//解析插件  pluginElement(root.evalNode("plugins"));objectFactoryElement(root.evalNode("objectFactory"));objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));reflectorFactoryElement(root.evalNode("reflectorFactory"));settingsElement(settings);// read it after objectFactory and objectWrapperFactory issue #631environmentsElement(root.evalNode("environments"));databaseIdProviderElement(root.evalNode("databaseIdProvider"));typeHandlerElement(root.evalNode("typeHandlers"));mapperElement(root.evalNode("mappers"));} catch (Exception e) {throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);}}
    
      private void pluginElement(XNode parent) throws Exception {if (parent != null) {for (XNode child : parent.getChildren()) {String interceptor = child.getStringAttribute("interceptor");Properties properties = child.getChildrenAsProperties();Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor().newInstance();interceptorInstance.setProperties(properties);//注册插件configuration.addInterceptor(interceptorInstance);}}}
    
      public void addInterceptor(Interceptor interceptor) {interceptorChain.addInterceptor(interceptor);}
    
    public class InterceptorChain {//插件列表private final List<Interceptor> interceptors = new ArrayList<>();//应用插件public Object pluginAll(Object target) {for (Interceptor interceptor : interceptors) {target = interceptor.plugin(target);}return target;}//注册插件public void addInterceptor(Interceptor interceptor) {interceptors.add(interceptor);}public List<Interceptor> getInterceptors() {return Collections.unmodifiableList(interceptors);}}
    
    • 通过Bean注入的方式注册拦截器的方式代码不在此处
  2. 应用插件

    //Configuration类中public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);//应用插件,拦截ParameterHandlerparameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);return parameterHandler;}public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,ResultHandler resultHandler, BoundSql boundSql) {ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);//应用插件,拦截ResultSetHandlerresultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);return resultSetHandler;}public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);//应用插件,拦截StatementHandlerstatementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);return statementHandler;}public Executor newExecutor(Transaction transaction) {return newExecutor(transaction, defaultExecutorType);}public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? defaultExecutorType : executorType;executorType = executorType == null ? ExecutorType.SIMPLE : executorType;Executor executor;if (ExecutorType.BATCH == executorType) {executor = new BatchExecutor(this, transaction);} else if (ExecutorType.REUSE == executorType) {executor = new ReuseExecutor(this, transaction);} else {executor = new SimpleExecutor(this, transaction);}if (cacheEnabled) {executor = new CachingExecutor(executor);}//应用插件,拦截Executorexecutor = (Executor) interceptorChain.pluginAll(executor);return executor;}
    
    public class InterceptorChain {private final List<Interceptor> interceptors = new ArrayList<>();//应用拦截器,对目标对象进行拦截。target为ParameterHandler、ResultSetHandler、StatementHandler、Executorpublic Object pluginAll(Object target) {for (Interceptor interceptor : interceptors) {//链式拦截//遍历所有插件,对target进行拦截后,返回值为target,target将被下一个拦截器拦截target = interceptor.plugin(target);}return target;}
    }
    
    /*** @author Clinton Begin*/
    public interface Interceptor {Object intercept(Invocation invocation) throws Throwable;//拦截器拦截default Object plugin(Object target) {//拦截器拦截return Plugin.wrap(target, this);}default void setProperties(Properties properties) {// NOP}}
    
  3. 生成插件拦截代理类

    //Plugin类中  
    public static Object wrap(Object target, Interceptor interceptor) {//1.构建签名映射表signatureMap。//记录拦截器所关注的方法签名及其对应的拦截逻辑Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);//2.确定目标对象接口Class<?> type = target.getClass();//找出所有需要被代理的接口Class<?>[] interfaces = getAllInterfaces(type, signatureMap);//3.创建代理对象if (interfaces.length > 0) {//该类,在signatureMap中有需要被拦截的方法才生成代理类return Proxy.newProxyInstance(type.getClassLoader(),interfaces,new Plugin(target, interceptor, signatureMap));}return target;}
    
    • 构建签名映射表
      • K:StatementHandler.class、Executor.class、ParameterHandler.class、ResultSetHandler.class
      • V:update, query, flushStatements, commit, rollback…等
    //Plugin类中  
    private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);if (interceptsAnnotation == null) {throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());}//1.提取@Intercepts注解中的签名数组Signature[] sigs = interceptsAnnotation.value();Map<Class<?>, Set<Method>> signatureMap = new HashMap<>();for (Signature sig : sigs) {Set<Method> methods = MapUtil.computeIfAbsent(signatureMap, sig.type(), k -> new HashSet<>());try {//2.获取拦截的类的方法。该方法是最终被拦截的方法Method method = sig.type().getMethod(sig.method(), sig.args());methods.add(method);} catch (NoSuchMethodException e) {throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);}}return signatureMap;}
    

    找出所有需要被代理的接口

    //Plugin类中
    private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {Set<Class<?>> interfaces = new HashSet<>();while (type != null) {for (Class<?> c : type.getInterfaces()) {if (signatureMap.containsKey(c)) {interfaces.add(c);}}type = type.getSuperclass();}return interfaces.toArray(new Class<?>[0]);
    }
    
  4. 执行拦截

    //Plugin是一个InvocationHandler
    public class Plugin implements InvocationHandler {private final Object target;private final Interceptor interceptor;private final Map<Class<?>, Set<Method>> signatureMap;private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {this.target = target;this.interceptor = interceptor;this.signatureMap = signatureMap;}//执行拦截@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {Set<Method> methods = signatureMap.get(method.getDeclaringClass());//1.找到被拦截的方法if (methods != null && methods.contains(method)) {//2.执行拦截逻辑//interceptor.intercept方法由用户实现return interceptor.intercept(new Invocation(target, method, args));}return method.invoke(target, args);} catch (Exception e) {throw ExceptionUtil.unwrapThrowable(e);}}
    }  
    
  5. 小结

    1. 被拦截的类和方法

      拦截的类拦截的方法
      Executorupdate, query, flushStatements, commit, rollback,getTransaction, close, isClosed
      ParameterHandlergetParameterObject, setParameters
      StatementHandlerprepare, parameterize, batch, update, query
      ResultSetHandlerhandleResultSets, handleOutputParameters
    2. 代码执行链路

      parseConfiguration-解析SqlMapConfig.xml
      pluginElement-解析插件
      configuration.addInterceptor-注册拦截器
      interceptorChain.addInterceptor-加入拦截器列表
      InterceptorChain.pluginAll-应用全部插件
      Interceptor.plugin-组装递归式插件链
      Plugin.wrap-组装单个插件
      interceptorChain.pluginAll-拦截parameterHandler
      interceptorChain.pluginAll-拦截resultSetHandler
      interceptorChain.pluginAll-拦截statementHandler
      interceptorChain.pluginAll-拦截executor
      getSignatureMap-构建签名映射表
      getAllInterfaces-需要被代理的接口
      Proxy.newProxyInstance-创建代理
      interceptsAnnotation.value-提取Intercepts注解中的签名数组
      getMethod-获取拦截的类的方法.该方法是最终被拦截的方法
      Plugin.invoke-触发代理类拦截
      interceptor.intercept-用户自定义的插件拦截器方法
    3. 核心类图

      在这里插入图片描述

      • Interceptor:拦截器,定义了拦截逻辑,以及拦截器链的注册。
      • InterceptorChain:拦截器链,将多个拦截器组成拦截器链。
      • Plugin:Plugin 则是一种利用 Interceptor 实现的插件机制,它可以对指定的目标对象进行代理,并在方法调用前后插入额外的逻辑
      • Invocation:封装了被拦截方法的信息,包括被拦截方法的对象,被拦截方法的方法,被拦截方法的参数。
      • Plugin 是基于 Interceptor 实现的插件机制,而 Interceptor 是实现拦截器功能的接口。

自定义插件

  1. MyBaits的Interceptor定义

    public interface Interceptor {Object intercept(Invocation invocation) throws Throwable;default Object plugin(Object target) {return Plugin.wrap(target, this);}default void setProperties(Properties properties) {// NOP}}
    
    • intercept方法:核心方法,最终会被调用,执行自定义的拦截逻辑。
    • plugin方法:⽣成target的代理对象
    • setProperties方法:传递插件所需参数,插件初始化的时候调⽤,也只调⽤⼀次
  2. 自定义Interceptor实现类

    @Intercepts({//注意看这个⼤花括号,也就这说这⾥可以定义多个@Signature对多个地⽅拦截,都⽤这个拦截器@Signature(type = StatementHandler.class,//这是指拦截哪个接⼝method = "prepare",//这个接⼝内的哪个⽅法名,不要拼错了args = {Connection.class, Integer.class})// 这是拦截的⽅法的⼊参,按顺序写到这,不要多也不要少,如果⽅法重载,可是要通过⽅法名和⼊参来确定唯⼀的
    })
    public class MyPlugin implements Interceptor {/*拦截方法:只要被拦截的目标对象的目标方法被执行时,每次都会执行intercept方法*/@Overridepublic Object intercept(Invocation invocation) throws Throwable {System.out.println("对方法进行了增强....");return invocation.proceed(); //原方法执行}/*** 包装⽬标对象 为⽬标对象创建代理对象,会将当前Interceptor也包装进去** @param target 要拦截的对象* @return 代理对象*/@Overridepublic Object plugin(Object target) {Object wrap = Plugin.wrap(target, this);return wrap;}/*获取配置文件的参数插件初始化的时候调⽤,也只调⽤⼀次,插件配置的属性从这⾥设置进来*/@Overridepublic void setProperties(Properties properties) {System.out.println("获取到的配置文件的参数是:" + properties);}
    }
    
  3. sqlMapConfig.xml中配置插件,启用自定义的Interceptor实现类

        <plugins><plugin interceptor="com.muchfish.plugin.MyPlugin"><property name="name" value="tom"/></plugin></plugins>
    
  4. 测试

    public class PluginTest {@Testpublic void test() throws IOException {InputStream resourceAsStream =Resources.getResourceAsStream("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = newSqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession();IUserDao userMapper = sqlSession.getMapper(IUserDao.class);List<User> byPaging = userMapper.findAllUserAndRole();for (User user : byPaging) {System.out.println(user);}}
    }
    

pageHelper分⻚插件

  1. 导⼊通⽤PageHelper的坐标

            <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>3.7.5</version></dependency><dependency><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId><version>0.9.1</version></dependency>
    
  2. 在mybatis核⼼配置⽂件中配置PageHelper插件

        <plugins><!--   <plugin interceptor="com.muchfish.plugin.MyPlugin"><property name="name" value="tom"/></plugin>--><!--注意:分⻚助⼿的插件 配置在通⽤馆mapper之前*--><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/></plugin></plugins>
    
  3. 测试分⻚数据获取

        @Testpublic void testPageHelper() throws IOException {InputStream resourceAsStream =Resources.getResourceAsStream("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = newSqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession();IUserDao userMapper = sqlSession.getMapper(IUserDao.class);//设置分⻚参数PageHelper.startPage(1, 2);List<User> select = userMapper.findAllUserAndRole();for (User user : select) {System.out.println(user);}}
    

通⽤ mapper

基于插件实现单表增删改查?

  1. 导⼊通⽤tk.mybatis的坐标

    <!--通用mapper-->
    <dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>3.1.2</version>
    </dependency>
    
  2. 在mybatis核⼼配置⽂件中配置tk.mybatis插件

        <plugins><plugin interceptor="com.muchfish.plugin.MyPlugin"><property name="name" value="tom"/></plugin><!--注意:分⻚助⼿的插件 配置在通⽤馆mapper之前*--><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/></plugin><plugin interceptor="tk.mybatis.mapper.mapperhelper.MapperInterceptor"><!--指定当前通用mapper接口使用的是哪一个--><property name="mappers" value="tk.mybatis.mapper.common.Mapper"/></plugin></plugins>
    
  3. 实体类设置主键

    @Table(name = "user")
    public class UserDO {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;private String username;private String password;private String birthday;//...省略getter/setter
    }    
    
  4. 定义Dao继承Mapper类

    import com.muchfish.pojo.UserDO;
    import tk.mybatis.mapper.common.Mapper;public interface UserMapper extends Mapper<UserDO> {
    }
    
  5. 测试

        @Testpublic void mapperTest() throws IOException {InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);UserDO user = new UserDO();user.setId(1);UserDO user1 = mapper.selectOne(user);System.out.println(user1);//2.example方法Example example = new Example(User.class);example.createCriteria().andEqualTo("id",1);List<UserDO> users = mapper.selectByExample(example);for (UserDO user2 : users) {System.out.println(user2);}}
    
  6. 通用Mapper拦截器部分源码

    /*** 通用Mapper拦截器** @author liuzh*/
    @Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
    })
    public class MapperInterceptor implements Interceptor {private final MapperHelper mapperHelper = new MapperHelper();@Overridepublic Object intercept(Invocation invocation) throws Throwable {Object[] objects = invocation.getArgs();MappedStatement ms = (MappedStatement) objects[0];String msId = ms.getId();//不需要拦截的方法直接返回if (mapperHelper.isMapperMethod(msId)) {//第一次经过处理后,就不会是ProviderSqlSource了,一开始高并发时可能会执行多次,但不影响。以后就不会在执行了if (ms.getSqlSource() instanceof ProviderSqlSource) {mapperHelper.setSqlSource(ms);}}return invocation.proceed();}@Overridepublic Object plugin(Object target) {if (target instanceof Executor) {return Plugin.wrap(target, this);} else {return target;}}@Overridepublic void setProperties(Properties properties) {mapperHelper.setProperties(properties);}
    }
      
    // MapperHelper类中/*** 注册的通用Mapper接口*/private Map<Class<?>, MapperTemplate> registerMapper = new ConcurrentHashMap<Class<?>, MapperTemplate>();/*** 判断当前的接口方法是否需要进行拦截** @param msId* @return*/public boolean isMapperMethod(String msId) {if (msIdSkip.get(msId) != null) {return msIdSkip.get(msId);}for (Map.Entry<Class<?>, MapperTemplate> entry : registerMapper.entrySet()) {if (entry.getValue().supportMethod(msId)) {msIdSkip.put(msId, true);return true;}}msIdSkip.put(msId, false);return false;}
    
      // MapperHelper类中/*** 注册的通用Mapper接口*/private Map<Class<?>, MapperTemplate> registerMapper = new ConcurrentHashMap<Class<?>, MapperTemplate>();/*** 配置属性** @param properties*/
    public void setProperties(Properties properties) {if (properties == null) {return;}String UUID = properties.getProperty("UUID");if (UUID != null && UUID.length() > 0) {setUUID(UUID);}//...省略//注册通用接口String mapper = properties.getProperty("mappers");if (mapper != null && mapper.length() > 0) {String[] mappers = mapper.split(",");for (String mapperClass : mappers) {if (mapperClass.length() > 0) {registerMapper(mapperClass);}}}
    }/*** 注册通用Mapper接口** @param mapperClass* @throws Exception*/public void registerMapper(Class<?> mapperClass) {if (!registerMapper.containsKey(mapperClass)) {registerMapper.put(mapperClass, fromMapperClass(mapperClass));}//自动注册继承的接口Class<?>[] interfaces = mapperClass.getInterfaces();if (interfaces != null && interfaces.length > 0) {for (Class<?> anInterface : interfaces) {registerMapper(anInterface);}}}
    
    • 只增强了Executor
    • MapperInterceptor.setProperties进行了mapper注册
    • 会在注册的mapper中进行匹配,判断是否对该MappedStatementId进行拦截

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/790939.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

『51单片机』蜂鸣器

&#x1f6a9; WRITE IN FRONT &#x1f6a9; &#x1f50e; 介绍&#xff1a;"謓泽"正在路上朝着"攻城狮"方向"前进四" &#x1f50e;&#x1f3c5; 荣誉&#xff1a;2021|2022年度博客之星物联网与嵌入式开发TOP5|TOP4、2021|2222年获评…

OpenHarmony实战:轻量级系统之配置其他子系统

除上述子系统之外&#xff0c;还有一些必要但是无需进行移植的子系统。如&#xff1a;分布式任务调度子系统、DFX子系统。 这些子系统添加方式比较简单&#xff0c;在“vendor/MyVendorCompany/MyProduct/config.json”文件中进行如下配置即可&#xff1a; {"subsystem&…

专有钉钉微应用埋点以及本地调试埋点总结

最近在对接浙政钉&#xff0c;稳定性监控、通用采集 SDK、基础埋点、基础埋点&#xff0c;每次发布上去&#xff0c;工作人员那边反馈抓取不到信息 稳定性监控代码、通用采集 SDK index.html <!-- 流量稳定监控 S 关于埋点上线打开--><script src"https://wpk-…

IDEA 中能提高开发效率的插件

目录 前言 插件 Rainbow Brackets AceJump POJO to JSON Json Helper MybatisX Maven Helper PlantUML Integration TONYYI Lingma 前言 IDEA 里又很多好用的插件可以帮助我们提升开发效率&#xff0c;这里罗列下自己开发过程中常用的插件&#xff0c;善于利用插件&…

【第十一届大唐杯全国大学生新一代信息通信技术大赛】赛题分析

赛道一 一等奖 7% 二等奖 15% 三等奖 25% 赛道二 参考文档&#xff1a; 《第十一届大唐杯全国大学生新一代信息通信技术大赛&#xff08;产教融合5G创新应用设计&#xff09;专项赛说明.pdf》 一等奖&#xff1a;7% 二等奖&#xff1a;10% 三等奖&#xff1a;20% 赛项一&am…

unity工程输出的log在哪里?

在编辑器里进行活动输出的log位置&#xff1a; C:\Users\username\AppData\Local\Unity\Editor\Editor.log ------------------------------------ 已经打包完成&#xff0c;形成的exe运行后的log位置&#xff1a; C:\Users\xxx用户\AppData\LocalLow\xx公司\xx项目

销售与营销的区别:从手中到心中

一、引言 在商界&#xff0c;销售和营销常常被视为同义词&#xff0c;但实际上它们各自扮演着不同的角色。简而言之&#xff0c;销售是将产品送到客户手里&#xff0c;而营销则是将产品送到客户心里。这种微妙的差异对于企业的成功至关重要。正如彼得德鲁克所说&#xff1a;“…

Redis安装-Docker

安装redis的docker容器 1、创建redis挂载目录 mkdir -p /liuchaoxu/redis/{data,conf}2、复制配置文件 在 /liuchaoxu/redis/conf 目录中创建文件 redis.conf&#xff0c;文件从 redis-6.2.7.tar.gz 中解压获取 修改默认配置(从上至下依次)&#xff1a; #bind 127.0.0.1 …

游戏引擎中的物理应用

一、 角色控制器 Character Controller和普通的动态对象&#xff08;Dynamic Actor &#xff09;是不同的&#xff0c;主要的三个特点是: 它拥有可控制的刚体间的交互假设它是有无穷的摩擦力&#xff08;可以站停在位置上&#xff09;&#xff0c;没有弹性加速和刹车几乎立即…

《QT实用小工具·十》本地存储空间大小控件

1、概述 源码放在文章末尾 本地存储空间大小控件&#xff0c;反应电脑存储情况&#xff1a; 可自动加载本地存储设备的总容量/已用容量。进度条显示已用容量。支持所有操作系统。增加U盘或者SD卡到达信号。 下面是demo演示&#xff1a; 项目部分代码如下&#xff1a; #if…

vue项目引入微信sdk: npm install weixin-js-sdk --save报错

网上查到要用淘宝的镜像 同事告知旧 域名&#xff1a;https://registry.npm.taobao.org/已经不能再使用 使用 npm config set registry http://registry.npmmirror.com

css心跳动画

图标引入 <img class"icon" src"heart.svg" alt"" srcset""> CSS代码 <style>.icon {animation:bpm 1s linear,pulse 0.75s 1s linear infinite;}keyframes pulse {from,75%,to {transform: scale(1);}25% {transform:…

极简云验证 download.php 文件读取漏洞复现

0x01 产品简介 极简云验证是一款开源的网络验证系统&#xff0c;支持多应用卡密生成&#xff1a;卡密生成 单码卡密 次数卡密 会员卡密 积分卡密、卡密管理 卡密长度 卡密封禁 批量生成 批量导出 自定义卡密前缀等&#xff1b;支持多应用多用户管理&#xff1a;应用备注 应用版…

智能仪器驱动企业数字化转型 迈向智慧未来!

在当今数字化时代&#xff0c;企业正面临着前所未有的挑战和机遇。为了在竞争激烈的市场中立足并实现可持续发展&#xff0c;数字化转型已成为企业的当务之急。智能仪器作为数字化转型的核心驱动力&#xff0c;以其卓越的性能和创新的技术&#xff0c;为企业开启了通向智慧未来…

C_C++数据的在内存中的分布

C/C内存分布 在编程世界中&#xff0c;C和C语言一直以其强大的性能和灵活性著称。然而&#xff0c;这种强大和灵活的背后&#xff0c;离不开对内存分布的深入理解和熟练掌握。本文将详细介绍C/C程序中的内存分布&#xff0c;包括栈、堆和全局变量的存储区域。下面是c/c中&…

基于OrangePi Zero2的智能家居项目(开发阶段)

智能家居项目的软件实现 紧接上文 基于OrangePi Zero2的智能家居项目&#xff08;准备阶段&#xff09;-CSDN博客 目录 一、项目整体设计 1.1项目整体设计 1.2具体划分 二、开发工作的前期准备 1、进行分类&#xff0c;并用Makefile文件进行管理 参考&#xff1a;自己创…

基于单片机的智能报站系统仿真设计

**单片机设计介绍&#xff0c;基于单片机的智能报站系统仿真设计 文章目录 一 概要二、功能设计设计思路 三、 软件设计原理图 五、 程序六、 文章目录 一 概要 基于单片机的智能报站系统仿真设计概要是关于采用单片机技术实现公交车报站功能的系统设计概述。以下是对该设计的…

Unity 学习日记 13.地形系统

下载源码 UnityPackage 1.地形对象Terrain 目录 1.地形对象Terrain 2.设置地形纹理 3.拔高地形地貌 4. 绘制树和草 5.为地形加入水 6.加入角色并跑步 7.加入水声 右键创建3D地形&#xff1a; 依次对应下面的按钮 || 2.设置地形纹理 下载资源包 下载资源包后&#x…

vue2+element-ui 实现OSS分片上传+取消上传

遇到问题&#xff1a;项目中需要上传500MB以上的视频。一开始使用上传组件el-upload&#xff0c;调用后台接口&#xff0c;但是出现了onprogress显示百分百后接口一直pending&#xff0c;过了很多秒后接口才通&#xff0c;如果遇到大文件的话&#xff0c;接口就会报超时。 解决…

基于卷积神经网络的苹果等级分类系统(pytorch框架)【python源码+UI界面+前端界面+功能源码详解】

功能演示&#xff1a; 苹果等级分类系统&#xff0c;基于vgg16&#xff0c;resnet50卷积神经网络&#xff08;pytorch框架&#xff09;_哔哩哔哩_bilibili &#xff08;一&#xff09;简介 基于卷积神经网络的苹果等级分类系统是在pytorch框架下实现的&#xff0c;系统中有两…