Springboot项⽬l配置⽂件以及启动类的⼀些配置l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
//当前项⽬的名称
<groupId>dhcc.basicbusiness</groupId>
<artifactId>dhcc-basicbusiness</artifactId>
<version>0.0.1-SNAPSHOT</version>
//打包⽅式:jar、war
<packaging>jar</packaging>
<name>dhcc-basicbusiness</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<properties>
//通过mybatis⾃动⽣成mapper.实体类。以及l⽂件(具体的⾃动⽣成xml⽂件在下⾯的l中)<!-- Java接⼝和实体类 -->
<targetJavaProject>${basedir}/src/main/java</targetJavaProject>
<targetMapperPackage>com.basicbusiness.mapper</targetMapperPackage>
<targetModelPackage>ity</targetModelPackage>
<!-- XML⽣成路径 -->
<targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject>
<targetXMLPackage>mybatis</targetXMLPackage>
<!-- 依赖版本 -->
<mapper.version>3.4.0</mapper.version>
<mybatis.version>3.3.1</mybatis.version>
<mybatis.spring.version>1.2.4</mybatis.spring.version>
<pagehelper.version>4.1.1</pagehelper.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
//引⼊的所有依赖包
<dependencies>
<!--spring session 与redis应⽤基本环境配置,需要开启redis后才可以使⽤,不然启动Spring boot会报错 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!--注册器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-数据库连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--⽇志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency><!-- 引⼊log4j2依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--oracle数据库依赖-->
<!--oracle驱动需要通过mavn命令⽣成到本地mavn仓库,才可以这么引⼊
命令:mvn install:install-file -acle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=ojdbc6.jar -->
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>11.2.0.4</version>
</dependency>
<!--springboot测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId&le.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>al.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
<!--对第三⽅jar的依赖-->
<dependency>
<groupId>sigar</groupId>
<artifactId>sigar</artifactId>
<version>1.1.8</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/sigar.jar</systemPath>
</dependency>
<!--对其他项⽬的依赖-->
<dependency>
<groupId>com.dhcc</groupId>
<artifactId>dhcc-comment</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!-- 将引⽤的第三⽅jar,⾮mavn依赖也打进要打的jar中 -->
<resources>
<resource>
<!-- 第三⽅jar所在的路径-->
<directory>src/main/resources/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources>
<plugins>
<!-- 此插件⽤来⽣成通⽤mapper的代码 -->
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--⾃动⽣成跟数据库相对应的实体,接⼝以及xml配置⽂件-->
<configurationFile>${basedir}/src/main/l</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId&batis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
</dependencies>
</plugin>
<!--将该项⽬打包成可执⾏的jar的插件-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--该项⽬的启动类-->
<mainClass>com.basicbusiness.DhccBasicbusinessApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
2.⾃动⽣成跟数据相对应的实体、接⼝、xml映射⽂件的配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--location:数据库驱动包在本地的位置-->
<classPathEntry
location="G:\Program Files\apache-maven-3.5.0\oracle\ojdbc14\11.2.0.4\ojdbc14-11.2.0.4.jar"/>
<context id="Oracle" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
log4j与log4j2<property name="mergeable" value="false"></property>
<plugin type="tk.ator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mappermon.Mapper"/>
</plugin>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.2.4:21521:orcl"
userId="wisdom"
password="wisdomdb">
</jdbcConnection>
<!--⽣成Model类存放位置-->
<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
<!--⽣成映射⽂件存放位置-->
<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
<!--⽣成Dao类存放位置-->
<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}"
type="XMLMAPPER"/>
<!-- 要⽣成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="TT_RESOURCE" domainObjectName="Resource"></table>
</context>
</generatorConfiguration>
3.spring boot 的启动类:
package dhccmfish.dhccmfish;
undumstudio.socketio.AuthorizationListener;
undumstudio.socketio.Configuration;
undumstudio.socketio.HandshakeData;
undumstudio.socketio.SocketIOServer;
undumstudio.socketio.annotation.SpringAnnotationScanner;
import com.dhccmentmon.ComDef;
import fig.JdbcDataSource;
import fig.RedisConfig;
import com.dhccment.log.LogWriter;
import com.dhccment.util.BufferStream;
import com.dhccment.util.FileManager;
import com.dhccment.util.StringUtil;
import ity.ClientInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.logging.log4j.LogManager;
import at.jdbc.pool.DataSource;
batis.spring.SqlSessionFactoryBean;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.t.properties.ConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloudflix.eureka.EnableEurekaClient;
import t.annotation.Bean;
import io.support.PathMatchingResourcePatternResolver;
import org.dis.cache.RedisCacheManager;
import org.tion.jedis.JedisConnectionFactory;
import org.RedisTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.fig.annotation.web.http.EnableRedisHttpSession;
import ansaction.PlatformTransactionManager;
import ansaction.annotation.EnableTransactionManagement;
import flect.Method;
import java.util.Map;
import java.util.Properties;
//启动注册器
@EnableDiscoveryClient
@EnableEurekaClient
//启动事物管理
@EnableTransactionManagement
//mybatis的接⼝扫描位置
@MapperScan("dhccmfish.dhccmfishmon.mapper")
//设置session有效时间
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = ComDef.SESSIOM_EXPIRE_TIMEOUT)
//启动该项⽬的时候不⾃动去数据库的配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class DhccMfishApplication{
//实例化 RedisConnectionFactory 对象
@Bean(name = "jedisConnectionFactory")
public JedisConnectionFactory connectionFactory() {
tionFactory(4);
}
// 实例化 RedisTemplate 对象
@Bean(name = "redistemplate")
public RedisTemplate<Object, Object> functionDomainRedisTemplate(@Qualifier(value = "jedisConnectionFactory") JedisConnectionFactory factory) { return RedisConfig.functionDomainRedisTemplate(factory);
}
/**
* ⽣成key的策略(当缓存不写key的时候,会有⼀套规则⾃动⽣成相应的key)
*
* @return
*/
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, params) {
StringBuilder sb = new StringBuilder();
sb.Class().getName());
sb.Name());
for (Object obj : params) {
sb.String());
}
String();
}
};
}
//缓存管理器
@Bean
public CacheManager cacheManager(@Qualifier(value = "redistemplate") RedisTemplate redisTemplat
e) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
cacheManager.setDefaultExpiration(Integer.parseInt(ComDef.RdeisExpireTimeout));
return cacheManager;
}
//数据库的配置
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new at.jdbc.pool.DataSource();
}
//数据库对应mybatis的配置
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//mybatis的映射⽂件的位置
sqlSessionFactoryBean.Resources("classpath:/mybatis/*.xml"));
Object();
}
//数据库事物的配置
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DhccMfishApplication.class);
app.run();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论