mybatis用到的pom:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
druid用到的pom:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
直接连接
1.配置数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#根据自己环境配置可连接的数据库信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=devgroup
spring.datasource.password=devgroup
2.编写pojo(根据需求)
package com.kexin.report.pojo;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
}
3.编写映射类使用@Mapper
package com.kexin.report.mapper.local;
import com.kexin.report.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user limit 10")
List<User> getAllUsers();
}
4.编写service类和接口,直接注入映射类
package com.kexin.report.service.imple;
import com.kexin.report.mapper.local.UserMapper;
import com.kexin.report.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author KeXin
* @Date 2018/8/7 下午3:10
**/
@Service("userService")
public class UserServiceImple implements com.kexin.report.service.UserService {
@Autowired
UserMapper userMapper;
@Override
public List<User> getAllUsers(){
return userMapper.getAllUsers();
}
}
5.在controller直接调用服务就可以
使用druid
**启动类需要使用@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
指明禁用springboot自动配置数据源**
package com.kexin.report;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ReportApplication {
public static void main(String[] args) {
SpringApplication.run(ReportApplication.class, args);
}
}
1.yml文件
spring:
datasource:
hikari:
local:
jdbc-url: jdbc:mysql://localhost:3306/report
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
impala:
jdbc-url: jdbc:impala://
username:
password:
driver-class-name:
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
2.配置类
DataSourceConfig:
package com.kexin.report.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* @Author KeXin
* @Date 2018/8/7 下午2:55
**/
@Configuration
public class DataSourceConfig {
@Bean("impalaDS")
@ConfigurationProperties(prefix = "spring.datasource.hikari.impala")
public DataSource dataSource1(){
DataSource source = DataSourceBuilder.create().build();
System.out.println("impala\t"+source.toString());
return source;
}
@Bean("localDS")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.hikari.local")
public DataSource dataSource2(){
// return DataSourceBuilder.create().build();
DataSource source = DataSourceBuilder.create().build();
System.out.println("local\t"+source.toString());
return source;
}
}
其他数据库配置类似,LocalDSConfig:
package com.kexin.report.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @Author KeXin
* @Date 2018/8/7 下午2:59
**/
//basePackages是本数据库对应的映射类所在的包,这样就不用在映射类中使用mapper注解
@Configuration
@MapperScan(basePackages = "com.kexin.report.mapper.local",sqlSessionFactoryRef = "sqlSessionFactory2")
public class LocalDSConfig {
@Autowired
@Qualifier("localDS")
private DataSource localDS;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(localDS); // 使用titan数据源, 连接titan库
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2()); // 使用上面配置的Factory
return template;
}
}