标签: spring-mybatis

从mybatis xml查询生成sql查询

我有一个mybatis映射文件xml,复杂的查询很多where条款的条款.

有什么办法可以创建可能的查询组合吗?

我想在所有这些查询上运行解释,因为我计划添加NOT IN所有查询.

java sql mybatis spring-mybatis

10
推荐指数
1
解决办法
269
查看次数

Spring Service @Transactional不回滚事务Mybatis SqlSession

目标是在发生故障时回滚所有/任何事务.但这并不像预期的那样有效.

我们使用Spring MVC + JMS + Service + Mybatis.在日志中,JMS设置为回滚,但是插入行而不是回滚.想知道我错过了什么或做错了什么?

最近添加了@Transactional标记.所以不确定它是否按预期工作.

码:

服务类别:

@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DataExchangeLogic implements DataExchangeService {

private DataExchDao dataExchDao;

...

    @Override
    public void save(DataExch dataExch) throws ValidationException {
        if (dataExch.getId() != null && dataExch.getId() > 0) {
            this.dataExchDao.update(dataExch);
        } else {
            //LOGGER.debug("in insert::");
            this.dataExchDao.create(dataExch);
            //Empty exception throw to test rollback
            throw new RuntimeException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

DAO:

public interface DataExchDaoMybatis
extends NotificationDao {

void create(DataExch dataExch);

}
Run Code Online (Sandbox Code Playgroud)

春天语境

<bean …
Run Code Online (Sandbox Code Playgroud)

spring transactions mybatis spring-jms spring-mybatis

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

使用spring-mybatis进行Spring-boot - 如何强制它记录所有SQL查询

我有一个简单的spring-boot-mybatis应用程序(请记住,请).Mybatis仅在出现故障时(在例外情况下)记录SQL查询.请告诉我,如何强制它将所有SQL查询记录到控制台?

此刻我正在使用slf4j记录器(自动配置spring-boot).
我找到了这个链接:http://www.mybatis.org/mybatis-3/logging.html
但是我没有设法遵循它.首先显示配置log4j,我不确定如果我正确理解:配置是否足够application.properties

提前致谢

java spring spring-boot spring-mybatis

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

MyBatis如何为不同的数据库后端生成不同的sql

我正在使用mybatis-spring 1.2.3和Spring4来创建一个Web应用程序.主数据存储是MySQL在生产环境中,但我也在内存数据库H2中进行单元测试.

MyBatis在测试和生产中都能很好地兼容MySQL和H2,但是我遇到了一个问题,有一天我需要force index(idx1)在MySQL的查询中使用它,这将导致单元测试中的语法错误,因为H2不支持force index.结果,单元测试完全被打破.

我想知道MyBatis有什么方法可以处理这种情况吗?(数据库类型在测试和生产方面有所不同,它们对SQL语法的支持也不尽相同.)

这是我的映射文件:

<?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="myproject.mapper.UserMapper">
  <select id="getGameUsersForDate" resultType="myproject.dao.domain.GameUser">
    select
    *
    from game_user
    force index(idx1)
    where
    game_id in
    <choose>
      <when test="gameIds.size() > 0">
        <foreach item="gameId" collection="gameIds" open="(" separator="," close=")">
          #{gameId}
        </foreach>
      </when>
      <otherwise>
        (null)
      </otherwise>
    </choose>
    and uid in
    <choose>
      <when test="uids.size() > 0">
        <foreach item="uid" collection="mids" open="(" separator="," close=")">
          #{mid}
        </foreach>
      </when>
      <otherwise>
        (null)
      </otherwise>
    </choose>
    and `date` = #{date}
  </select>
</mapper>
Run Code Online (Sandbox Code Playgroud)

java h2 mybatis spring-boot spring-mybatis

7
推荐指数
1
解决办法
2062
查看次数

TransactionManagementType.CONTAINER 与 TransactionManagementType.BEAN

TransactionManagementType.CONTAINER 和 TransactionManagementType.BEAN 有什么区别

因为我在我所有的 EJB 中使用 TransactionManagementType.CONTAINER 并且当使用多个数据库实例时,它会抛出一个错误,如果我将其更改为 TransactionManagementType.BEAN

我想知道如果我将其更改为 TransactionManagementType.BEAN 有什么优点和缺点以及如何影响它

ERROR:
Error updating database.  Cause: java.sql.SQLException: javax.resource.ResourceException: 
IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.
connectionmanager.listener.TxConnectionListener@680f2be0[state=NORMAL managed 
connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@7ba33a94 connection 
handles=0 lastReturned=1495691675021 lastValidated=1495690817487 
lastCheckedOut=1495691675018 trackByTx=false pool=org.jboss.jca.core.connectionmanager.
pool.strategy.OnePool@efd42c4 mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool
@71656eec[pool=FAQuery] xaResource=LocalXAResourceImpl@4c786e85
[connectionListener=680f2be0 connectionManager=5c3b98bc warned=false 
currentXid=null productName=Oracle productVersion=Oracle Database 12c 
Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
jndiName=java:/FAQuery] txSync=null]
Run Code Online (Sandbox Code Playgroud)

transactionmanager spring-transactions ejb-3.0 spring-mybatis

7
推荐指数
1
解决办法
7566
查看次数

为什么接口和xml映射器文件必须在同一个包中并且具有相同的名称?

今天我正在准备一个使用Spring Boot并使用MyBatis进行Spring-MyBatis旁边的数据访问通信的示例.这是相关的项目配置(使用maven):

src/main/java
- edu.home.ltmj.controller
  + CategoryController.java
- edu.home.ltmj.dao
  + CategoryDao.java
- edu.home.ltmj.domain
  + Category.java
src/main/resources
- edu.home.ltmj.dao
  + CategoryMapper.xml
Run Code Online (Sandbox Code Playgroud)

文件的相关内容:

CategoryDao.java:

package edu.home.ltmj.dao;

public interface CategoryDao {
    List<Category> getAllCategories();
}
Run Code Online (Sandbox Code Playgroud)

CategoryMapper.xml:

<mapper namespace="edu.home.ltmj.dao.CategoryDao">
    <resultMap id="categoryMap"
        type="edu.home.ltmj.domain.Category">
        <id property="id" column="id" />
        <result property="name" column="name" />
    </resultMap>
    <select id="getAllCategories" resultMap="categoryMap">
        SELECT id, nombre
        FROM category
    </select>
</mapper>
Run Code Online (Sandbox Code Playgroud)

然后,我在请求控制器中注入一个这个dao的实例(用于测试目的),如下所示:

package edu.home.ltmj.controller;

@RestController
public class CategoryController {
    @Autowired
    private CategoryDao dao;

    @RequestMapping(value="/category/all",
        method=RequestMethod.GET,
        produces=MediaType.APPLICATION_JSON_VALUE)
    public List<Categoria> getAllCategories() {
        return dao.getAllCategories();
    }
}
Run Code Online (Sandbox Code Playgroud)

我运行我的项目并通过使用测试执行 …

java spring mybatis spring-boot spring-mybatis

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

使用 MyBatis 和 MySql 以编程方式创建表

我想创建一种方法来动态创建表,只需将表名作为变量传递即可。我已经定义了我的 xml 映射器

<mapper namespace="com.mappers.TableCreatorMapper">
    <cache />
    <insert id="createNewTableIfNotExists" parameterType="String" > 
        CREATE TABLE IF NOT EXISTS #{tableName} 
        (
        `ID` varchar(20) NOT NULL,
        PRIMARY KEY (`ID`)
        ) 
        ENGINE=InnoDB
    </insert>
</mapper>
Run Code Online (Sandbox Code Playgroud)

我的 Java 接口映射器很简单:

public interface TableCreatorMapper {
     public void createNewTableIfNotExists(String tableName);
}
Run Code Online (Sandbox Code Playgroud)

但是当我调用我的界面时

tableCreatorMapper.createNewTableIfNotExists("test");
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test' 
        (
        `ID` varchar(20) …
Run Code Online (Sandbox Code Playgroud)

java mysql database mybatis spring-mybatis

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

Spring with MyBatis:预期单个匹配 bean,但发现 2

我一直在将 Spring 与 MyBatis 一起使用,它在单个数据库中运行得非常好。我在尝试添加另一个数据库时遇到了困难(请参阅Github 上的可重现示例)。

我正在使用 Spring Java 配置(即不是 XML)。我见过的大多数示例都展示了如何使用 XML 实现这一点。

我有两个这样的数据配置类(A & B):

@Configuration
@MapperScan("io.woolford.database.mapper")
public class DataConfigDatabaseA {

    @Bean(name="dataSourceA")
    public DataSource dataSourceA() throws SQLException {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new com.mysql.jdbc.Driver());
        dataSource.setUrl("jdbc:mysql://" + dbHostA + "/" + dbDatabaseA);
        dataSource.setUsername(dbUserA);
        dataSource.setPassword(dbPasswordA);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSourceA());
        return sessionFactory.getObject();
    }
}
Run Code Online (Sandbox Code Playgroud)

两个映射器,以及一个自动连接映射器的服务:

@Service
public class DbService {

    @Autowired
    private DbMapperA dbMapperA;

    @Autowired
    private DbMapperB …
Run Code Online (Sandbox Code Playgroud)

java spring mybatis spring-boot spring-mybatis

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

spring-data-jdbc 是如何赞美 MyBatis 的?

我想了解为什么 spring-data-jdbc 提供与 MyBatis 的集成。

  • 它在解决什么问题?
  • spring-data-jdbc 是如何赞美 MyBatis 的。
  • 这只是一个使映射器符合存储库的问题,还是超出了这个问题。
  • 当我可以使用 Mappers 时,为什么要结合使用 MyBatis 和 spring-data-jdbc。

java mybatis spring-data spring-mybatis spring-data-jdbc

6
推荐指数
1
解决办法
1785
查看次数

Spring Data JPA 和 MyBatis

我正在尝试将 Spring Data JPA 与 MyBatis 一起使用。由于 MyBatis 没有 Vendor Adapter,这里有什么替代方案?

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.abc.xyz.domain"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

当我尝试初始化我的应用程序时,出现以下异常。

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either
Run Code Online (Sandbox Code Playgroud)

谢谢

java mybatis spring-data-jpa spring-mybatis

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