SpringBoot MyBatis搭建项目

1.项目结构

218B9B3E-CEA2-418f-A16B-91E4CE88FD73.png


2.application配置

# mysql

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/wifiportal?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=r123



# mybatis

mybatis.mapper-locations=classpath*:/mapper/**Mapper.xml

mybatis.type-aliases-package=com.wifi.portal.entity


3.Application启动类

@SpringBootApplication

@MapperScan("com.wifi.portal.mapper")

public class WifiPortalApplication {



   public static void main(String[] args) {

      SpringApplication.run(WifiPortalApplication.class, args);

   }



}


4.mybatis UserInfoMapper.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.wifi.portal.mapper.UserInfoMapper">

    <select id="findById" resultType="UserInfo">

        SELECT * FROM user_info WHERE id=#{id}

    </select>

</mapper>


5.dao层UserInfoMapper

import com.wifi.portal.entity.UserInfo;

import org.apache.ibatis.annotations.Mapper;

import org.springframework.stereotype.Repository;



import java.util.List;



@Mapper

@Repository

public interface UserInfoMapper {



    public UserInfo findById(Integer id);

}


6.service层UserService

public interface UserInfoService {

    public UserInfo findById(Integer id);

}


7.service实行层UserServiceImpl

@Service

public class UserInfoServiceImpl implements UserInfoService {



    @Autowired

    private UserInfoMapper userInfoMapper;



    @Override

    public UserInfo findById(Integer id) {

        return userInfoMapper.findById(id);

    }

}


8.Ctroller层UserInfoController

@Controller

public class UserInfoController {



    @Autowired

    private UserInfoService userInfoService;



    @RequestMapping("/findById")

    @ResponseBody

    public UserInfo findById(@RequestParam Integer id) {

        return userInfoService.findById(id);

    }



}


9.entity层UserInfo

public class UserInfo {

    private String name;

    private String code;



    public UserInfo() {

    }



    public UserInfo(String name, String code) {

        this.name = name;

        this.code = code;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public String getCode() {

        return code;

    }



    public void setCode(String code) {

        this.code = code;

    }



    @Override

    public String toString() {

        return String.format("name: %s - code: %s", this.name, this.code);

    }

}


10.完整的pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.3.2.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->    </parent>

    <groupId>com.wifi</groupId>

    <artifactId>portal</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>wifi-portal</name>

    <description>Wifi Portal Server</description>



    <properties>

        <java.version>1.8</java.version>

    </properties>



    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>2.1.3</version>

        </dependency>



        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.junit.vintage</groupId>

                    <artifactId>junit-vintage-engine</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

    </dependencies>



    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

        <resources>

            <resource>

                <directory>src/main/resources</directory>

                <includes>

                    <include>**/*.xml</include>

                    <include>**/*.properties</include>

                </includes>

                <filtering>true</filtering>

            </resource>

        </resources>

    </build>



</project>



遇到的问题:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

原因是使用IDEA开发,UserInfoMapper指定xml显示,忘记加.xml后缀名了,所以找不到。
还有个原因是使用maven构建项目,mapper目录下的xml文件没有打包进去,需要手动配置添加resource:

<resources>

    <resource>

        <directory>src/main/resources</directory>

        <includes>

            <include>**/*.xml</include>

            <include>**/*.properties</include>

        </includes>

        <filtering>true</filtering>

    </resource>

</resources>



 

展开阅读全文