spring - boot 주석 설정 my batis + druid (초보 자 출발)

8245 단어 spring-boot
spring - boot 주석 설정 my batis + druid (초보 자 출발)
초보 자 들 이 간단 한 spring boot 를 만 드 는 방법 을 소개 했다. 다음은 spring boot + my batis + druid 의 설정 방법 을 소개 한다.
저 는 재능 이 없어 서 고수 들 의 박문 과 공식 문 서 를 참고 하여 소감 을 정리 하 였 습 니 다.
고수 에 대한 감 사 는 말로 표현 할 수 없습니다.http://blog.csdn.net/lxhjh/article/details/51764604
http://blog.csdn.net/xiaoyu411502/article/details/51392237
사용 환경: JDK 1.8, eclipse - neon, maven 3.5 +, mysql 5.5
프레임 워 크 사용: springboot 1.4.1, druid 1.0.26, my batis 자동 으로 버 전 번호 가 져 오기
소개: 제 가 사용 하 는 프로필 형식 은 application. properties 입 니 다. 기본적으로 주 해 를 사용 하여 만 듭 니 다.
데이터베이스 시트 구조, databases 이름 은 my databases 입 니 다.city 로 표 시 됩 니 다. 필드 는 id, name, province 이 고 데이터 형식 은 varchar (20) 입 니 다.
첫 번 째 단계:
시작 할 때 는 springboot 프로젝트 를 새로 만 들 고 가방 을 안내 합 니 다.
새 maven 항목 입 니 다. jar 패 키 지 를 선택 하 십시오.
pom. xml 파일 을 설정 합 니 다.
  
  	1.8
  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.1.RELEASE
  
  
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        com.alibaba
        druid
        1.0.26
	
	
	
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.1.1
    
    
    
        mysql
        mysql-connector-java
    
    
    
        org.springframework.boot
        spring-boot-devtools
        true
    
    

두 번 째 단계:
시작 클래스 를 만 듭 니 다.
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan	//  Servlet
@MapperScan("mapper")	//  mapper   mybatis mapper  。
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

세 번 째 단계:
프로필 을 작성 하 다.(여기 요. 매개 변수 가 많아 서 작성 을 선택 할 수 있 습 니 다.)
응용 프로그램. properties 파일 을 사용 합 니 다. 이 파일 은 src / main / resources 디 렉 터 리 에 놓 여 있 습 니 다. springboot 프레임 워 크 가 시작 되면 자동 으로 읽 습 니 다.
#     
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabases
spring.datasource.username=root
spring.datasource.password=123
#--------------------------
#            ,           
#      ,  ,  
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
#              
spring.datasource.maxWait=60000
#              ,           ,      
spring.datasource.timeBetweenEvictionRunsMillis=60000
#                 ,      
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
#   PSCache,         PSCache    
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#          filters,       sql    ,'wall'      
spring.datasource.filters=stat,wall,log4j
#   connectProperties     mergeSql  ; SQL  
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#     DruidDataSource     
#spring.datasource.useGlobalDataSourceStat=true

네 번 째 단계:
데이터 원본 설정.여기에 관련 된 매개 변 수 는 자동 으로 datasource 에 대 입 됩 니 다.
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DruidDataSourceConfiguration {

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource druidDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		return druidDataSource;
	}
}

다섯 번 째 단계:
실체 류 를 쓰다.여 긴 마음대로 해도 되 는데.
import java.io.Serializable;

public class City implements Serializable{
	private static final long serialVersionUID = 1L;
	private String id;
	private String name;
	private String province;
	City(){
		
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	@Override
	public String toString() {
		return "City [id=" + id + ", name=" + name + ", province=" + province + "]";
	}
	
}

여섯 번 째 단계:
mapper 파일 을 작성 합 니 다.여기 가방 주 소 는 애플 리 케 이 션 의 MapperScan 값 입 니 다.
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface CityMapper {
	
	@Select("select * from city where id = #{id}")
	City findCityById(@Param("id") String id);
}

일곱 번 째 단계:
여 기 는 Service 를 쓰 지 않 고 컨트롤 러 에 직접 썼 습 니 다.
import javax.annotation.Resource;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
@EnableAutoConfiguration
public class HelloController {
	@Resource
	private CityMapper cityMapper;
	@RequestMapping("/test")
	String test1(){
		return "hello,test1()";
	}
	@RequestMapping("/findCity2")
	City findCity2(@RequestParam String id){
		return cityMapper.findCityById(id);
	}
}

여덟 번 째 단계:
druid 모니터링 에 맞 춰 filter 를 써 야 합 니 다.
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

import com.alibaba.druid.support.http.WebStatFilter;

@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
         @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//     
 }
)
public class DruidStatFilter extends WebStatFilter{

}

9 단계:
모니터링 인터페이스 설정.
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
@WebServlet(urlPatterns = "/druid/*", 
    initParams={
            @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP    (        ,       )
            @WebInitParam(name="deny",value="192.168.16.111"),// IP    (     ,deny   allow)
            @WebInitParam(name="loginUsername",value="admin"),//    
            @WebInitParam(name="loginPassword",value="123"),//   
            @WebInitParam(name="resetEnable",value="false")//   HTML    “Reset All”  
    })
public class DruidStatViewServlet extends StatViewServlet {
	private static final long serialVersionUID = 1L;
	
}

10 단계:
테스트 시작.application. java 에서 시작 합 니 다. 브 라 우 저 에서 먼저 들 어 갑 니 다: localhost: 8080 / druid / login. html, 사용자 이름 입력: admin, 비밀번호: 123, 들 어 갑 니 다.
localhost: 8080 / demo / findCity 2 를 다시 실행 하 시 겠 습 니까?id = 001, 결 과 를 봅 니 다.여기 당신 의 데이터베이스 에 데이터 가 필요 합 니 다.
모니터링 페이지 에서 sql 모니터링 이 모니터링 되 었 는 지 확인 하 세 요.
원본 첨부: 클릭 하여 링크 열기
배가 고 프 다.

좋은 웹페이지 즐겨찾기