标签: mybatis

MyBatis、Select Provider 和 SQLBuilder

这不仅仅是一个简单的问题,而且我的英语没有我想要的那么好......我会尽力而为。

\n\n

我使用 java 8,在 Postgres 9.6 上使用 Mybatis 3.4.6,我需要执行自定义动态查询。

\n\n

在我的 mapper.java 类中,我创建了一个与myBatis SQL Builder 类一起使用的方法

\n\n
@SelectProvider(type = PreIngestManager.class, method = "selectPreIngestsSQLBuilder")\n@Results({ @Result(property = "id", column = "id"), @Result(property = "inputPath", column = "input_path"),\n        @Result(property = "idCategoriaDocumentale", column = "id_categoria_documentale"), @Result(property = "idCliente", column = "id_cliente"),\n        @Result(property = "outputSipPath", column = "output_sip_path"), @Result(property = "esito", column = "esito"),\n        @Result(property = "stato", column = "stato"), @Result(property = "pathRdp", column = "path_rdp"),\n        @Result(property = "dataInizio", column = "data_inizio"), @Result(property …
Run Code Online (Sandbox Code Playgroud)

java postgresql mybatis mybatis-generator

3
推荐指数
1
解决办法
9601
查看次数

通过MyBatis中的参数设置FROM子句

我没有在文档中看到任何与我的问题有关的内容,在部署它时,我的应用程序运行不正常(更多内容在一秒内).我想做点什么

<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
   select id, name, active, valid
   from #{table}
</select>
Run Code Online (Sandbox Code Playgroud)

在MyBatis.我有许多具有共享列的查找表,因此视图级别的用户确定最终使用哪个查找表.我尝试执行getLookupRows时得到的错误是

Cause: org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter table of statement info.pureshasta.mapper.LookupMapper.getLookupRows
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:77)
org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:69)
org.apache.ibatis.binding.MapperMethod.executeForList(MapperMethod.java:85)
org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:65)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38)
$Proxy15.getLookupRows(Unknown Source)
info.pureshasta.service.FieldTitleService.getLookupRows(FieldTitleService.java:33)
Run Code Online (Sandbox Code Playgroud)

我的mapper界面如下:

List<Lookup> getLookupRows(@Param("specificColumn") String specificColumn, 
                           @Param("table") String table);
Run Code Online (Sandbox Code Playgroud)

所以我们知道我正在尝试将String传递给此查询,没什么特别的.我有专门的专栏,因为这将是我的下一个任务.实际上每个查找表的其中一列是唯一的,因此我必须调用相应的specificColumn,但如果我能使table参数和FROM子句工作,我会非常高兴.

ibatis dynamic-queries mybatis

2
推荐指数
1
解决办法
8898
查看次数

如何在使用MyBatis时自动更新创建/修改日期等字段?

我正在使用MyBatis,并希望在每个"创建","修改"的表上实现2个字段.它们都是日期字段.有没有办法在插入或更新时自动更新这些字段?幕后我可以调整映射,但我想知道是否有更通用和干的方式这样做?

aop spring dry mybatis

2
推荐指数
1
解决办法
6980
查看次数

Mybatis resultmap将值映射到模型的哈希映射字段

我正在尝试映射创建将填充vehicleVO的结果映射.我想将几列映射到vehicleDocuments HashMap.我正在填充的数据也出现在同一个表中.

public class VehicleVO implements Serializable {
    public String vehicleId;
    public String vehicleNumber;
    public String model;
    public Map<String, Date> vehicleDocuments;
    public TransportVO transport;
    public String distanceTraveled;

}
Run Code Online (Sandbox Code Playgroud)

我试图使用以下xml进行映射.但它似乎没有用.我得到了这个错误

"元素类型"resultMap"的内容必须匹配"(构造函数?,id*,结果*,关联*,集合*,鉴别器?)".

