1.pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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.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.实体类。以及mapper.xml文件(具体的自动生成xml文件在下面的generatorConfig.xml中) <!-- Java接口和实体类 --> <targetJavaProject>${basedir}/src/main/java</targetJavaProject> <targetMapperPackage>com.basicbusiness.mapper</targetMapperPackage> <targetModelPackage>com.dhcc.comment.entity</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> <project.reporting.outputEncoding>UTF-8</project.reporting.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>org.mybatis.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 -DgroupId=com.oracle -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>com.google.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>com.vaadin.external.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>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration>
<!--自动生成跟数据库相对应的实体,接口以及xml配置文件--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>tk.mybatis</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 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--location:数据库驱动包在本地的位置--> <classPathEntry location="G:Program Filesapache-maven-3.5.0oracleojdbc1411.2.0.4ojdbc14-11.2.0.4.jar"/> <context id="Oracle" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="mergeable" value="false"></property> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.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; import com.corundumstudio.socketio.AuthorizationListener; import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.HandshakeData; import com.corundumstudio.socketio.SocketIOServer; import com.corundumstudio.socketio.annotation.SpringAnnotationScanner; import com.dhcc.comment.common.ComDef; import com.dhcc.comment.config.JdbcDataSource; import com.dhcc.comment.config.RedisConfig; import com.dhcc.comment.log.LogWriter; import com.dhcc.comment.util.BufferStream; import com.dhcc.comment.util.FileManager; import com.dhcc.comment.util.StringUtil; import dhccmfish.dhccmfish.common.entity.ClientInfo; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.logging.log4j.LogManager; import org.apache.tomcat.jdbc.pool.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.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.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.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.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import java.lang.reflect.Method; import java.util.Map; import java.util.Properties; //启动注册器 @EnableDiscoveryClient @EnableEurekaClient
//启动事物管理 @EnableTransactionManagement
//mybatis的接口扫描位置 @MapperScan("dhccmfish.dhccmfish.common.mapper")
//设置session有效时间 @EnableRedisHttpSession(maxInactiveIntervalInSeconds = ComDef.SESSIOM_EXPIRE_TIMEOUT) //启动该项目的时候不自动去找数据库的配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class DhccMfishApplication{ //实例化 RedisConnectionFactory 对象 @Bean(name = "jedisConnectionFactory") public JedisConnectionFactory connectionFactory() { return RedisConfig.connectionFactory(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, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } //缓存管理器 @Bean public CacheManager cacheManager(@Qualifier(value = "redistemplate") RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //设置缓存过期时间 cacheManager.setDefaultExpiration(Integer.parseInt(ComDef.RdeisExpireTimeout)); return cacheManager; } //数据库的配置 @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } //数据库对应mybatis的配置 @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//mybatis的映射文件的位置 sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml")); return sqlSessionFactoryBean.getObject(); } //数据库事物的配置 @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } public static void main(String[] args) {
SpringApplication app = new SpringApplication(DhccMfishApplication.class); app.run(); } }