我开始学习Mybatis并搜索了如何处理存储函数。我想知道如何使用 mybatis 调用存储函数。我可以使用这里描述的存储过程http://loianegroner.com/2011/03/ibatis-mybatis-working-with-stored-procedures/
提前致谢。
我使用mybatis从DB检索数据,返回的数据包含重复的条目.
必需结果:列名称,值
预期的结果是:column1值A但是返回的结果是:COLUMN1值A,column1值A.
希望能够澄清我的怀疑.
谁能告诉我为什么会这样?
<select id="getContentMap" resultType="map" parameterType="map">
select planId,location_qualifier from disclaimer_disclosure_content where
<choose>
<when test="plan_id != null">
plan_id = #{plan_id}
</when>
<when test="product_id != null">
product_id = #{product_id}
</when>
<otherwise>
issuer_id = #{issuer_id}
</otherwise>
</choose>
and effective_date >= #{effective_date}
and location_qualifier LIKE CONCAT('%' , #{location_qualifier} , '%')
</select>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的工作项目中实施 MyBatis。它是一个遗留系统,它使用 vanilla JDBC 来访问数据库,仅通过存储过程。我知道要调用存储过程,MyBatis 需要一个包含存储过程输入参数的对象和另一个保存结果集的对象。不确定这是否完全正确。
为了防止在系统中创建过多的数据实体,我想重用现有的。这就是问题出现的地方。让我解释一下我面临的典型情况/场景,然后我将如何解决它。
假设我在系统中有以下数据实体:
class Account {
private int accountID;
private String accountName;
private OrganizationAddress address;
// Getters-Setters Go Here
}
class OrganizationAddress extends Address {
// ... some attributes here
// Getters-Setters Go Here
}
class Address {
private String address;
private String city;
private String state;
private String country;
// Getters-Setters Go Here
}
Run Code Online (Sandbox Code Playgroud)
我正在使用注释,所以我的Mapper类有这样的东西:
@Select(value = "{call Get_AccountList(#{accountType, mode=IN, jdbcType=String})}")
@Options(statementType = StatementType.CALLABLE)
@Results(value = {
@org.apache.ibatis.annotations.Result
(property = "accountID", column …Run Code Online (Sandbox Code Playgroud) 我正在使用spring mvc + mybatis + mysql的web应用程序下工作.
我发现我无法获得最后插入记录的自动生成密钥(我已经用Google搜索了这么多).
这是相关的配置(例如,将模型'Post'):
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
//omitted
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="config.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
Run Code Online (Sandbox Code Playgroud)
<configuration>
<typeAliases>
<typeAlias alias="Post" type="com.king.model.Post" />
</typeAliases>
<mappers>
<mapper resource="com/king/model/PostMapper.xml" />
</mappers>
</configuration>
Run Code Online (Sandbox Code Playgroud)
<mapper namespace="com.king.model.PostMapper">
<insert id="insert" parameterType="Post">
insert into posts (title,body,created_at,updated_at) values (#{title},#{body},#{createDate},#{updateDate})
</insert>
</mapper>
Run Code Online (Sandbox Code Playgroud)
public abstract class AbstractSimpleDaoImpl<T> extends SqlSessionDaoSupport{
@Override
public int add(T entity) {
return getSqlSession().insert(getMapperNamespace() + …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用MyBat和MySql.
我有:
myField ENUM('是','不')
我想映射到java布尔值:
我知道我可以修改所有mybatis模板,例如:
<update id="update">
UPDATE
myTable
<set>
...
<if test="myField != null">myField = <choose>
<when test="myField == true">'yes'</when>
<otherwise>'no'</otherwise>
</choose>,
</if>
...
</set>
WHERE
...
</update>
Run Code Online (Sandbox Code Playgroud)
但我能以更方便的方式做到这一点吗?
我在这里看到很多关于在IN子句中使用元组的问题.我的情况与其他情况略有不同.IN子句中元组的一般用法如下所示
Select * from MY_TABLE
where (id,name,date) IN ((1,'new','10-JUL-13'),(2, 'old','09-JUN-13'))
Run Code Online (Sandbox Code Playgroud)
考虑到上述查询,我的要求是检索具有id和name值的记录以及特定范围内的日期.我们说吧
effectiveDate <= date <= termDate
Run Code Online (Sandbox Code Playgroud)
我正在使用ORACLE数据库和MyBatis ORM.我将数据作为对象列表获取,因此当我使用mybatis时,我可以使用foreach/for循环来填充元组,但是当我想使用条件来处理对象中的一个值时.
当我使用Mybatis从列表中读取一个值时,where子句如下所示
<where>
and (id,name) IN
<foreach item="object" collection="data" open="(" separator=","close=")">
(#{object.id},#{object.name})
</foreach>
</where>
Run Code Online (Sandbox Code Playgroud)
我也必须在循环中包含条件.
等待专家的建议.提前致谢.
试图找出是否有办法在一个查询中重用相同的片段.
考虑一下:
<sql id="personFields">
per.id person_id,
per.created_at person_created_at,
per.email_address person_email_address,
per.first_name person_first_name,
per.last_name person_last_name,
per.middle_name person_middle_name
</sql>
Run Code Online (Sandbox Code Playgroud)
"per." 别名用于在使用多个连接表的查询中使用时避免列名冲突.它包括这样:
SELECT
<include refid="com.acme.data.mapper.PersonMapper.personFields"/>
FROM Person per
Run Code Online (Sandbox Code Playgroud)
问题是每个查询不能多次使用它,因为我们有"per".别名.
有这样的事情会很棒:
<sql id="personFields">
#{alias}.id #{alias}_person_id,
#{alias}.created_at #{alias}_person_created_at,
#{alias}.email_address #{alias}_person_email_address,
#{alias}.first_name #{alias}_person_first_name,
#{alias}.last_name #{alias}_person_last_name,
#{alias}.middle_name #{alias}_person_middle_name
</sql>
Run Code Online (Sandbox Code Playgroud)
并包括这样:
SELECT
<include refid="com.acme.data.mapper.PersonMapper.personFields" alias="per1"/>,
<include refid="com.acme.data.mapper.PersonMapper.personFields" alias="per2"/>
FROM Person per1
JOIN Person per2 ON per2.parent_id = per1.id
Run Code Online (Sandbox Code Playgroud) 在映射器界面中,我有:
ArrayList<Item> select(@Param("filterId")int filterId, @Param("filterData")HashMap<String,Object> filterData);
Run Code Online (Sandbox Code Playgroud)
在映射器 xml 中,我有:
<select id="select" parameterType="map" resultMap="RM">
SELECT ...
FROM ....
WHERE id=#{filterData["id"]}
</select>
Run Code Online (Sandbox Code Playgroud)
没有错误,但结果不符合预期(它返回空集,但我知道存在具有此类 ID 的项目)。#{filterData["id"]} 似乎不起作用。我的错误在哪里?
我有一个我的 batis (3.2.7) 应用程序,我正在使用 java 代码(不是 xml )创建配置,如下所示。
public static Configuration getConfiguration(DataSet data) {
if (configuration == null) {
DataSource dataSource = getDataSource(DRIVER, URL, data.getUsername(), data.getPassword());
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment =
new Environment("development", transactionFactory, dataSource);
configuration = new Configuration(environment);
}
return configuration;
}
Run Code Online (Sandbox Code Playgroud)
使用上述配置创建 sql 会话工厂。
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
Run Code Online (Sandbox Code Playgroud)
我的映射器是 xml 格式的(我需要 xml 格式的这些),目前与映射器接口在同一个包中。我正在使用以下代码添加映射器。
configuration.addMapper(CrudMapper.class);
Run Code Online (Sandbox Code Playgroud)
这将自动添加与映射器接口 (CrudMapper) 位于同一文件夹中的 xml 映射器。但我需要将这些 xml 文件移动到资源文件夹。所以映射器接口将在一个位置,而 xml 映射器在不同的位置。我找不到任何方法将 xml 映射器添加到配置中。有没有办法做到这一点 ?
我正在尝试执行此插入:
@Insert("" +
"insert into EXTRANET.EX_ENTIDAD (ENT_REFNUM, ENT_TIPO, REG_PUB_ID, OFIC_REG_ID, AREA_REG_ID, " +
"COD_LIBRO, NUM_PARTIDA, ANO_TITU, NUM_TITU, ENT_TRIGGER, TMSTMP_CREA, PRIORIDAD) " +
" values (EXTRANET.EX_ENTIDAD_SEQ.nextval, #{entTipo}, " +
"#{regPubId}, #{oficRegId}, #{areaRegId}, #{codLibro}, #{numPartida}, #{anoTitu}, " +
" #{numTitu}, 'SYNC', SYSDATE, #{prioridad})")
public int insertEntidades(Map map);
Run Code Online (Sandbox Code Playgroud)
但是如果某些 #{param} 是 NULL 我得到它错误:
引起:org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType NULL 。尝试为此参数设置不同的 JdbcType 或不同的 jdbcTypeForNull
我正在搜索一些解决方案并阅读我必须配置一个属性:jdbcTypeForNull
然后我在 Spring 中更新我的配置:
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception,Throwable {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); …Run Code Online (Sandbox Code Playgroud)