Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to integrate mybatis in spring

Shulou Source: shulou.com Published: 2022-06-02 10:42:54 10月05日 Update

This article introduces how to integrate mybatis in spring, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

Mysql profile spring-jdbc.properties:

Jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8jdbc.username=testjdbc.password=testjdbc.filters=statjdbc.maxActive=800jdbc.initialSize=5jdbc.maxWait=60000jdbc.minIdle=5jdbc.timeBetweenEvictionRunsMillis=60000jdbc.minEvictableIdleTimeMillis=36000jdbc.validationQuery=SELECT 'x'jdbc.testWhileIdle=truejdbc.testOnBorrow=falsejdbc.testOnReturn=falsejdbc.poolPreparedStatements=truejdbc.maxPoolPreparedStatementPerConnectionSize=50

Spring-mybatis.xml:

Spring Mybatis

The configuration file in the spring context references the mybatis configuration file:

Add packaging configuration to the pom.xml of maven to ensure that packaging can include mapper.xml:

Src/main/java * * / .xml src/main/resources * * / *

Mapper interface test:

Screenshot of file location:

TestMapper.xml:

SELECT COUNT (1) FROM T1

TestMapper interface:

Package com.test.mybatis.dao;public interface TestMapper {int getCount ();}

Test the code:

Package com.test.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.test.mybatis.dao.TestMapper RestController@RequestMapping (value= "/ test") public class TestController {private static final Logger logger = LoggerFactory.getLogger (TestController.class); @ Autowired TestMapper testMapper; @ RequestMapping (value= "/ test", method = RequestMethod.GET) public Object test (HttpServletRequest request, HttpServletResponse response) {int count = testMapper.getCount () Logger.info ("test: {}", count); return count;}

Results:

On how to integrate mybatis in spring to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Files configuration content interfaces more help testing good up and down context code location interests guys partners screenshots articles knowledge articles results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Redmi Docker Huawei