<resultMap id="BaseResultMap" type="com.svms.service.vo.VehicleVO">
        <id column="vehicle_id" jdbcType="BIGINT" property="vehicleId" />
        <result column="vehicle_no" jdbcType="VARCHAR" property="vehicleNumber" />
        <result column="Model" jdbcType="VARCHAR" property="model" />
        <association property="vehicleDocuments" javaType="java.util.HashMap">
            <result column="FC" jdbcType="DATE" property="FC_TD" />
            <result column="TAX" jdbcType="DATE" property="TAX_TD" />
            <result column="Insureance" jdbcType="DATE" property="INSURANCE_TD" />
            <result column="Form47" jdbcType="DATE" property="FORM47_TD" />
            <result column="NC" jdbcType="DATE" property="NC_TD" />
        </association>
        <result column="total_distance" jdbcType="INTEGER" …
Run Code Online (Sandbox Code Playgroud)

java mybatis

2
推荐指数
1
解决办法
1万
查看次数

Mybatis和无效类型

我使用mybatis 3.2.2,PostgreSQL并获得"使用无效类型或值实例化类xxx时出错".调用getPluginId()时会触发异常.这是我的配置:

mapper.xml:

<resultMap type="my.class.plugins.Plugin" id="pluginMap">
    <constructor>
        <idArg column="id" javaType="_int"/>
        <arg column="dtype" javaType="String"/>
    </constructor>

</resultMap>
Run Code Online (Sandbox Code Playgroud)

这是一个有问题的方法:

<select id="getPluginById" resultMap="pluginMap">
SELECT *
FROM public.plugin
WHERE id = #{id, jdbcType=NUMERIC}
</select>   
Run Code Online (Sandbox Code Playgroud)

默认构造函数:

public abstract class Plugin {
...
public Plugin(int id, String name) {
    this.id = id;
    this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)

一个例外:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class my.class.plugins.Plugin with invalid types (int,String,) or values (7418,FORWARD,). Cause: java.lang.InstantiationException
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)
at $Proxy15.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159) …
Run Code Online (Sandbox Code Playgroud)

java postgresql persistence mybatis

2
推荐指数
2
解决办法
1万
查看次数

如何在Play Framework 2.2中自定义unmanagedResourceDirectories和excludeFilter?

我正在尝试根据指南将MyBatis与Play Framework 2.2集成.本指南是为Play v2.1.x编写的,project/Build.scala而不是使用build.sbt.

在这种情况下,如何将mapper xml文件添加到classpath?

摘自以下文章中使用的配置:

val main = play.Project(appName, appVersion, appDependencies).settings(
  // Add app folder as resource directory so that mapper xml files are in the classpath
  unmanagedResourceDirectories in Compile <+= baseDirectory( _ / "app" ),
  // but filter out java and html files that would then also be copied to the classpath
  excludeFilter in Compile in unmanagedResources := "*.java" || "*.html"
)
Run Code Online (Sandbox Code Playgroud)

java sbt mybatis playframework playframework-2.2

2
推荐指数
1
解决办法
2467
查看次数

mybatis映射器文件转义字符

在sql select语句的mybatis映射器文件中,我无法在表达式中使用特殊字符(<=).例如(简化选择):

<select id="selectMonday" resultType="SheetGameRec">
    select ColumnName
    from Table
    where ColumnName <= 2
    order by ColumnName;
</select>
Run Code Online (Sandbox Code Playgroud)

生成以下错误

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in Mapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper 

Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: xx; columnNumber: xx; The content of elements must consist of well-formed character data or markup.
Run Code Online (Sandbox Code Playgroud)

如果我替换<= with> =或=,映射器文件将工作,虽然这不是我想要的选择.

如何逃脱这些特殊角色.我也遇到了像其他表达式一样的问题.我正在使用mybatis 3.0.2.

谢谢.

mybatis

2
推荐指数
1
解决办法
1万
查看次数

将java.lang.String类型映射为Postgres JSON类型

我的问题与以下问题相关:将postgreSQL JSON列映射到Hibernate值类型,尽管当我测试在postgres中将字符串传递给psql时答案有效,但从Java代码传递字符串时却无法解决问题。

我正在使用MyBatis通过Spring注入将sql映射到postgres数据库。

这是MyBatis的Java接口,映射到Postgres JSON列时遇到问题的方法是updateState()方法。

package receiver.spoke;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import receiver.bean.Spoke;

