SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建

最近项目中要用SpringBoot + MyBatis + MySQL + Maven的开发环境。经过一晚上折腾,终于搭建了开发环境。以下为环境搭建过程。


1.新建一个Spring工程

SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建_第1张图片
填写项目信息。Type选择Maven Project即可
SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建_第2张图片
选择依赖时,选择web下的web。以及SQL下的JPA,MyBatis,MySQL。
SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建_第3张图片

SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建_第4张图片
之后,一路点击next即可完成工程的创建。

2.配置pom文件

以下是pom.xml的完整配置


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.aim.demogroupId>
    <artifactId>demoartifactId>
    <version>1.0.0-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>demoname>
    <description>Demo project for Spring Boot of Aimdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.4.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
            <version>1.3.6.RELEASEversion>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.0version>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

3.Mybatis配置

新建一个 MybatisConfig类

@Configuration // @Configuration注解(该注解类似于spring的配置文件)
@MapperScan(basePackages = "com.aim.demo.dao")// @MapperScan注解,指定扫描的mapper接口所在的包
public class MybatisConfig implements EnvironmentAware {
    private static final Logger logger = LoggerFactory.getLogger(MybatisConfig.class);

    @Autowired
    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }

    /**
     * 创建数据源
     * @Primary 该注解表示在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让@autowire注解报错
     */
    @Bean
    @Primary
    public DataSource getDataSource() throws Exception{
        Properties props = new Properties();
        props.put("driverClassName", env.getProperty("spring.datasource.driver-class-name"));
        props.put("url", env.getProperty("spring.datasource.url"));
        props.put("username", env.getProperty("spring.datasource.username"));
        props.put("password", env.getProperty("spring.datasource.password"));
        return DruidDataSourceFactory.createDataSource(props);
    }

    /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception{
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(ds);//指定数据源(这个必须有,否则报错)
        //下边两句仅仅用于*.xml文件,如果整个持久层操作不需要使用到xml文件的话(只用注解就可以搞定),则不加
        fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));//指定基包
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));//指定xml文件位置
        return fb.getObject();
    }
}

4.application.yml配置

在这里配置数据库连接及mybatis默认mapper地址和基包

#默认使用配置
spring:
  profiles:
      active: dev

#公共配置与profiles选择无关
mybatis:
  typeAliasesPackage: com.aim.demo.dao.entity
  mapperLocations: classpath:mapper/*.xml

---

#开发配置
spring:
  profiles: dev

  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

到此,SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建就已经结束了。
编写mapper文件及对应接口,即可对数据库进行相关操作了。

你可能感兴趣的:(SpringBoot)