我试图将java应用程序中的文件插入到mysql表中.
对映射器的调用完成没有任何异常,但最后我在表中找不到任何记录.
也许我将文件内容映射到blob字段的方式有问题?
这是豆子:
public class CustomFile {
private int id;
private String title;
private String fileName;
private String fileType;
private String fileSize;
private byte[] fileContent;
private String fileExtension;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileType() …Run Code Online (Sandbox Code Playgroud) 为什么此代码中发生错误?我不明白..请帮助我。
我测试过它:doSave(),getListCount(),doDelete(),getDetails()都不错。
但是,在getList()方法中会发生错误!
为什么?该方法没有参数,只有一个查询。
select * from tb_board_data
Run Code Online (Sandbox Code Playgroud)
我的配置:mybatis 3.2.2 / mybatis-spring 1.2.0 / spring 3.2.2
package kr.co.goodwilldd.board.mapper;
import java.util.ArrayList;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;
@Repository
public interface CommonBoardMapper<T> {
@Select(value = "SELECT COUNT(*) FROM TB_BOARD_DATA")
public long getListCount();
@Select(value = "SELECT * FROM TB_BOARD_DATA")
public ArrayList<T> getList();
@Select(value = "SELECT * FROM TB_BOARD_DATA WHERE bseq = #{_seq}")
public T getDetails(long _seq);
@SelectKey(before=true, …Run Code Online (Sandbox Code Playgroud) 我看不到任何mapper(mybatis-3-mapper.dtd),我可以在mybatis中调用merge语句.
我看到了更新,插入,删除和SQL的标签
任何人请建议如何在Mybatis中使用oracle merge语句.
我在我的域模型中有一个一对多的关系,我基本上想要阅读Foos和过滤的集合,Bars其中包含一个 MyBatis select 语句,使用嵌套选择Bars.
解释一下:我的领域模型类看起来或多或少是这样的(真正的领域模型当然更复杂,但我的问题归结为这个):
public class Foo {
private String name;
private Set<Bar> bars;
// getters and setters omitted
}
public class Bar {
private String color;
// getters and setters omitted
}
Run Code Online (Sandbox Code Playgroud)
现在我想Foos用Bars特定颜色的特定名称阅读:
public interface FooRepository {
public List<Foo> selectFoosWithBars(String name, String color);
}
Run Code Online (Sandbox Code Playgroud)
我的 MyBatis XML Mapper 文件的相关部分如下所示:
<select id="selectFoosWithBars" resultMap="fooResult">
SELECT f.id f_id, f.name f_name FROM foos f WHERE f.name = #{name}
</select>
<select id="selectBars" resultMap="barResult"> …Run Code Online (Sandbox Code Playgroud) 对于以下查询,
SELECT
MSGS.MSGTYPE,
count(*) as NOOFRECORDS
FROM
SCHEMA.MESSAGES MSGS
GROUP BY
MSGS.MSGTYPE
Run Code Online (Sandbox Code Playgroud)
什么是MyBatis映射器中的等价物,以便我可以将结果作为HashMap获取,其中MSGS.MSGTYPE作为键,NOOFRECORDS作为值?
使用 MyBatis,我有一个 SQL 作为参数接收字符串 "fr.id_lotacao in (3007, 3008, 3009, 3010)"
查询语句:
...
<if test="idLotacao != -1">
and ${idLotacao}
</if>
...
Run Code Online (Sandbox Code Playgroud)
我以这种方式从 Java 调用:
getDB1SqlSessionTemplate().selectList("sql", MyBatisUtil.createMap("idLotacao", getIdsLotacao(lotacao)));
Run Code Online (Sandbox Code Playgroud)
其中getIdsLotacao(lotacao)返回作为参数传递的字符串。
但是,当执行时,MyBatis 抛出错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NumberFormatException: For input string: "fr.id_lotacao in (3007, 3008, 3009, 3010)"
Run Code Online (Sandbox Code Playgroud)
当用 $ 接收参数时,MyBatis 不是应该${idLotacao}用 String替换"fr.id_lotacao in (3007, 3008, 3009, 3010)"吗?
我究竟做错了什么?是什么导致了这个错误?
我指的是这篇文章,其中我们可以使用 Spring Framework 中的 AbstractRoutingDataSource 来动态更改应用程序使用的数据源。我正在使用 Mybatis (3.3.0) 和 Spring (4.1.6.RELEASE)。如果从主数据库获取数据时发生异常,我想切换到备份数据库。在这个例子中,我使用了 hsql 和 mysql db。
路由数据源:
public class RoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getTargetDataSource();
}
}
Run Code Online (Sandbox Code Playgroud)
数据源上下文持有者:
public class DataSourceContextHolder {
private static final ThreadLocal<DataSourceEnum> contextHolder = new ThreadLocal<DataSourceEnum>();
public static void setTargetDataSource(DataSourceEnum targetDataSource) {
contextHolder.set(targetDataSource);
}
public static DataSourceEnum getTargetDataSource() {
return (DataSourceEnum) contextHolder.get();
}
public static void resetDefaultDataSource() {
contextHolder.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
应用数据配置:
@Configuration
@MapperScan(basePackages = "com.sample.mapper")
@ComponentScan("com.sample.config") …Run Code Online (Sandbox Code Playgroud) <select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
Run Code Online (Sandbox Code Playgroud)
但是,如果list包含超过 1000 个项目并且您使用的是 Oracle DB,则会出现以下异常:
java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000
Run Code Online (Sandbox Code Playgroud)
我能做些什么来解决这个问题,使其适用于 1000 多个元素?
我在Oracle中调用一个存储过程,接收7个参数并返回5,我只对两个参数感兴趣.这是我的MyBatis Select
@Select(value = "{CALL prc_ultimo_nombramiento(#{tipoIdentificacion, mode=IN},#{numeroIdentificacion, mode=IN},#{idEt, jdbcType=VARCHAR},#{fechaPosesion, mode=OUT, jdbcType=VARCHAR},#{idTipoNombramiento, mode=OUT, jdbcType=VARCHAR},#{validar, jdbcType=VARCHAR},#{mensaje, jdbcType=VARCHAR})}")
@Options(statementType = StatementType.CALLABLE)
@ResultType(CPDatosDocente.class)
CPDatosDocente obtenerDatosFechaPosesionIdNombramiento(CPDatosDocente datosDocente);
Run Code Online (Sandbox Code Playgroud)
我的CPDatosDocente是一个包含我需要的所有变量的POJO.
String idTipoNombramiento;
String validar;
String mensaje;
String fechaPosesion;
String tipoIdentificacion;
String numeroIdentificacion;
String idEt;
//Getters and setters...
Run Code Online (Sandbox Code Playgroud)
我有一个dao,我在调用MyBatis Sentence,但是当我调用procedure时,我的对象(CPDatosDocente)为null
public CPDatosDocente obtenerFechaPosesionIdNombramiento(Long tipoIdentificacion,
Long numeroIdentificacionDocente) {
SqlSession session = sf.openSession();
try {
// Se abre conexión con el mapper
CPDatosDocenteMapper mapper = session.getMapper(CPDatosDocenteMapper.class);
// Se ejecuta la consulta para obtener la fecha de posesión y el
// …Run Code Online (Sandbox Code Playgroud) 有没有办法让MyBatis返回一个Optional<MyClass>实例而不仅仅是一个MyClass实例?