日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

springboot2整合mysql5_SpringBoot2.X (二十五):SpringBoot整合 Mybatis + MySQL CURD 示例

發(fā)布時(shí)間:2023/12/10 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot2整合mysql5_SpringBoot2.X (二十五):SpringBoot整合 Mybatis + MySQL CURD 示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

話不多數(shù),直接開始擼代碼…

工程結(jié)構(gòu)圖

開始之前先放張工程結(jié)構(gòu)圖

1、maven 依賴:

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.9

com.alibaba

druid-spring-boot-starter

1.1.9

org.projectlombok

lombok

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

2、yml 配置文件:

spring:

application:

name: mybatis-curd

datasource:

username: root

password: root

url: jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&autoReconnect=true

continue-on-error: true

sql-script-encoding: UTF-8

driver-class-name: com.mysql.jdbc.Driver

type: com.alibaba.druid.pool.DruidDataSource

druid:

initial-size: 5

min-idle: 5

max-active: 20

max-wait: 60000

# 間隔多久進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接

time-between-eviction-runs-millis: 60000

# 一個(gè)連接在池中最小生存的時(shí)間

min-evictable-idle-time-millis: 300000

validation-query: SELECT 1 FROM DUAL

test-while-idle: true

test-on-borrow: false

test-on-return: false

pool-prepared-statements: true

max-pool-prepared-statement-per-connection-size: 20

connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

use-global-data-source-stat: true

filters: stat,wall,log4j2

mybatis:

type-aliases-package: com.fxbin.mybaits.*

configuration:

map-underscore-to-camel-case: true

# 打印sql, 方便調(diào)試

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

use-generated-keys: true

default-statement-timeout: 60

default-fetch-size: 100

3、核心代碼:

User.java

package com.fxbin.mybatis.bean;

import lombok.Data;

import java.util.Date;

/**

* created with IntelliJ IDEA.

* author: fxbin

* date: 2018/10/21

* time: 5:59

* version: 1.0

* description:

*/

@Data

public class User {

/**

* 主鍵ID

*/

private Integer id;

/**

* 用戶名

*/

private String username;

/**

* 密碼

*/

private String password;

/**

* 創(chuàng)建時(shí)間

*/

private Date gmtCreate;

/**

* 修改時(shí)間

*/

private Date gmtModified;

}

MyBatisConfig.java

package com.fxbin.mybatis.config;

import com.github.pagehelper.PageHelper;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**

* created with IntelliJ IDEA.

* author: fxbin

* date: 2018/10/21

* time: 11:33

* version: 1.0

* description: MyBatis 分頁插件配置

*/

@Configuration

public class MyBatisConfig {

@Bean

public PageHelper pageHelper(){

PageHelper pageHelper = new PageHelper();

Properties p = new Properties();

// 設(shè)置為true時(shí),會(huì)將RowBounds第一個(gè)參數(shù)offset當(dāng)成pageNum頁碼使用

p.setProperty("offsetAsPageNum","true");

//設(shè)置為true時(shí),使用RowBounds分頁會(huì)進(jìn)行count查詢

p.setProperty("rowBoundsWithCount","true");

p.setProperty("reasonable","true");

pageHelper.setProperties(p);

return pageHelper;

}

}

UserController.java

package com.fxbin.mybatis.controller;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fxbin.mybatis.bean.User;

import com.fxbin.mybatis.service.UserService;

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;

/**

* created with IntelliJ IDEA.

* author: fxbin

* date: 2018/10/21

* time: 5:53

* version: 1.0

* description:

*/

@RestController

public class UserController {

@Resource

private UserService userService;

/**

* 查詢?nèi)?/p>

* @param page

* @param size

* @return

*/

@RequestMapping("/listAll")

public Object listAll(@RequestParam(value = "page",defaultValue = "1")int page,

@RequestParam(value = "size",defaultValue = "10")int size){

return userService.listAll(page, size);

}

/**

* 添加數(shù)據(jù)

* @param user

* @return

*/

@RequestMapping("/insert")

public int insert (User user){

return userService.insert(user);

}

/**

* 刪除

* @param userId

* @return

*/

@RequestMapping("/remove")

public int remove(Integer userId){

return userService.remove(userId);

}

/**

* 修改

* @param user

* @return

*/

@RequestMapping("/update")

public int update(User user){

return userService.update(user);

}

}

UserService.java

package com.fxbin.mybatis.service;

import com.fxbin.mybatis.bean.User;

import java.util.List;

/**

* created with IntelliJ IDEA.

* author: fxbin

* date: 2018/10/21

* time: 5:54

* version: 1.0

* description:

*/

public interface UserService {

Object listAll(int page, int size);

int insert(User user);

int remove(Integer userId);

int update(User user);

}

UserServiceImpl.java

package com.fxbin.mybatis.service.impl;

import com.fxbin.mybatis.bean.User;

import com.fxbin.mybatis.mapper.UserMapper;

import com.fxbin.mybatis.service.UserService;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

/**

* created with IntelliJ IDEA.

* author: fxbin

* date: 2018/10/21

* time: 5:54

* version: 1.0

* description:

*/

@Service

public class UserServiceImpl implements UserService {

@Resource

private UserMapper userMapper;

@Override

public Object listAll(int page, int size) {

PageHelper.startPage(page, size);

List userList = userMapper.listAll();

PageInfo pageInfo = new PageInfo<>(userList);

return pageInfo;

}

@Override

public int insert(User user) {

return userMapper.insert(user);

}

@Override

public int remove(Integer userId) {

return userMapper.remove(userId);

}

@Override

public int update(User user) {

return userMapper.update(user);

}

}

UserMapper.java

package com.fxbin.mybatis.mapper;

import com.fxbin.mybatis.bean.User;

import org.apache.ibatis.annotations.*;

import java.util.List;

/**

* created with IntelliJ IDEA.

* author: fxbin

* date: 2018/10/21

* time: 5:55

* version: 1.0

* description:

*/

@Mapper

public interface UserMapper {

@Select({

"select * from user"

})

List listAll();

@Insert({

"insert into user(`username`, `password`) values(#{username}, #{password})"

})

int insert(User user);

@Delete({

"delete from user where id = #{userId}"

})

int remove(Integer userId);

@Update({

"update user set username = #{username}, password = #{password} where id = #{id}"

})

int update(User user);

}

4、測試

1)查詢

2)添加

查看數(shù)據(jù)庫記錄,進(jìn)一步驗(yàn)證,確實(shí)已經(jīng)新增了一條記錄

3)移除

查看數(shù)據(jù)庫,id 為1 的記錄已經(jīng)成功移除

4)修改

我們修改之前新增的id為13 的記錄,username 修改為 “修改”,password 修改為 “222”

查看數(shù)據(jù)庫修改記錄,已成功

總結(jié):

以上就是有關(guān) SpringBoot2.X 整合 Mysql + Mybatis 的 CURD 的簡單案例,這里我沒有對(duì)返回結(jié)果進(jìn)行封裝,各位可根據(jù)自行需要,進(jìn)行返回結(jié)果的封裝…

— end —

如有問題,請(qǐng)及時(shí)聯(lián)系,謝謝

總結(jié)

以上是生活随笔為你收集整理的springboot2整合mysql5_SpringBoot2.X (二十五):SpringBoot整合 Mybatis + MySQL CURD 示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。