Configuration Setup
Establish database connectivity and tune the connection pool via application.yml. The following excerpt demonstrates HikariCP parameter optimization alongside MyBatis scanning direcitves.
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://db-server:3306/data_store?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: admin
password: secure_pass_456
hikari:
validation-timeout: 3000
connection-timeout: 30000
idle-timeout: 600000
minimum-idle: 5
maximum-pool-size: 10
max-lifetime: 1800000
connection-test-query: SELECT 1
mybatis:
type-aliases-package: com.demo.persistence.model
mapper-locations: classpath:mapper/*Dao.xml
Application Initialization
The bootstrap class must instruct Spring to locate DAO interfaces. Applying @MapperScan eliminates the need for individual @Mapper annotations on each repository.
package com.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.demo.persistence.dao")
@SpringBootApplication
public class PersistenceBootstrapper {
public static void main(String[] args) {
SpringApplication.run(PersistenceBootstrapper.class, args);
}
}
Persistent Model Definition
Define the domain object that maps to the underlying table. An explicit alias ensures accurate result set mapping without fully qualified class references.
package com.demo.persistence.model;
import lombok.Data;
import org.apache.ibatis.type.Alias;
@Data
@Alias("ArticleNode")
public class ArticleModel {
private String recordId;
private String heading;
private String bodyContent;
}
Data Access Layer
Declare the abstraction for data base interactions followed by the executable SQL definition.
package com.demo.persistence.dao;
import com.demo.persistence.model.ArticleModel;
import org.springframework.stereotype.Component;
@Component
public interface ArticleDao {
ArticleModel fetchRecord(String recordId);
}
Corresponding SQL mapping:
<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="com.demo.persistence.dao.ArticleDao">
<select id="fetchRecord" resultType="ArticleNode">
SELECT record_id, heading, body_content
FROM articles
WHERE record_id = #{recordId}
</select>
</mapper>
Service Layer Implementation
Encapsulate data operations behind a business contract. Constructor injection promotes testability and explicit dependencies.
package com.demo.business.service;
import com.demo.persistence.model.ArticleModel;
public interface ArticleService {
ArticleModel retrieveById(String recordId);
}
package com.demo.business.service.impl;
import com.demo.persistence.dao.ArticleDao;
import com.demo.persistence.model.ArticleModel;
import com.demo.business.service.ArticleService;
import org.springframework.stereotype.Service;
@Service
public class DefaultArticleService implements ArticleService {
private final ArticleDao articleDao;
public DefaultArticleService(ArticleDao articleDao) {
this.articleDao = articleDao;
}
@Override
public ArticleModel retrieveById(String recordId) {
return articleDao.fetchRecord(recordId);
}
}
REST Controller Exposure
Expose the retrieval workflow through HTTP endpoints. Path variables simplify request routing.
package com.demo.web.controller;
import com.demo.business.service.ArticleService;
import com.demo.persistence.model.ArticleModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ArticleController {
private final ArticleService articleService;
public ArticleController(ArticleService articleService) {
this.articleService = articleService;
}
@GetMapping("/articles/{id}")
public ArticleModel getArticle(@PathVariable String id) {
return articleService.retrieveById(id);
}
}