zoukankan      html  css  js  c++  java
  • mybatis查询日期和log4j2配置

    查询数据库表中某一个日期的所有数据,通过传入日期参数来查询。数据库表结构如下:

    mybatis配置

    在web.xml中引用spring-mybatis.xml配置

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:conf/spring-mybatis.xml</param-value>
        </context-param>

    在spring-mybatis.xml中引入数据库操作mapping文件和mybaits配置文件

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
    
            <property name="typeAliasesPackage" value="com.risk.model"/>
            <property name="mapperLocations" value="classpath*:mybatis/*.xml"/>
            <property name="configLocation" value="classpath:conf/mybatis-config.xml" />
    
        </bean>

    在mybatis-config.xml中设置log4j2的配置

    <?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>
        <settings>
            <setting name="logImpl" value="LOG4J2"/>
        </settings>
    
    </configuration>

    查询日期xml和代码

    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.risk.dao.HighLimitDao">
        <resultMap type="com.risk.entity.HighLimitEntity" id="hResultMap" >
            <id property="index" column="index"/>
            <result property="date" jdbcType="DATE" column="date"  javaType="java.util.Date" />
            <result property="open" column="open"/>
            <result property="high" column="high"/>
            <result property="low" column="low"/>
            <result property="volume" column="volume"/>
        </resultMap>    
    
        <select id="query" parameterType="java.util.HashMap" resultMap="hResultMap">
            select *
            from limit
            where 1=1
            <if test="date != null">
                AND date=#{date}
            </if>
        </select>
    </mapper>

    对应的java实现类

    public class HighLimitEntity {
        private Long index;
        private Date date;
        private String code;
        private Double open;
        private Double close;
        private Double high;
        private Double low;
    }
    
    @Repository
    public class HighLimitDaoImpl implements HighLimitDao {
        @Autowired
        private SqlSessionFactory sqlSessionFactory;
    
        public List<HighLimitEntity> query(Map<String, Object> params){
            SqlSession sqlSession = sqlSessionFactory.openSession();
            List<HighLimitEntity> entities = sqlSession.selectList("com.risk.dao.HighLimitDao.query", params);
            sqlSession.close();
            return entities;
        }
    }

    Map<String, Object> params = new HashMap<String, Object>();

    Date date = new Date();
    Date target = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    try {
    String dateString = dateFormat.format(date);
    System.out.println("date:" + dateString);
    target = dateFormat.parse(dateString);
    System.out.println("date:" + target);
    params.put("date", target);
    }catch (Exception e){
    e.printStackTrace();
    return null;
    }
    highLimitDao.query(params)
     

    log4j2.xml

    如果想增加调试日志,看Mybatis中有什么错误,可以在log4j2中定义sql查询的日志,打印出查询过程。log4j2.xml中定义了dao的路径,level是debug,这样就可以打印出查询语句到日志中

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration status="OFF">
        <appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
            <File name="File" fileName="../logs/test.log" append="true">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
            </File>
        </appenders>
    
        <loggers>
            <logger name="com.risk.test" level="info" additivity="false">
                <appender-ref ref="Console"/>
            </logger>
            <logger name="log4j.logger.java.sql.Statement" level="info" additivity="false">
                <appender-ref ref="File"/>
            </logger>
            <Logger name="com.risk.dao" level="debug" additivity="false">
                <AppenderRef ref="File"/>
            </Logger>
    
            <root level="info">
                <appender-ref ref="Console"/>
                <appender-ref ref="File"/>
            </root>
        </loggers>
    </configuration>
    View Code
  • 相关阅读:
    5 -- Hibernate的基本用法 --5 1 持久化类的要求
    5 -- Hibernate的基本用法 --5 深入理解持久化对象
    Java -- POI -- 随笔汇总
    jdk 自带的数据库Derby使用
    SpringMVC中 -- @RequestMapping的作用及用法
    Hibernate -- Dao层 -- CURD -- 随记
    JAVA WEB -- request
    SpringMVC -- @RequestMapping -- 随记
    Navicat -- Oracle -- 错误锦集
    Tomcat -- 启动错误 -- 解决锦集
  • 原文地址:https://www.cnblogs.com/beilong/p/7500203.html
Copyright © 2011-2022 走看看