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>

时间: 2024-08-06 03:41:59

mybatis查询日期和log4j2配置的相关文章

mybatis查询日期时间数据得到long类型数据的问题

使用mybatis查询数据时,如果数据库存储的是timestamp.datetime.date.time等时间类型,而Java bean也使用的是date类型,mybatis会自动将date类型转换为unix long时间,而不是时间格式. 解决方式有两种: 1.将Java bean 中的类型改为String类型. 2.在java bean 中date类型的get方法上加上注解@JsonFormat jackson中有一个@JsonFormat注解,将它配置到Date类型的get方法上后,jac

MyBatis框架中Mapper映射配置的使用及原理解析(三) 配置篇 Configuration

从上文<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 我们知道XMLConfigBuilder调用parse()方法解析Mybatis配置文件,生成Configuration对象. Configuration类主要是用来存储对Mybatis的配置文件及mapper文件解析后的数据,Configuration对象会贯穿整个Mybatis的执行流程,为Mybatis的执行过程提供必要的配

mybatis使用注解替代xml配置,动态生成Sql

mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectProvider来动态生成sql. 典型的使用场景 1. 无参数@SelectProvide方法在Mapper接口方法上和@SelectProvide指定类方法上,均无参数:UserMapper.java: 1     @SelectProvider(type = SqlProvider.class, 

【转】mybatis 自增主键配置

mybatis自增主键配置(?) mybatis进行插入操作时,如果表的主键是自增的,针对不同的数据库相应的操作也不同.基本上经常会遇到的就是Oracle Sequece 和 MySQL 自增主键,至于其他的手动生成唯一主键的问题在这里就不讨论了,这里主要说明下在mybatis中对于自增主键的配置. 不返回自增主键值 如果考虑到插入数据的主键不作为其他表插入数据的外键使用,那么可以考虑使用这种方式. Oracle Sequence 配置 <sql id='TABLE_NAME'>TEST_US

MyBatis Generator自动生成的配置及使用

注意:文件名不能有中文字符,不然不能自动生成 找到MyBatis Generator.rar\MyBatis Generator\eclipse里的features和plugins文件,把这两个文件复制到MyEclipse安装目录下dropins包里. 重新打开MyEclipse选中项目右键New→Other→MyBatis→选中MyBatis Generator ConfiGuration File→Next 配置generatorConfig.xml: <?xml version="1

Mybatis Generator.xml最完整配置详解

作者:小码哥Java学院 链接:http://www.jianshu.com/p/e09d2370b796 來源:简书 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://my

Mybatis分页插件PageHelper的配置和使用方法

http://www.cnblogs.com/kangoroo/p/7998433.html 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分页 一次性请求数据表格中的所有记录(ajax),然后在前端缓存并且计算count和分页逻辑,一般前端组件(例如dataTable)会提供分页动作. 特点是:简单,很适合小规模的web平台:当数据量大的时候会产生性能问题,在查询和网络传输的时间会很长. 后端分页 在aj

Spring整合MyBatis (使用扫描包配置mapper代理)

Spring整合MyBatis (使用扫描包配置mapper代理) pojo是根据表生成的实体类,属性名要跟字段名相同,不相同sql语句查询时用别名. 首先导jar包 实体类 public class User { private Integer id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 } 1 2 3

apache ignite系列(九):使用ddl和dml脚本初始化ignite并使用mybatis查询缓存

? 博客又断了一段时间,本篇将记录一下基于ignite对jdbc支持的特性在实际使用过程中的使用. 使用ddl和dml脚本初始化ignite 由于spring-boot中支持通过spring.datasource.schema属性指定初始化DDL脚本,spring.datasource.data指定初始化DML脚本.而ignite支持jdbc协议,测试了一下,发现一样可以通过该配置初始化ignite. spring.datasource.url=jdbc:ignite:thin://127.0.