public interface SpokeDAOMyBatis extends SpokeDAO {

    void updateState(@Param("id") long spokeId, @Param("state") String state);

    String getState(@Param("id") long spokeId);

    List<Spoke> getSpokes();

    // The close() method must exist.
    void close();

}
Run Code Online (Sandbox Code Playgroud)

以下是我的映射器类:

<?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="receiver.spoke.SpokeDAOMyBatis">

    <update id="updateState">
        UPDATE 
            spokes 
        SET 
            state = #{state} 
        WHERE 
            id = #{id}
    </update>

    <select id="getState" resultType="java.lang.String" 
            parameterType="long" useCache="false">
        SELECT 
            state 
        FROM 
            spokes 
        WHERE 
            id …
Run Code Online (Sandbox Code Playgroud)

java postgresql spring json mybatis

2
推荐指数
2
解决办法
7913
查看次数

MyBatis在批量插入中使用GeneratedKeys作为嵌套对象

批量插入和获取生成的密钥时,获取错误.批量插入工作正常.

对象结构:

对象1:

Long id, String name, Obj2 obj 
Run Code Online (Sandbox Code Playgroud)

对象2:(Obj2)

Long id, String value
Run Code Online (Sandbox Code Playgroud)

两个对象都存储在不同的表中.

object1

id | name  | object2_id (Foreign Key)
Run Code Online (Sandbox Code Playgroud)

object2

id | value
Run Code Online (Sandbox Code Playgroud)

现在我有一个要插入的对象1的列表.

该过程将插入Object 2获取id,并插入Object 1Object2的 " id " (作为外键).

在插入Object2时,插入块在Mapper.xml中

情况1:

<insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="obj1s.obj2.id">
<!-- obj1s is name of the list --> 
    insert into object2 (value) values 
        <foreach collection="obj1s" item="obj1" separator=",">
            (#{obj1.obj2.id})
        </foreach>
</insert>
Run Code Online (Sandbox Code Playgroud)

错误:获取生成密钥或将结果设置为参数对象时出错.

案例2:

<insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="obj1.obj2.id"> …
Run Code Online (Sandbox Code Playgroud)

mysql mybatis spring-mybatis

2
推荐指数
1
解决办法
3553
查看次数

使用MyBatis映射输入和输出参数

我正在学习如何使用MyBatis。老实说,我非常喜欢这个框架。它易于使用,并且对它感到满意,因为我可以使用它的sql命令:)我使用MyBatis 3.4.2和PostgreSQL数据库。

例如,我喜欢在插入@SelectKey注释之前执行查询的难易程度。和数据映射就像一个魅力,如果我的接口方法之前添加一些注释,像这样:@Results({ @Result(property = "javaField", column = "database_field", javaType = TypeHandler.class)

以下是我不喜欢的(希望您能将我带往正确的方向):


(问题1)我有一些查询,这些查询使我可以使用null和正常值,而无需任何其他“ if” java语句来检查变量是否包含null值。他们看起来像这样:

SELECT * FROM table
WHERE key_name = ? AND ((? IS NULL AND user_id IS NULL) OR User_id = ?) 
Run Code Online (Sandbox Code Playgroud)

使用JDBC,我需要执行以下操作:

stmt = connection.prepareStatement(query);
stmt.setString(1, "key");
stmt.setString(2, userId);
stmt.setString(3, userId);
Run Code Online (Sandbox Code Playgroud)

如您所见,我需要传递两次userId,因为这是JDBC的工作方式。老实说,我期望下面的代码可以在MyBatis上使用,但是不幸的是它不起作用。仍然需要定义第三个参数。

我想知道是否可以将此功能添加到MyBatis。如果MyBatis可以自动绑定userId两次,那应该没问题,如下所示:

@Select("SELECT * FROM table key_name = #{key} and ((#{userId} is null and user_id is null) OR user_id = #{userId})
SomeClass findByKeyAndUserId(String …
Run Code Online (Sandbox Code Playgroud)

java postgresql mybatis mybatis-generator spring-mybatis

2
推荐指数
1
解决办法
1万
查看次数