我最近开始学习使用myBatis.我现在面临这样的情况,我需要不断通过WebService获取一个新的Objects列表,然后对于这个列表,我需要通过oracle DB表插入/更新每个对象MyBatis的.
棘手的部分是,我不能简单地每次都进行批量插入,因为某些对象可能已经存在于DB中,对于这些记录,我需要更新它们的字段而不是新的插入.
我当前的解决方案可能非常愚蠢,使用Java,从webservice构建Object列表,循环遍历每个,执行myBatis选择,如果它不是null(已经存在于db中),则执行myBatis更新; 否则,为这个新对象执行myBatis插入.
功能实现.但我的技术负责人表示它效率非常低,因为使用Java进行for循环并逐个插入/更新将消耗大量系统资源.他建议我通过传递一个对象列表来使用myBatis进行批量插入.
然而,myBatis中的批量插入很简单,因为我不是纯插入(对于我需要更新的现有记录),我不认为批量插入在这里是合适的.我已经google了一段时间,并意识到我可能需要使用"merge"而不是"insert"(对于Oracle).
我在myBatis中搜索合并的示例仅适用于一个对象,而不是批处理.因此,我想知道专家是否可以提供一些关于如何在MyBatis中进行批量合并的例子(编写Mapper的正确方法)?
我对使用感到困惑$ vs #.我没有找到任何指南.我把他们当作
name = #{name},name like '%${word}%',order by name ${orderAs},where name = #{word}
有时,这些都是做工精细,但在有些时候,参数不包括或给我的错误,如
org.apache.ibatis.reflection.ReflectionException:名为'name'的属性没有getter .......
那么,我想知道何时使用$或#?
这是我使用spring 3.2.4,mybatis-spring-1.2.1的第一个mybatis spring mvc应用程序
当我尝试调用我的web服务时,我收到错误::
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is org.apache.ibatis.binding.BindingException: Invalid bound
statement (not found):
org.mydomain.formulary.drugmaster.dao.DrugMasterDao.getDrugsWithAlert
Run Code Online (Sandbox Code Playgroud)
我一定错过了一些明显的东西.谢谢你的帮助
以下是我的相关文件: applicationContext.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="formularyDb" />
<property name="configLocation" value="file:/web/sites/drugformulary-spring/config/mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mydomain.formulary.mappers" />
</bean>
<bean id="DrugMasterDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mydomain.formulary.drugmaster.dao.DrugMasterDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
Run Code Online (Sandbox Code Playgroud)
映射文件 - > /classes/org/mydomain/formulary/mappers/drugmasterDao.xml
<mapper namespace="org.mydomain.formulary.drugmaster.dao.DrugMasterDao">
<select id="getDrugsWithAlert" parameterType="int" resultType="org.mydomain.formulary.drug_master.model.DrugMasters">
Select drug_id,drug_name,drug_alert_date,drug_alert_source, rownum
from (select drug_id,drug_name,to_char(drug_alert_datetime,'MM/DD/YYYY') as drug_alert_date ,drug_alert_source, …Run Code Online (Sandbox Code Playgroud) 我试图避免使用额外的xml来定义mybatis3中的映射器.注释适合于.
我对@ SelectProvider/@ InsertProvider /等的使用感到有点困惑.不要以为网上有很多资源可以指导我完成这项工作.
基本上,我想在mybatis3中找到替代的注释版本.
例如,我有一个xml映射器,我想将其转换为使用注释
<select ...>
<where>
<if cause.....>
</if>
<if cause......>
</if>
</where>
</select>
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供包含代码的具体答案/解决方案吗?
提前致谢!
我想知道是否有可能在1 go中执行多个sql语句.例如,我想从多个表中删除行的场景,有没有办法可以做的事情,比如...
<delete id="delete" parameterType="String">
DELETE FROM DUMMYTABLE_A where X=${value}
DELETE FROM DUMMYTABLE_B where X=${value}
</delete>
Run Code Online (Sandbox Code Playgroud) 我想重新使用来自不同*Mapper.xml文件的特定文件,这些文件都以某种方式读取相同的对象.
我有一个名为Project的数据库表,我创建了以下resultMap:
<resultMap id="ProjectMap" type="com.model.Project">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="client_prj_no" jdbcType="VARCHAR" property="clientPrjNo" />
<result column="notes" jdbcType="VARCHAR" property="notes" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
...
<resultMap>
Run Code Online (Sandbox Code Playgroud)
它在ProjectMapper.xml中工作得很好,但是,现在我想创建一个ClientWithProjectsMapper.xml,我想要SELECT*FROM CLIENT,PROJECT,其中PROJECT.CLIENT_ID = CLIENT.ID并且Client对象返回一个List对象.换句话说,我想用一个SQL获取ClientWithProjects.
在我的映射中,我想重用我在ProjectMapper.xml中定义的ProjectMap(没有复制/粘贴),但我不知道如何实现这一点.
我可以将ProjectMap分解为一个单独的文件,但我没有在MyBatis中找到#include其他文件的任何工具.
关于如何做到这一点的任何想法?(我正在使用Maven,是否有任何插件可以过滤寻找#include等文件,并将文件内容直接包含在正在处理的文件中?).
谢谢.
-AP_
我正在使用Spring和Mybatis并且我有两个数据库,第一个数据库的配置相对容易,但我无法使用Spring和事务处理第二个数据库,这是我的代码
@Configuration
@ComponentScan(basePackages = {"hernandez.service", "hernandez.dao"})
@EnableTransactionManagement
@MapperScan(basePackages="hernandez.mapper" )
@Import(DbConfig2.class)
public class AppConfig {
@Bean(name = "dataSource")
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/northwind", "root", "");
return ds;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource());
return factoryBean;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
@Configuration
@MapperScan("loli.mapper" )
public class DbConfig2 {
@Bean(name = "dataSource_2")
public DataSource dataSource2() {
DriverManagerDataSource ds = new DriverManagerDataSource("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/dmsolut_dmsms", "root", ""); …Run Code Online (Sandbox Code Playgroud) 我知道之前已经问过这个问题,但是根据我到目前为止找到的信息,我无法实现解决方案.所以也许有人可以向我解释.
我有一张桌子"状态".它有两列:id和name.id是PK.
我想使用枚举,而不是使用POJO状态.我创建了如下枚举:
public enum Status {
NEW(1), READY(2), CLOSED(3);
private int id;
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
Status(int id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的映射器
<select id="getStatusByName" resultType="Status" parameterType="String">
SELECT ls.id, ls.name
FROM status AS ls
WHERE ls.name = #{name}
</select>
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,当我尝试检索枚举时,某些东西会中断,但不会抛出任何异常.
我正在使用注释设置mybatis,并获得这个有用的例外
org.apache.ibatis.binding.BindingException:MapperRegistry不知道类型接口org.foo.Bar
谷歌搜索没有找到任何东西,也没有找到用户指南.我错过了什么?
我正在从mybatis3执行mysql查询执行.我是新来的.mybatis 3中的集合和关联映射有什么区别?
具体例子如下.
SELECT e.empid AS empid,e.empname AS empname,
e.empsalary AS empsalary,p.proname AS proname,p.proid AS proid
FROM projects p,employees e,projectassigns pa
WHERE pa.empid=e.empid AND pa.proid=p.proid;
Run Code Online (Sandbox Code Playgroud)
我需要员工和项目的所有细节.
我给出了如下结果图.
<resultMap id="resultProjects" type="com.pratap.model.ProjAssigns">
<association property="employee" javaType="com.pratap.model.Employee"
resultMap="resultEmployees" />
<association property="project" javaType="com.pratap.model.Project"
resultMap="resultProjects" />
</resultMap>
Run Code Online (Sandbox Code Playgroud)
任何人都可以用我的例子或你自己的例子解释这个区别吗?
我对此感到困惑..
谢谢.
mybatis ×10
java ×5
ibatis ×2
mysql ×2
spring ×2
sql ×2
annotations ×1
associations ×1
enums ×1
jdbc ×1
mapper ×1
mappers ×1
mapping ×1
oracle ×1
orm ×1
refactoring ×1
resultset ×1
spring-mvc ×1