springboot+mybatis+mysql整合

一.通過idea建立工程


1.png

2.png

3.png

4.png

二.配置文件
1.在resources文件夾下建立config文件夾,并創(chuàng)建mybatis-config.xml,代碼如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.wyh.springbootmybatisdemo.pojo"/>
    </typeAliases>
    <!--<mappers>-->
    <!--</mappers>-->
</configuration>

2.在resources文件夾下建立mapper文件夾,并創(chuàng)建UserMapper.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.wyh.springbootmybatisdemo.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.wyh.springbootmybatisdemo.pojo.User" >
        <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="password" property="password" />
    </resultMap>
    <select id="findById" resultMap="BaseResultMap">
        select * from user where id = #{id}
    </select>
    <select id="findAll" resultMap="BaseResultMap">
        select * from user
    </select>
</mapper>

3.application.properties配置文件代碼如下:

spring.datasource.url=jdbc:mysql://localhost:3306/webtest?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver

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

如果遇到下列錯誤,是由于MySQL 這個jar 包依賴類型默認(rèn)是runtime ,
也就是說只有運行時生效,所以雖然這里報錯,但是不影響你代碼運行。


error.png

在pom.xml文件內(nèi)將依賴改成compile就行


image.png

三.pojo類
在com.wyh.springbootmybatisdemo下建立pojo包,并在該包下建立User類
import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private String password;

    public User() {
    }

    public User(Long id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

四.mapper
在com.wyh.springbootmybatisdemo下建立mapper包,并在該包下建立UserMapper接口

import com.wyh.springbootmybatisdemo.pojo.User;

import java.util.List;

public interface UserMapper {
    User findById(Long id);
    List<User> findAll();
}

五.controller控制器
在com.wyh.springbootmybatisdemo下建立controller包,并在該包下建立UserController類


import com.wyh.springbootmybatisdemo.mapper.UserMapper;
import com.wyh.springbootmybatisdemo.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserMapper userMapper;

    @GetMapping
    public User findUser(@RequestParam(value = "id") Long id){
        return userMapper.findById(id);
    }
}

六.啟動類
在啟動類上面加入掃描的注解
@MapperScan("com.wyh.springbootmybatisdemo.mapper")
如下所示:

@SpringBootApplication
@MapperScan("com.wyh.springbootmybatisdemo.mapper")
public class SpringbootmybatisdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootmybatisdemoApplication.class, args);
    }

}

七.數(shù)據(jù)庫
建立webtest數(shù)據(jù)庫,user表結(jié)構(gòu)如下:


mysql_user表

八.項目結(jié)構(gòu)目錄


image.png

九. 啟動springboot項目并測試
在瀏覽器url輸入http://localhost:8080/user?id=1

test.png

測試成功?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容