spring boot項目摸索

之前我自己配置了 一下 springboot 項目 給大家分享一下自己整合的經驗 比較完整的一段代碼
希望能夠幫到大家

首先eclipise 下載boot 插件

  1. help–>Eclipse Marketplace 下的Search中搜索spring-tool-suite(網好的情況下 可以選擇這種安裝模式)
  2. 下載好重啓eclipse 會發現 有一個 spring Starter Project 這樣就可以直接創建一個 快捷的spring boot 項目
    在這裏插入圖片描述
  3. 配置一些main函數
package online.example.development;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;


@ComponentScan(value={"com.example.demo","online.example.development"})
@SpringBootApplication
//定時器註解
@EnableScheduling
public class Application extends SpringBootServletInitializer{
	 @Value("${server.port}")
	   private static String port;
	
	@Override
	  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	        return application.sources(Application.class);
	    }

	
	public static void main(String[] args) {
		
		SpringApplication.run(Application.class, args);
		System.err.println(port);
	}

}

ComponentScan 註解 描述的是項目啓動的時候所要掃描的包 我配置的兩個 一個是基本業務代碼路徑 一個是配置注入bean的文件路徑 這裏會報錯 原因是缺少一些jar 包

所以需要早pom 中 引入對應的jar 包

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-tomcat</artifactId>
	        <scope>provided</scope>
		</dependency>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency> 

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

然後一個 spring boot 項目配置完畢 如果能夠正常啓動 表示配置·OK

  1. spring boot 整合mybatis
    在整合mybatis的時候 走了 很多冤枉路 這裏需要注意的是
    第一 掃描mapper 路徑位置
    第二 dataSource 的文件配置
    話不多說 直接上代碼

jar包依賴 必不可少

<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
			<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.0.2</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.25</version>
		</dependency>

yml 配置核心所在

server:
  port: 8888
spring:
  datasource:
    url: jdbc:mysql://192.168.9.455:3306/ccgy_test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

這是坑最多的 dataSource 配置
順帶將pagehelper 也配置了 直接用即可
如果項目出錯 了 在註解上面加上 @Component註解

package online.example.development.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionFactoryRef = "ninthSqlSessionFactory")
public class NinthDatabaseConfig {
	//private static Logger logger = Logger.getLogger(NinthDatabaseConfig.class);

	@Bean(name = "ninthDataSource")
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource ninthDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		System.err.println("加載"+druidDataSource);
		return druidDataSource;
	}

	@Bean(name = "ninthSqlSessionFactory")
	public SqlSessionFactory ninthSqlSessionFactory() throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	//	logger.info("ninth sqlsession--" + this.ninthDataSource().hashCode());
		sqlSessionFactoryBean.setDataSource(this.ninthDataSource());
		System.err.println("11111111111載");
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("dialect", "mysql");
		properties.setProperty("pageSizeZero", "true");
		properties.setProperty("reasonable", "false");
		properties.setProperty("params", "pageNum=pageHelperStart;pageSize=pageHelperRows;");
		properties.setProperty("supportMethodsArguments", "true");
		properties.setProperty("returnPageInfo", "none");
		pageHelper.setProperties(properties);
		Interceptor[] interceptors = new Interceptor[] { pageHelper };
		sqlSessionFactoryBean.setPlugins(interceptors);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
		return sqlSessionFactoryBean.getObject();
	}


	@Bean(name = "ninthTxMan")
	public PlatformTransactionManager ninthTransactionManager() {
//		logger.info("ninth dataSource--" + this.ninthDataSource().hashCode());
		return new DataSourceTransactionManager(this.ninthDataSource());
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章