1.配置项目
然后完成
2.项目目录结构:
3.SpringBoot与Mybatis整合:
贴上需要的Maven的pom.xml文件:
4.0.0
com.zzf
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
com.mchange
c3p0
0.9.5.2
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-lang3
3.4
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.datatype
jackson-datatype-joda
com.fasterxml.jackson.module
jackson-module-parameter-names
org.springframework.boot
spring-boot-maven-plugin
Mybatis的全局配置文件:
application.yml配置文件
# 设置数据库相关属性
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/要连接的数据库
spring.datasource.username: 数据库用户名
spring.datasource.password: 数据库密码
# 设置MyBatis 的全局配置文件,mapper 接口路径,mapper 接口对应的xml 配置文件
mybatis.config.path: mybatis-config.xml
mapper.xml.config.path: /mapperConfig/**.xml
mapper.package.path: com.zzf.mapper
下面贴上JavaConfig文件:
首先贴上我的XML文件来讲讲为什么这么配置
首先配置dataSource数据库连接池
package com.zzf.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
@MapperScan("com.zzf.mapper")
@SpringBootConfiguration
public class DataSourceConfiguration {
@Value("${spring.datasource.driver-class-name}")
private String jdbcDriver;
@Value("${spring.datasource.url}")
private String jdbcUrl;
@Value("${spring.datasource.username}")
private String jdbcUser;
@Value("${spring.datasource.password}")
private String jdbcPassword;
@Bean
public DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUser);
dataSource.setPassword(jdbcPassword);
// 关闭连接后不自动提交
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
}
然后配置sqlSessionFactory
package com.zzf.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@SpringBootConfiguration
public class SessionFactoryConfiguration {
@Value("${mybatis.config.path}")
private String myBatisConfigPath;
@Value("${mapper.xml.config.path}")
private String mapperXMLConfigPath;
@Value("${mapper.package.path}")
private String mapperPackagePath;
@Autowired
private DataSource dataSource;
@Bean
public SqlSessionFactoryBean createSqlSessionFactory() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageXMLConfigPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperXMLConfigPath;
// 设置MyBatis 配置文件的路径
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(myBatisConfigPath));
// 设置mapper 对应的XML 文件的路径
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageXMLConfigPath));
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置mapper 接口所在的包
sqlSessionFactoryBean.setTypeAliasesPackage(mapperPackagePath);
return sqlSessionFactoryBean;
}
}
事实上到这一步SpirngBoot与Mybatis的整合已经好了……(感觉好方便)
因为有事物所以我加了个事物的配置:
package com.zzf.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import javax.sql.DataSource;
/**
* @EnableTransactionManagement 注解用于开启事务
**/
@SpringBootConfiguration
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {
@Autowired
private DataSource dataSource;
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
然后就是填写mapper(dao)层,service层,controller层,数据库映射
先看Mapper层:
package com.zzf.mapper;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public interface DemoMapper {
List
它的映射文件:
service层:
package com.zzf.service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Transactional
public interface DemoService {
List
它的实现类:
package com.zzf.service.impl;
import com.zzf.mapper.DemoMapper;
import com.zzf.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class AreaServiceImpl implements DemoService {
@Autowired
private DemoMapper demoMapper;
@Override
public List
然后看下controller层:
package com.zzf.controller;
import com.zzf.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/zzf")
public class AreaController {
@Autowired
private DemoService demoService;
@GetMapping("/getall")
public Map getall(){
List
最后设置启动类:
package com.zzf.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.zzf")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Ok~SpringBoot与Mybatis的整合(JavaConfig实现)