SpringBoot 配置文件以及依赖 加上跨域配置文件
阅读原文时间:2023年07月08日阅读:3

配置目录:

application.properties的配置

#设置服务端口号
server.port = 8090

#配置数据源
spring.datasource.driver-class-name = org.mariadb.jdbc.Driver
spring.datasource.url = jdbc:mariadb://localhost:3306/数据库名
spring.datasource.username = root
spring.datasource.password = 123456

#连接池
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource

配置 mybatis

mybatis.config-location=classpath:mybatis/mybatis-config.xml

设置 别名类型包 (实体类)

mybatis.type-aliases-package=com.hjb.carsales.entity

设置 mybatis 映射 的 SQL语法 xml文件

mybatis.mapper-locations=classpath:mapper/*.xml

#设置日志
logging.config=classpath:mybatis/logback-spring.xml

#JpaRepository save()加上下面一句话 才起作用
spring.jpa.hibernate.ddl-auto = update

#配置分页
pagehelper.helper-dialect= mariadb
pagehelper.reasonable= true

#如果是mariadb,需要配置如下
spring.jpa.database-platform=org.hibernate.dialect.MariaDB10Dialect

generatorConfig.xml的配置


<!--<properties resource="application.properties" />-->

<context id="main" targetRuntime="MyBatis3Simple">

    <commentGenerator>  
        <property name="suppressAllComments" value="true" />  
    </commentGenerator>

    <jdbcConnection driverClass="org.mariadb.jdbc.Driver"  
                    connectionURL="jdbc:mariadb://localhost:3306/数据库名"  
                    userId="root"  
                    password="123456">  
    </jdbcConnection>

    <javaModelGenerator targetPackage="com.hjb.carsales.entity" targetProject="src\\main\\java">  
        <property name="trimStrings" value="true" />  
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="mapper"  targetProject="src\\main\\resources">  
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="com.hjb.carsales.dao"  targetProject="src\\main\\java">  
    </javaClientGenerator>

    <table tableName="%">  
        <property name="useActualColumnNames" value="true"/>  
        <generatedKey column="ID" sqlStatement="JDBC" identity="true" />  
    </table>  
</context>  

logback-spring.xml日志配置



%msg%n

<logger name="com.hjb.carsales.dao" level="TRACE" />

<root level="error">  
    <appender-ref ref="STDOUT" />  
</root>  

mybatis-config.xml的配置









org.springframework.boot spring-boot-starter-actuator

org.springframework.boot spring-boot-starter-data-jpa

org.springframework.boot spring-boot-starter-thymeleaf

org.springframework.boot spring-boot-starter-web

org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2

org.springframework.boot spring-boot-devtools runtime

org.springframework.boot spring-boot-starter-tomcat provided

org.springframework.boot spring-boot-starter-test test

org.mariadb.jdbc mariadb-java-client 2.3.0

com.mchange c3p0 0.9.5.2

com.github.pagehelper pagehelper-spring-boot-starter 1.2.4

org.apache.shiro shiro-spring 1.4.0

</dependencies>

插件的配置



org.springframework.boot spring-boot-maven-plugin

        <plugin>  
            <groupId>org.mybatis.generator</groupId>  
            <artifactId>mybatis-generator-maven-plugin</artifactId>  
            <version>1.3.7</version>

            <configuration>  
                <configurationFile>${basedir}/src/main/resources/mybatis/generatorConfig.xml</configurationFile>  
                <overwrite>true</overwrite>  
            </configuration>

            <dependencies>  
                <dependency>  
                    <groupId>org.mariadb.jdbc</groupId>  
                    <artifactId>mariadb-java-client</artifactId>  
                    <version>2.3.0</version>  
                </dependency>  
            </dependencies>  
        </plugin>

    </plugins>  
</build>

跨域配置文件

@Configuration
public class CorsFiter implements Filter {

@Override  
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {  
    HttpServletResponse response = (HttpServletResponse) servletResponse;  
    response.setHeader("Access-Control-Allow-Origin","http://localhost:8080");  
    response.setHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");  
    response.setHeader("Access-Control-Allow-Credentials","true");  
    response.setHeader("Access-Control-Max-Age","3600");  
    response.setHeader("Access-Control-Allow-Headers","X-Requested-with,Content-Type");  
    filterChain.doFilter(servletRequest,servletResponse);  
}

@Override  
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override  
public void destroy() {

}  

}