『壹』 mybatis的sql查询返回数据类型有哪些
在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。
3.1 当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性是resultType的时
候,MyBatis对自动的给把对应的值赋给resultType所指定对象的属性。
『贰』 mybatis怎么读写clob类型的数据
<?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" > <mapper namespace="com.mzj..BizGovInfoMapper" > <resultMap id="BaseResultMap" type="com.mzj.model.BizGovInfo" > <id column="ID" property="id" jdbcType="CHAR" /> <result column="BAR_ID" property="bar_id" jdbcType="CHAR" /> <result column="INDEX_NUM" property="index_num" jdbcType="VARCHAR" /> <result column="PUB_UNIT" property="pub_unit" jdbcType="VARCHAR" /> <result column="INFO_NAME" property="info_name" jdbcType="VARCHAR" /> <result column="MENO" property="meno" javaType="string" jdbcType="CLOB" /> <result column="FILE_NUM" property="file_num" jdbcType="VARCHAR" /> <result column="CREATE_TIME" property="create_time" jdbcType="TIMESTAMP" /> <result column="STATE" property="state" jdbcType="CHAR" /> <result column="OPEN_LIMIT" property="open_limit" jdbcType="CHAR" /> <result column="INFO_ID" property="info_id" jdbcType="CHAR" /> </resultMap> <select id="findDataGrid" resultMap="BaseResultMap" parameterType="com.mzj.model.BizGovInfo"> select * from BIZ_GOV_INFO t <where> <if test="id != null and id !='' "> t.id = #{id} </if> </where> </select> </mapper>
『叁』 mybatis 框架 查询oracle数据表中的long类型的数据
恩,是的,类型不对,而且怎么可能用Long类型来接收CLOB数据呢!
『肆』 mybatis如何读取clob数据 详细过程
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)
2、CLOB
SQL CLOB 是内置类型,它将字符大对象 (Character Large Object) 存储为数据库表某一行中的一个列值。默认情况下,驱动程序使用 SQL locator(CLOB) 实现 Clob 对象,这意味着 CLOB 对象包含一个指向 SQL CLOB 数据的逻辑指针而不是数据本身。Clob 对象在它被创建的事务处理期间有效。
3、MyBatis对CLOB类型数据实现增删改查
oracle表结构
createtableT_USERS
(
IDNUMBERnotnull,
NAMEVARCHAR2(30),
SEXVARCHAR2(3),
BIRSDATE,
MESSAGECLOB
)
createsequenceSEQ_T_USERS_ID
minvalue1
maxvalue99999999
startwith1
incrementby1
cache20;
配置mybatis配置文件UsersMapper.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper
PUBLIC"-//mybatis.org//DTDMapper3.0//EN">
<mappernamespace="examples.mapper.UsersMapper">
<!--ResultMap-->
<resultMaptype="examples.bean.Users"id="BaseResultMap">
<resultproperty="id"column="id"/>
<resultproperty="name"column="name"/>
<resultproperty="sex"column="sex"/>
<resultproperty="birs"column="birs"jdbcType="TIMESTAMP"/>
<resultproperty="message"column="message"jdbcType="CLOB"
javaType="java.lang.String"typeHandler="examples.service.OracleClobTypeHandler"/>
</resultMap>
<sqlid="Tabel_Name">
t_users
</sql>
<!--表中所有列-->
<sqlid="Base_Column_List">
id,name,sex,birs,message
</sql>
<!--查询条件-->
<sqlid="Example_Where_Clause">
where1=1
<trimsuffixOverrides=",">
<iftest="id!=null">
andid=#{id}
</if>
<iftest="name!=nullandname!=''">
andnamelikeconcat(concat('%','${name}'),'%')
</if>
<iftest="sex!=nullandsex!=''">
andsexlikeconcat(concat('%','${sex}'),'%')
</if>
<iftest="birs!=null">
andbirs=#{birs}
</if>
<iftest="message!=null">
andmessage=#{message}
</if>
</trim>
</sql>
<!--2.查询列表-->
<selectid="queryByList"resultMap="BaseResultMap"parameterType="Object">
select
<includerefid="Base_Column_List"/>
fromt_users
<includerefid="Example_Where_Clause"/>
</select>
</mapper>
Mapper类接口
packageexamples.mapper;
importjava.util.List;
publicinterfaceUsersMapper<T>{
publicList<T>queryBySelective(Tt);
publicList<T>queryByList(Tt);
}
类型转换工具类
packageexamples.service;
importjava.sql.CallableStatement;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importoracle.sql.CLOB;
importorg.apache.ibatis.type.JdbcType;
importorg.apache.ibatis.type.TypeHandler;
<Object>{
publicObjectvalueOf(Stringparam){
returnnull;
}
@Override
publicObjectgetResult(ResultSetarg0,Stringarg1)throwsSQLException{
CLOBclob=(CLOB)arg0.getClob(arg1);
return(clob==null||clob.length()==0)?null:clob.getSubString((long)1,(int)clob.length());
}
@Override
publicObjectgetResult(ResultSetarg0,intarg1)throwsSQLException{
returnnull;
}
@Override
publicObjectgetResult(CallableStatementarg0,intarg1)throwsSQLException{
returnnull;
}
@Override
publicvoidsetParameter(PreparedStatementarg0,intarg1,Objectarg2,JdbcTypearg3)throwsSQLException{
CLOBclob=CLOB.empty_lob();
clob.setString(1,(String)arg2);
arg0.setClob(arg1,clob);
}
}
Spring配置文件
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="
xmlns:xsi="
xmlns:mvc="
xmlns:tx="
xsi:schemaLocation="
default-autowire="byType">
<!--配置数据源-->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<propertyname="url"><value>jdbc:oracle:thin:@127.0.0.1:1521:pms</value></property>
<propertyname="username"><value>pms</value></property>
<propertyname="password"><value>pms</value></property>
</bean>
<!--配完数据源和拥有的sql映射文件sqlSessionFactory也可以访问数据库和拥有sql操作能力了-->
<!--
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:mybatis-config.xml"/>
</bean>
-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="mapperLocations">
<list>
<value>classpath:examples/mybatis/oracle/UsersMapper.xml</value>
</list>
</property>
</bean>
<!--通过设置mapperInterface属性,使接口服务bean和对应xml文件管理可以使用其中的sql-->
<beanid=""class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--此处等同于Mybatis中ServerDaoserverDao=sqlSession.getMapper(ServerDao.class);指明映射关系-->
<propertyname="mapperInterface"value="examples.mapper.UsersMapper"/>
<propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>
</bean>
</beans>
测试类
packageexamples.service;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.List;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.;
importexamples.bean.Users;
importexamples.mapper.UsersMapper;
publicclassTestUsersService{
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)throwsParseException{
ApplicationContextac=
new("classpath:/examples/service/spring.xml");
UsersMapper<Users>=(UsersMapper<Users>)ac.getBean("");
//查询
UsersnullBean=newUsers();
List<Users>list=.queryByList(nullBean);
if(list!=null){
for(Usersuser:list){
System.out.println(user);
}
}
}
}
『伍』 mybatis 都能返回什么类型啊
Batis的返回值参数类型也有种:resultMap与resultClass
这两种类型的选择可以用两句话说明之:
一:当结果集列名和类的属性名完全相对应的时候,则可直接用resultClass直接指定查询结果类型。
二:当查询结果集与属性名对应不上的时候,就可以采用resultMap指定列名与对象属性名之间的对应关系,否则对应不上的属性将为null或0。
『陆』 Mybatis里java数据类型与数据库数据类型之间转换是不是比较重要
重要,但只要匹配、符合需求就行
『柒』 springboot + mybatis java 怎么处理大数据分批查询
springboot + mybatis java 怎么处理大数据分批查询
这里用到spring-boot-starter基础和spring-boot-starter-test用来做单元测试验证数据访问 引入连接mysql的必要依赖版mysql-connector-java 引入整合权MyBatis的核心依赖mybatis-spring-boot-starter 这里不引入spring-boot-starter-jdbc依赖
『捌』 MyBatis 读取 Mysql Blob类型的SQL怎么写
MySQL中的blob,mediumblob ,longblob 可以映射到mybatis中 的byte[] 类型 ,需要mybatis的org.apache.ibatis.type.BlobTypeHandler 类型转换处理器的支持。
<resultMap type="java.util.Map" id="imgResultMap" >
<result property="imgBytes" column="imgBytes" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler"/>
</resultMap>
<select id="findBookImg" parameterType="string" resultMap="imgResultMap" >
SELECT a.`imgBytes` FROM `t_book` a WHERE a.`id`=#{_parameter}
</select>
『玖』 mybatis bigdecimal 对应什么类型
SQL数据类型和Java数据类型的对应关系
integer、int ---> int
tinyint、smallint ---> short
bigint ---> long
decimal、numeric ---> java.math.BigDecimal
float ---> float
double ---> double
char、varchar ---> String
boolean、bit ---> boolean
date ---> java.sql.Date
time ---> java.sql.Time
timestamp ---> java.sql.Timestamp
blob ---> java.sql.Blob
clob ---> java.sql.Clob
array ---> java.sql.Array
『拾』 请问:mybatis查询某表的某个字段的最大值并返回,该如何写mapper的配置文件
<select id="findMax" parameterType="string">
select max(${fieldName}) from ...
</select>