springboot2.0整合mybatis

Posted 小狗吃月亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot2.0整合mybatis相关的知识,希望对你有一定的参考价值。

pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>

<dependencies>
<!-- 使用外部tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>5.5.23</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>

 

application.properties

#viewResovler
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp

#dataSource
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=scott
spring.datasource.password=tiger

#springMVC applicationContext-dao.xml SqlSessionFactory mapperLocations
mybatis.mapper-locations=classpath:mapper/*.xml

 

dao,service,entity略,同springMVC一样

 

启动

package com.springboot;

import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
//相当于mybatis scan
@MapperScan("com.springboot.dao")
public class App extends SpringBootServletInitializer implements CommandLineRunner {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// TODO Auto-generated method stub
return super.configure(builder);
}

@Autowired
private DataSource dataSource;

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}

@Override
public void run(String... args) throws Exception {
System.out.println("DATASOURCE:" + dataSource);

}
}















































































以上是关于springboot2.0整合mybatis的主要内容,如果未能解决你的问题,请参考以下文章

mybatisSpring整合(eclipse)以及事务管理

SpringBoot2.0应用:SpringBoot2.0整合RabbitMQ

SpringBoot2.0源码分析:整合ActiveMQ分析

SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+PageHelper

SpringBoot2.0整合fastjson的正确姿势

springboot2.0整合jpa