springboot如何集成mybatis呢?

戚薇 SpringBoot 2022-07-15 13:51:11 3795 1
下文笔者讲述SpringBoot集成MyBatis的方法分享,如下所示
实现思路:
    引入mybatis-spring-boot-starter
	即可在SpringBoot中使用MyBatis

pom.xml配置
 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.0.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
<!--        mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.yml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

application.yml配置

mybatis的xml文件我是放在了resources下mybatis文件夹中
#mybatis
mybatis:
  mapper-locations: classpath*:mybatis/*.xml
  type-aliases-package: com.java265.pojo
  configuration:
    map-underscore-to-camel-case: true
#showSql
logging:
  level:
    com.java265.mapper: debug
#pagehelper
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: countSql

查询示例分享

@Data
@NoArgsConstructor
@AllArgsConstructor
 
public class User implements Serializable {
    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Integer age;
}
@Repository
public interface UserMapper {
    List<User> batchFind(List<Integer> ids);
 
     List<User> list();
}
@Service
public class UserService {
    @Autowired
    EmployeeMapper userMapper;
 
    public List<User> batchFind(List<Integer> ids){
        return userMapper.batchFind(ids);
    }
 
    public List<User> list(){
        return userMapper.list();
    }
}

mybaits之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 namespace="com.java265.mapper.UserMapper">
    <select id="batchFind" resultType="User" parameterType="java.util.List">
        select * from tbl_user
        <where>
            <if test="list!=null and list.size()>0">
                and id in
                <foreach collection="list" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
测试

    @Test
    public void test2(){
        List<User> users = UserService.batchFind(Arrays.asList(1, 3, 4));
        log.info("结果:[{}]",users);
    } 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: http://www.Java265.com/JavaFramework/SpringBoot/202207/3981.html

最近发表

热门文章

好文推荐

Java265.com

//www.java265.com

站长统计|USA-001

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者