{"id":1936,"date":"2025-02-17T17:27:21","date_gmt":"2025-02-17T09:27:21","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1936"},"modified":"2025-02-17T17:27:52","modified_gmt":"2025-02-17T09:27:52","slug":"how-spring-boot-injects-sqlsessiontemplate-for-mapper-interfaces","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1936","title":{"rendered":"How Spring Boot Injects `SqlSessionTemplate` for `@Mapper` Interfaces"},"content":{"rendered":"<p>In <strong>Spring Boot with MyBatis<\/strong>, the <code>@Mapper<\/code> annotation is used to mark an interface as a MyBatis Mapper. <strong>Spring Boot automatically injects <code>SqlSessionTemplate<\/code> behind the scenes<\/strong> to enable SQL execution for these mapper interfaces.<\/p>\n<h3><strong>How Spring Boot Injects <code>SqlSessionTemplate<\/code> for <code>@Mapper<\/code> Interfaces<\/strong><\/h3>\n<p>Spring Boot and MyBatis integrate through <code>MyBatis-Spring-Boot-Starter<\/code>, which:<\/p>\n<ol>\n<li><strong>Scans for <code>@Mapper<\/code> interfaces<\/strong> and registers them as Spring beans.<\/li>\n<li><strong>Automatically configures a <code>SqlSessionFactory<\/code> and <code>SqlSessionTemplate<\/code>.<\/strong><\/li>\n<li><strong>Injects <code>SqlSessionTemplate<\/code> behind the scenes<\/strong> into each <code>@Mapper<\/code> interface.<\/li>\n<\/ol>\n<hr \/>\n<h3><strong>Example Setup<\/strong><\/h3>\n<h4><strong>1. Add Dependencies<\/strong><\/h4>\n<p>If using Maven, add:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.mybatis.spring.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;mybatis-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;3.0.3&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h4><strong>2. Define the MyBatis <code>@Mapper<\/code> Interface<\/strong><\/h4>\n<pre><code class=\"language-java\">@Mapper\npublic interface UserMapper {\n    @Select(&quot;SELECT * FROM users WHERE id = #{id}&quot;)\n    User findById(@Param(&quot;id&quot;) Long id);\n}<\/code><\/pre>\n<h4><strong>3. Configure MyBatis in <code>application.properties<\/code><\/strong><\/h4>\n<pre><code class=\"language-properties\">spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/mydb\nspring.datasource.username=root\nspring.datasource.password=root\nspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver\nmybatis.mapper-locations=classpath:mappers\/*.xml<\/code><\/pre>\n<h4><strong>4. Enable <code>@MapperScan<\/code> in the <code>@SpringBootApplication<\/code><\/strong><\/h4>\n<pre><code class=\"language-java\">@SpringBootApplication\n@MapperScan(&quot;com.example.mappers&quot;)  \/\/ Automatically scans @Mapper interfaces\npublic class MyApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(MyApplication.class, args);\n    }\n}<\/code><\/pre>\n<h4><strong>5. Use the <code>UserMapper<\/code> in a Service<\/strong><\/h4>\n<pre><code class=\"language-java\">@Service\npublic class UserService {\n\n    @Autowired\n    private UserMapper userMapper;\n\n    public User getUserById(Long id) {\n        return userMapper.findById(id);\n    }\n}<\/code><\/pre>\n<hr \/>\n<h3><strong>How <code>SqlSessionTemplate<\/code> Works Behind the Scenes<\/strong><\/h3>\n<ul>\n<li><strong>Spring Boot auto-configures <code>SqlSessionFactory<\/code> and <code>SqlSessionTemplate<\/code>.<\/strong><\/li>\n<li><strong>MyBatis-Spring framework generates a dynamic proxy for <code>UserMapper<\/code><\/strong> and internally uses <code>SqlSessionTemplate<\/code>.<\/li>\n<li><strong>No need to manually inject <code>SqlSessionTemplate<\/code>.<\/strong> It is injected by Spring Boot through <code>SqlSessionFactoryBean<\/code>.<\/li>\n<\/ul>\n<h3><strong>Manually Defining <code>SqlSessionTemplate<\/code> (If Needed)<\/strong><\/h3>\n<p>If you want explicit control over <code>SqlSessionTemplate<\/code>, you can configure it manually:<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class MyBatisConfig {\n\n    @Bean\n    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {\n        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();\n        factoryBean.setDataSource(dataSource);\n        return factoryBean.getObject();\n    }\n\n    @Bean\n    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {\n        return new SqlSessionTemplate(sqlSessionFactory);\n    }\n}<\/code><\/pre>\n<hr \/>\n<h3><strong>Summary<\/strong><\/h3>\n<p>\u2705 Spring Boot <strong>automatically injects <code>SqlSessionTemplate<\/code><\/strong> into <code>@Mapper<\/code> interfaces.<br \/>\n\u2705 <strong>No need to manually define <code>SqlSessionTemplate<\/code><\/strong> unless you have custom configurations.<br \/>\n\u2705 <code>@MapperScan<\/code> allows <strong>Spring Boot to detect and register <code>@Mapper<\/code> interfaces automatically<\/strong>.<br \/>\n\u2705 <code>UserMapper<\/code> gets proxied using <code>SqlSessionTemplate<\/code>, handling database interactions seamlessly.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Spring Boot with MyBatis, the @Mapper annotation is used to mark an interface as a MyBatis Mapper. Spring Boot automatically injects SqlSessionTemplate behind the scenes to enable SQL execution for these mapper interfaces. How Spring Boot Injects SqlSessionTemplate for @Mapper Interfaces Spring Boot and MyBatis integrate through MyBatis-Spring-Boot-Starter, which: Scans for @Mapper interfaces and [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1936\" title=\"Permanent Link to: How Spring Boot Injects `SqlSessionTemplate` for `@Mapper` Interfaces\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1936","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1936"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1936"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1936\/revisions"}],"predecessor-version":[{"id":1937,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1936\/revisions\/1937"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}