七、springBoot整合mybatis

前三步是環(huán)境引入,第四步開始是案例.最后把所有用到的注解說明一下。

一、新建SpringBoot項目

二、pom文件引入

這里要注意的是 Mysql驅(qū)動包SpringBoot集成mybatis框架
使用lombok你的idea還需要安裝一個插件。安裝教程如下。
lombok教程點這

<?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>

    <groupId>com.joy.mybatis.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <mybatis.spring.boot.starter.version>1.1.1</mybatis.spring.boot.starter.version>
    </properties>

    <dependencies>

        <!--springWeb組件-->
        <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>


        <!-- Mysql驅(qū)動包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- SpringBoot集成mybatis框架 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.starter.version}</version>
        </dependency>

 <!-- 這個是幫助 生成get set函數(shù) 還是打印日志的包。這個需要工具裝插件才能用   不同的工具 網(wǎng)上查一下lombok的安裝會有很多教程 更多詳細介紹請看https://blog.csdn.net/motui/article/details/79012846-->
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

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


</project>

三、配制數(shù)據(jù)源

image.png
#### DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisTest
spring.datasource.username=root
spring.datasource.password=joy123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

四、數(shù)據(jù)庫表結(jié)構(gòu)

前提是先要數(shù)對應(yīng)數(shù)據(jù)庫和表,跟據(jù)第三步中的數(shù)據(jù)源創(chuàng)建mybatisTest數(shù)據(jù)庫和User表.字段就是id,username,password,sex。表如下結(jié)構(gòu)

image.png

建表語法如下

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  `age` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = 1;

五、數(shù)據(jù)訪問層

實體bean代碼
image.png

dao層


image.png

代碼如下

package com.joy.mybatis.demo.demo.dao;

import com.joy.mybatis.demo.demo.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper    //mybatis用注解說明表掃描這個類為Mapper
public interface UserMapper {

    @Select("select * from users where username=#{username}")
    User findByUserName(@Param("username") String username);

    @Insert("insert into users(username,password,age)values(#{username},#{password},#{age})")
    int insert(User user);
}

六、業(yè)務(wù)邏輯層

image.png

代碼如下

package com.joy.mybatis.demo.demo.service;

import com.joy.mybatis.demo.demo.bean.User;
import com.joy.mybatis.demo.demo.dao.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j     //這個是依賴  lombok的注解  可以快帶打印日志
public class UserServcie {

    @Autowired
    UserMapper userMapper;

    public User findByUserName(String username) {
        return null;
    }

    public int insert(User user) {
        int result = userMapper.insert(user);
        log.info(user.toString());
        return result;
    }
}

七、控制層代碼

image.png
package com.joy.mybatis.demo.demo.controllers;

import com.joy.mybatis.demo.demo.bean.User;
import com.joy.mybatis.demo.demo.service.UserServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserServcie userServcie;

    @RequestMapping("/addUser")
    public Integer addUser(User user){
        return  userServcie.insert(user);
    }

}

八、測試

啟動函數(shù)


image.png

啟動項目在瀏覽器輸入:
http://localhost:8080/addUser?username=aaa&password=123456&age=18

數(shù)據(jù)庫中會有:


image.png

九:總結(jié)注解

1.從數(shù)據(jù)訪問層
@Mapper    //mybatis用注解說明表掃描這個類為Mapper ,說明這個類是直接是數(shù)據(jù)庫訪問層。

如果不加這個注解,也可以在項目啟動類中加入MapperScan,里面的參數(shù)是需要dao層需要掃描包的路徑


image.png
@MapperScan(basePackages = "com.joy.mybatis.demo.demo.dao")
2.
@Slf4j     //這個是依賴  lombok的注解  可以快帶打印日志

加入這個注解就可以直接如下寫代碼了。


image.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)容

  • 照例又該寫篇簡書了,可此刻的我腦子里一片空白。這一天也不知怎么過的,昏昏沉沉的。天氣相當不好,清晨的一場暴...
    慕敖閱讀 280評論 0 1
  • 寫文章或者說跟人聊天,都是自己內(nèi)心深處知識涵養(yǎng)的再現(xiàn)。 我堅持寫文章,兩個月,發(fā)現(xiàn)自己已有的東西,都已經(jīng)被寫完了。...
    馬二郎閱讀 192評論 0 0

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