• mybatis:@Intercepts小例子(一)


    这只是一个纯碎的mybatis的只针对@Intercepts应用的小列子,没有和spring做集成。

    1.工作目录

    2.数据库mysql,建立一个数据库表、实体对象User、UserMapper.java、UserMapper.xml省略。

    使用mybatis自动代码生成工具生成:mybatis-generator-core-1.3.2。(此处略)

    3.拦截器MyInterceptor.java

    package com.tiantian.mybatis.interceptor;
    
    import java.sql.Connection;
    import java.util.Properties;
    import org.apache.ibatis.executor.Executor;
    import org.apache.ibatis.executor.statement.StatementHandler;
    import org.apache.ibatis.mapping.MappedStatement;
    import org.apache.ibatis.plugin.Interceptor;
    import org.apache.ibatis.plugin.Intercepts;
    import org.apache.ibatis.plugin.Invocation;
    import org.apache.ibatis.plugin.Plugin;
    import org.apache.ibatis.plugin.Signature;
    import org.apache.ibatis.session.ResultHandler;
    import org.apache.ibatis.session.RowBounds;
    @Intercepts( {
        @Signature(method = "query", type = Executor.class, args = {
               MappedStatement.class, Object.class, RowBounds.class,
               ResultHandler.class }),
        @Signature(method = "prepare", type = StatementHandler.class, args = { Connection.class }) })
    public class MyInterceptor implements Interceptor {
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            Object result = invocation.proceed();
            System.out.println("Invocation.proceed()");
            return result;
        }
    
        @Override
        public Object plugin(Object target) {
            // TODO Auto-generated method stub
            return Plugin.wrap(target, this);
        }
    
        @Override
        public void setProperties(Properties properties) {
            String prop1 = properties.getProperty("prop1");
            String prop2 = properties.getProperty("prop2");
            System.out.println(prop1 + "------" + prop2);
        }
    }

    4.配置文件mybatis-config.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 resource="jdbc.properties"></properties>
        <typeAliases>
           <package name="com.tiantian.mybatis.model"/>
        </typeAliases>
        <plugins>
           <plugin interceptor="com.tiantian.mybatis.interceptor.MyInterceptor">
               <property name="prop1" value="prop1"/>
               <property name="prop2" value="prop2"/>
           </plugin>
        </plugins>
        <environments default="development">
           <environment id="development">
               <transactionManager type="JDBC" />
               <dataSource type="POOLED">
                  <property name="driver" value="${driver}" />
                  <property name="url" value="${url}" />
                  <property name="username" value="${username}" />
                  <property name="password" value="${password}" />
               </dataSource>
           </environment>
        </environments>
        <mappers>
           <mapper resource="com/tiantian/mybatis/mapper/UserMapper.xml"/>
        </mappers>
    </configuration>

    5.配置文件jdbc.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/database_yxl
    username=root
    password=123456
    #定义初始连接数
    initialSize=0
    #定义最大连接数
    maxActive=20
    #定义最大空闲
    maxIdle=20
    #定义最小空闲
    minIdle=1
    #定义最长等待时间
    maxWait=60000

    6.测试文件TestMyBatis.java

    package com.tiantian.mybatis.service;
    
    import org.apache.ibatis.session.SqlSession;
    import com.tiantian.base.MyBatisUtil;
    import com.tiantian.mybatis.domain.User;
    
    public class TestMyBatis {
        public static void main(String[] args) {
            SqlSession session = MyBatisUtil.getSqlSession();
            /**
             * 映射sql的标识字符串,
             * com.tiantian.mybatis.mapper.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
             * selectByPrimaryKey是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
             */
            String statement = "com.tiantian.mybatis.mapper.UserMapper.selectByPrimaryKey";//映射sql的标识字符串
            //执行查询返回一个唯一user对象的sql
            User user = session.selectOne(statement, 1);
            System.out.println(user);
        }
    }

    输出结果:

    prop1------prop2
    Invocation.proceed()
    Invocation.proceed()
    [id:1;username:测试;password:sfasgfaf]

    7.工具类MyBatisUtil.java

    package com.tiantian.base;
    
    import java.io.InputStream;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class MyBatisUtil {
        public static SqlSessionFactory getSqlSessionFactory() {
            String resource = "mybatis-config.xml";
            // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
            InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource);
            // 构建sqlSession的工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
            return sessionFactory;
        }
    
        public static SqlSession getSqlSession() {
            return getSqlSessionFactory().openSession();
        }
    
        public static SqlSession getSqlSession(boolean isAutoCommit) {
            return getSqlSessionFactory().openSession(isAutoCommit);
        }
    }

    参考文章

    http://www.tuicool.com/articles/7bYjUn

    http://www.cnblogs.com/xdp-gacl/p/4262895.html

    http://www.mybatis.org/mybatis-3/zh/index.html

  • 相关阅读:
    linux脚本mysql服务与keepalived服务高可用脚本
    linux脚本启动服务脚本
    linux脚本数据库备份脚本
    linux脚本清理日志文件脚本
    最大数字
    求阶乘
    素数
    水仙花数
    js基础1
    css常用属性2
  • 原文地址:https://www.cnblogs.com/xingxing0521/p/5310965.html
Copyright © 2020-2023  润新知