IDEA中SpringBoot整合MyBatis

1.使用IDEA创建SpringBoot项目的详细步骤

(1)点击Create New Project

(2)点击Spring Initializr

(3)修改项目名与包名,然后一路next下去。

 

2.项目的整体结构如下

 

3.详细代码

(1)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 http://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.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sun</groupId>
    <artifactId>mydata</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mydata</name>
    <description>Demo project for Spring Boot</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.13</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)建表SQL语句

CREATE TABLE account (  
	id int(11) NOT NULL AUTO_INCREMENT,  
	name varchar(20) NOT NULL,  
	money double DEFAULT NULL,  
	PRIMARY KEY (id) 
 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; 
 
 INSERT INTO account VALUES ('1', 'aaa', '1000'); 
 INSERT INTO account VALUES ('2', 'bbb', '1000'); 
 INSERT INTO account VALUES ('3', 'ccc', '1000'); 

(3)AccountController

package com.sun.mydata.controller;

import com.sun.mydata.domain.Account;
import com.sun.mydata.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController=@Controller+@ResponseBody
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    AccountService accountService;

    @RequestMapping("/{id}")
    public Account get(@PathVariable("id") int id){
        return accountService.findById(id);
    }
}

(4)AccountDao

package com.sun.mydata.dao;

import com.sun.mydata.domain.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
public interface AccountDao {
    public Account findById(int id);
}

(5)Account

package com.sun.mydata.domain;

public class Account {
    private int id;
    private String name;
    private double money;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

(6)AccountService

package com.sun.mydata.service;

import com.sun.mydata.domain.Account;

public interface AccountService {
    public Account findById(int id);
}

(7)AccountServiceImpl(accountDaoc]可能报错,不管他,不影响整个项目的正常运行。如果有强迫症的话,可以进入设置,把针对autowired的错误提示关闭)

package com.sun.mydata.service.impl;

import com.sun.mydata.dao.AccountDao;
import com.sun.mydata.domain.Account;
import com.sun.mydata.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public Account findById(int id) {
        return accountDao.findById(id);
    }
}

(8)AccountMapper.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.sun.mydata.dao.AccountDao">
    <select id="findById" parameterType="int" resultType="com.sun.mydata.domain.Account">
        select * from account where id=#{id}
    </select>
</mapper>

(9)application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydata?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=a123

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

 

4.测试

1.输入http://localhost:8080/account/1,即可看到输出的json数据