이 방법은 최소 구성이며 MySQL 데이터베이스 멀티 스토어 연결을 지원하고 Hikari 연결 풀링을 지원하며 MyBatis (DAO 클래스 및 XML 파일 위치를위한 구성 포함)를 지원합니다.
1. pom.xml에 종속성을 소개합니다.
<!-- Begin of DB related --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <아티 팩트> Tomcat-Jdbc </artifactid> </제외> </exclusions> </depectency> <prectionency> <groupId> com.zaxxer </groupId> <artifactId> hikaricp </aUtifactid> </fexendency> <groupId> mysql </groupId> <artifactid> mysql-connector-java </artifactid> </dependency> <!-db 관련->
우리는 mybatis-spring-boot-starter 사용하여 Tomcat-JDBC Connection Pool을 제외 시키므로 Spring-Boot는 Hikaricp를 사용할 수 있는지, 두 번째 의존성이 발견되고 MySQL-connector도 사용할 수 있습니다.
2. Application.yml의 관련 구성 :
스프링 : 프로파일 : Active : Dev DataSource : Driver-Class-Name : com.mysql.jdbc.driver 사용자 이름 : 루트 비밀번호 : 123456 Hikari : MaxLifetime : 1765000 #연결 시간 (밀리 초), 타임 아웃 및 사용되지 않음 (Retired). 기본값 : 30 분. 데이터베이스 타임 아웃 MaximumpOoldsize보다 30 초 이상을 설정하는 것이 좋습니다. 15 #연결 풀에서 허용되는 최대 연결 수. 기본값 : 10; 권장 공식 : ((core_count * 2) + exply_spindle_count) mybatis : mapperlocations : mapperlocations : mapper/ *. xml --- # 개발 환경 구성 스프링 : DataSource : URL : JDBC : MySQL : // localhost : 3306/---- # TestoUrce : urt testounource : # testoUrce : # Test Environment Configuration Spring : jdbc : mysql : //192.168.0.12 : 3306/--- # 생산 환경 구성 스프링 : 프로파일 : Prod DataSource : url : jdbc : mysql : //192.168.0.13 : 3306/
그 중에서도 DataSource.url의 끝에는 dbname이 포함되어 있지 않으므로 여러 DBS가 지원 될 수 있습니다. 그것을 사용할 때는 SQL 문의 테이블 이름 앞에서 DB 이름 만 지정하면됩니다.
3. DAO 인터페이스 코드 :
패키지 com.xjj.dao; import org.apache.ibatis.annotations.mapper; import org.apache.ibatis.annotations.select; com.xjj.entity.person import; @mapper public interface persondao {@select ( "id, first_name select, first_name, firstName, last_name, lastName, 생일 _date, birthdate, sex, phone_no as phoneno" + "test.t_person where id =#{0};") 공개 사람 getpersonyid (int id); 공개 int insertperson (사람); 공개 int updatepersonByid (개인); 공개 int updatePersonByphoneno (사람); }@Mapper 주석을 사용하여 MyBatis의 찾기를 지원하고 메소드에 대한 SQL 문을 작성하는 지원.
4. XML 파일 :
리소스 디렉토리에서 Mapper 디렉토리를 작성한 다음 다음과 같이 XML 파일을 만듭니다.
<? xml version = "1.0"encoding = "utf-8"?> <! doctype mapper public "-// mybatis.org//dtd Mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namper facece ="com.xjj.da.da.da.da.dao.da.dao.da.da.da.da.da.da.do.da. 데이터베이스 사용자 테이블-> <insert id = "insertperson"> test.t_person (first_name, last_name, birth_date, sex, phone_no, update_dt) value (#{firstname},#{lastname},#{birthdate},#{sex},#{phoneno}, nows () id = "updatePersonById"> update test.t_person set first_name =#{firstName}, last_name =#{lastName}, birth_date =#{birthdate}, sex =#{sex}, phone_no =#{phoneno}#{id} </update> set first_name =#{firstname}, last_name =#{lastname}, birth_date =#{birthdate}, sex =#{sex} there phone_no =#{phoneno} </update> </mapper>5. 테스트 :
@test public void dbtest ()는 jsonProcessingException {person perient2 = persondao.getpersonByid (2); logger.info ( "개인 번호 2는 : {}", ObjectMapper.writeValueAsstring (person2)); person.2.setfirstname ( "8"); persondao.updatepersonbyid (person2); person2 = persondao.getpersonByid (2); logger.info ( "업데이트 후 2 인원 2 인 사람은 다음과 같습니다. {}", ObjectMapper.writeValueAsstring (person2)); AssertThat (person2.getFirstName (), EqualTo ( "8")); }요약
위의 것은 MySQL 데이터베이스 연결, Hikari Connection Pool 및 MyBatis가 귀하에게 소개하기위한 Spring Boot의 간단한 구성 방법입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!