springboot 零xml集成mybatis

Posted 书山有路勤为径,学海无涯苦作舟(肖建锋)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 零xml集成mybatis相关的知识,希望对你有一定的参考价值。

maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>cn.xiaojf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-mybatis-annotation</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>

    </dependencies>

</project>

application.properties

#应用端口号
server.port=8010
#freemarker 默认文件后缀
spring.freemarker.suffix=.html

#数据库设置
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
#mybatis 设置
##mybatis.type-aliases-package=cn.xiaojf.springboot.mybatis.annotation.entity
mybatis.mapper-locations=classpath:**/mapper/*.xml
Application.java
package cn.xiaojf.springboot.mybatis.annotation;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Mapper
package cn.xiaojf.springboot.mybatis.annotation.mapper;

import cn.xiaojf.springboot.mybatis.annotation.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface UserMapper {
    List<User> findByUserName(String name);

    User findUserAddrByName(String name);
}

在service中的使用

UserService.java
package cn.xiaojf.springboot.mybatis.annotation.service;

public interface UserService {
    void findByUserName(String name);

    void findUserAddrByName(String name);
}
UserServiceImpl.java
package cn.xiaojf.springboot.mybatis.annotation.service.impl;

import cn.xiaojf.springboot.mybatis.annotation.entity.User;
import cn.xiaojf.springboot.mybatis.annotation.entity.UserAddr;
import cn.xiaojf.springboot.mybatis.annotation.mapper.UserMapper;
import cn.xiaojf.springboot.mybatis.annotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void findByUserName(String name) {
        List<User> list = userMapper.findByUserName(name);
        if (list != null) {
            for (User user : list) {
                System.out.println(user.getName());
            }
        }
    }

    @Override
    public void findUserAddrByName(String name) {
        User user = userMapper.findUserAddrByName(name);
        if (user != null) {
            List<UserAddr> userAddrList = user.getUserAddrList();
            if (userAddrList != null) {
                for (UserAddr userAddr : userAddrList) {
                    System.out.println(userAddr.getName());
                }
            }
        }
    }
}

 

源码地址 

https://gitee.com/xiaojf/springboot-demo/tree/master/springboot-mybatis-annotation 

 

以上是关于springboot 零xml集成mybatis的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot系列SpringBoot集成Fast Mybatis

xml SpringBoot-集成的MyBatis及MyBatis的加

SpringBoot+Mybatis集成搭建

SpringBoot——SpringBoot集成MyBatis

springboot通过xml集成mybatis实现mysql数据库操作探坑记

SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)