question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to config mybatis datasource?not a simple jdbc

See original GitHub issue

in a spring project,I usually config like this:

    <bean id="rowanTemplateSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="XXXDataSource" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="rowanTemplateSqlSessionFactory" />
    </bean>
``

but when I use mybatis-spring-boot,how to config XXXDataSource?I tried lots of times,but still failed.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gariemcommented, Jan 15, 2016

Add this to your dependencies:

compile("org.mybatis:mybatis:3.3.0")
compile("org.mybatis:mybatis-spring:1.2.3")
compile("org.springframework:spring-jdbc:4.1.7.RELEASE")

Then, put this in your application.yml:

spring:
  datasource:
    url: jdbc:oracle:thin:@db-host:port/catalog
    username: user
    password: pass
    driver-class-name: oracle.jdbc.OracleDriver

Then you can autowire the datasource inside any spring-managed bean, for instance in a configuration:

@Configuration
@MapperScan(basePackages = "com.mycompany.application.persistence.mapper", sqlSessionFactoryRef = "mySqlSessionFactory")
public class MyScannerConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyScannerConfig.class);

    @Autowired
    DataSource myDataSource;

    @Bean(name = "mySqlSessionFactory")
    public SqlSessionFactory mySqlSessionFactory() {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(myDataSource);
        sessionFactory.setTypeAliasesPackage("com.mycompany.application.domain");
        return sessionFactory.getObject();
    }
}

Then you just need to keep writing your Mapper XMLs and Interfaces in the same way you’ve been doing it.

0reactions
eddumelendezcommented, Jan 14, 2016

@CharlesMaster any news about this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MyBatis 3 | Configuration
The MyBatis configuration contains settings and properties that have a ... Some drivers require specifying the column JDBC type but others ...
Read more >
MYBATIS - Configuration XML - Tutorialspoint
This chapter discusses how to configure MyBatis using XML file. ... If we use the transaction manager of type JDBC, the application is...
Read more >
Apache Tomcat 8 (8.5.84) - JNDI Datasource How-To
Before you proceed, don't forget to copy the JDBC Driver's jar into $CATALINA_HOME/lib . 1. MySQL configuration. Ensure that you follow these ...
Read more >
Use MyBatis to use the JDBC driver for Tablestore
This topic describes how to access Tablestore by using MyBatis to use the Java Database Connectivity (JDBC) driver for Tablestore.
Read more >
MyBatis with Spring - Baeldung
In our examples, we'll use the H2 embedded database to simplify the setup and EmbeddedDatabaseBuilder class from the spring-jdbc module for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found