MyBatis

mybatis 查询时间戳类型(TIMESTAMP) 回显成时间字符串的问题

最后都变了- 提交于 2020-04-04 04:53:11
mybatis在select查询TIMESTAMP类型的时间时,如果resultType="java.util.HashMap",返回的map中时间的类型仍是TIMESTAMP类型, 想要回显成想要的字符串格式,则用时间函数 DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') 此方法适用于连表查询时 想返回想要的时间串类型 <select id="selectByUserGroupId" parameterType="String" resultType="java.util.HashMap"> SELECT u.user_id userId, u.dept_id deptId, u.user_name userName, u.user_acc userAcc, u.user_pwd userPwd, u.user_salt userSalt, u.user_state userState, u.user_company userCompany, u.user_mobile userMobile, u.user_email userEmail, u.user_last_login_time userLastLoginTime, DATE_FORMAT(u.create_time,'%Y-%m-%d %H:%i:%s') createTime, u

mybatis 时间区间比较

六眼飞鱼酱① 提交于 2020-04-04 04:52:43
直接上代码,此时数据库使用的Date类型:     <if test="minCreateTime != null and minCreateTime != ''"> <![CDATA[ and g.create_time >= to_date(#{minCreateTime,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss')]]> </if> <if test="maxCreateTime != null and maxCreateTime != ''"> <![CDATA[ and g.create_time <= to_date(#{maxCreateTime,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss')]]> </if> 若是字符串类型,也可以直接比较,如下:        <if test="createTime != null"> AND CREATE_TIME = CONCAT(CONCAT('%', #{createTime,jdbcType=DATE}), '%') </if> <if test="updateTime != null"> AND UPDATE_TIME = CONCAT(CONCAT('%', #{updateTime,jdbcType=DATE}), '%') </if> <if

MyBatis自学(3):MyBatis逆向工程

和自甴很熟 提交于 2020-04-04 00:55:08
  什么是Mybatis逆向工程?   可以针对单表自动生成MyBatis执行所需要的代码,包括:Mapper.java,Mapper.xml,实体类。    为什么要使用Mybatis逆向工程?   我们之前说过MyBatis是一个"半自动"的ORM框架,SQL语句需要开发者自定义,这样做的好处是代码更加灵活,缺点是如果参与业务的表太多,每张表的业务都需要自定义SQL,创建实体类,DAO接口,难免会很麻烦。所以我们需要使用逆向工程,让MyBatis自动生成表对应的各种资源,大大减少我们的工作量。    Mybatis逆向工程有什么不足?   逆向工程有它自身的局限性,逆向工程方法只能执行一次,如果再次执行就会重复生成对应的DAO接口,实体类等资源。如果需要对表结构进行修改,那么就必须删除已经生成的所有资源,重新生成一次。    如何使用Mybatis逆向工程?   MyBatis Generator,简称MBG:是一个专门为MyBatis框架开发者定制的代码生成器,可以根据表结构快速生成对应的Mapper.xml,Mapper接口以及实体类。支持基本的CRUD(Create,Read,Update,Delete),但是复杂的SQL语句需要开发者手动编写。   1. 添加依赖jar包到pom.xml文件。 1 <dependency> 2 <groupId>org.mybatis

Mybatis的逆向工程

妖精的绣舞 提交于 2020-04-04 00:54:06
Mybatis的逆向工程可以对数据库进行单表操作,自动生成相应的mapper.java,mapper.xml,相应实体类。 需要导入相应的jar包 先要配置相应的xml文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 生成时没有注释 --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 配置数据库的连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/student" userId=

myBatis实例

不羁的心 提交于 2020-04-03 23:02:35
一.搭建环境, 建立数据库 1 CREATE TABLE user( 2 id int(11) not NULL AUTO_INCREMENT, 3 userName varchar(50) DEFAULT NULL, 4 userAge int(11) DEFAULT NULL, 5 userAddress varchar(200) DEFAULT NULL, 6 PRIMARY KEY(id) 7 )ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 建立一个myBatis的java项目(不需要web项目) 然后倒入jar包 建总配置文件: 在src下建configration包.在建Configurations.xml文件 代码如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC " -//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <typeAliases> 6 <typeAlias type="com.user.User" alias="User"/> 7 <

Mybatis实例

你离开我真会死。 提交于 2020-04-03 21:59:52
数据库中:USERT t , WEBINFOR t(表中有一列关联usert某列) 建模型,分析好哪个表是一对一(webinfo ),哪个表是一对多(usert)(一条数据对另一个表中多条数据). 对象级联 建包 建xml和interface接口类 xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 第一种方式:结果是从一对一这个表(WebinfoMapper)查看 对象级联 方法查询 WebinfoMapper.xml <mapper namespace="com.hanqi.mapper.WebInfoMapper">//复制的对应的接口里面的限定名 <resultMap type="webInfo" id="webinfoResult2"> <id property="ids" column="IDS"/> <association property="usert" column="USERID" select="com.hanqi.mapper.UsertMapper.selectUsertById" /> <

MyBatis 实例

筅森魡賤 提交于 2020-04-03 21:58:49
确定依赖 <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j</artifactId> <version>2.11.2</version> <type>pom</type> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> 确定目前的目录结构 编写log4j配置文件 编写log4j配置文件

Mybatis(二):怎样使用Mybatis

无人久伴 提交于 2020-04-02 23:12:27
一、创建项目(本文以Idea基于Maven构建的项目为例) New——>Project 笔者这里是选择自己本地的Maven及配置 最后点击Finish即可 二、在配置文件中添加依赖包 pom.xml配置文件中添加Mybatis、JDBC驱动、log4j日志管理的包依赖 完整代码如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhurouwangzi</groupId> <artifactId>MybatisDemo</artifactId> <version>1.0-SNAPSHOT</version> <name>MybatisDemo</name> <!-- FIXME change it to the project's website --

mybatis入门

孤者浪人 提交于 2020-04-02 21:50:23
1.什么是MyBatis ? 亲爱的度娘是这样说的: MyBatis 本是 apache 的一个开源项目 iBatis , 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。 iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的 持久层 框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO) 我们把Mybatis的功能架构分为三层: (1)API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。 (2)数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。 (3)基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。 2.MyBatis 的入门案例 源码介绍: 1.jar包 2.其次,我们要准备mybatis-config.xml(mybatis的配置文件) <?xml version="1.0"

SpringBoot集成Mybatis

我的梦境 提交于 2020-04-02 20:45:15
SpringBoot底层是对Spring+SpringMVC的封装,如果使用Mybatis需要与SpringBoot进行整合 Spring整合Mybatis的回顾 1.导入jar 不在dependencies内,独立存在 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>   导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--mybatis的启动器--><dependency>