国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

spring boot 整合jdbc和事務(wù)

這篇具有很好參考價(jià)值的文章主要介紹了spring boot 整合jdbc和事務(wù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

目錄

1、j在pom文件中引入dbc驅(qū)動(dòng)與mybatis

?2、數(shù)據(jù)庫(kù)連接池參數(shù)配置

3、mybatis配置

4、application配置

5、通用mapper配置

6、編寫UserMapper

7、user表(需要自行去mysql創(chuàng)建數(shù)據(jù)庫(kù)表新增用戶數(shù)據(jù),sql語句見下一步)

8、sql信息

9、UserService編寫

10、測(cè)試結(jié)果

11、測(cè)試輸出結(jié)果


訪問效果

spring boot 整合jdbc和事務(wù),后端java,spring boot,java,spring

????????springboot連接數(shù)據(jù)庫(kù)需要整合jdbc與事務(wù),那么改怎么處理,答案是不需要我們處理,springboot已經(jīng)實(shí)現(xiàn),我們只需在pom文件中引入對(duì)應(yīng)的庫(kù)然后簡(jiǎn)單配置即可實(shí)現(xiàn)。jdbc驅(qū)動(dòng)的引入,別忘了還有mybatis引入。下面我們來實(shí)現(xiàn)下:

1、j在pom文件中引入dbc驅(qū)動(dòng)與mybatis

        <!--jdbc事務(wù)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- mysql 數(shù)據(jù)庫(kù)驅(qū)動(dòng) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

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

?2、數(shù)據(jù)庫(kù)連接池參數(shù)配置

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mumu
    username: "數(shù)據(jù)庫(kù)賬號(hào)"
    password: "數(shù)據(jù)庫(kù)密碼"

3、mybatis配置

#mybatis
# mybatis配置
mybatis:
  # 實(shí)體類別名包路徑
  type-aliases-package: springbootdemo.pojo
  # 映射文件路徑
  # mapper-locations: classpath:mappers/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4、application配置

在啟動(dòng)類上添加掃描包注解(推薦): import tk.mybatis.spring.annotation.MapperScan;別導(dǎo)錯(cuò)包。
package springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("springbootdemo.mapper")
public class Application {

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

}

5、通用mapper配置

????????通用Mapper的作者也為自己的插件編寫了啟動(dòng)器,我們直接引入即可。在項(xiàng)目的 pom.xml 文件中加入如下依賴,注意:一旦引入了通用Mapper的啟動(dòng)器,會(huì)覆蓋Mybatis官方啟動(dòng)器的功能,因此需要移除對(duì)官方Mybatis啟動(dòng)器的依賴。

     <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

6、編寫UserMapper

package springbootdemo.mapper;

import springbootdemo.pojo.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {
}

7、user表(需要自行去mysql創(chuàng)建數(shù)據(jù)庫(kù)表新增用戶數(shù)據(jù),sql語句見下一步)

package springbootdemo.pojo;

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Data
@Table(name = "tb_user")
public class User{
    // id
    @Id
    //開啟主鍵自動(dòng)回填
    @KeySql(useGeneratedKeys = true)
    private Long id;
    // 用戶名
    private String userName;
    // 密碼
    private String password;
    // 姓名
    private String name;
    // 年齡
    private Integer age;
    // 性別,1男性,2女性
    private Integer sex;
    // 出生日期
    private Date birthday;
    // 創(chuàng)建時(shí)間
    private Date created;
    // 更新時(shí)間
    private Date updated;
    // 備注
    private String note;
}

8、sql信息


-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) DEFAULT NULL COMMENT '用戶名',
  `password` varchar(100) DEFAULT NULL COMMENT '密碼',
  `name` varchar(100) DEFAULT NULL COMMENT '姓名',
  `age` int(10) DEFAULT NULL COMMENT '年齡',
  `sex` tinyint(1) DEFAULT NULL COMMENT '性別,1男性,2女性',
  `birthday` date DEFAULT NULL COMMENT '出生日期',
  `note` varchar(255) DEFAULT NULL COMMENT '備注',
  `created` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
  `updated` datetime DEFAULT NULL COMMENT '更新時(shí)間',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'sw', '123456', '千帆v', '30', '1', '1964-08-08', '李先生很棒', '2014-09-19 16:56:04', '2014-09-21 11:24:59');
INSERT INTO `tb_user` VALUES ('2', '34r', '123456', '李哇', '21', '2', '1995-01-01', '李李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('3', 'dsff', '123456', '王如何', '22', '2', '1994-01-01', '李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('4', 'ht', '123456', '張奧迪', '20', '1', '1996-09-01', '李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('5', 'grr', '123456', '李二', '28', '1', '1988-01-01', '李娜李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('6', 'rr', '123456', '李法人', '23', '1', '1993-08-08', '李雷李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('7', 'hh', '123456', '威威', '24', '2', '1992-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('8', 'hrr', '123456', '為如', '21', '2', '2008-07-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('9', 'kytt', '123456', '馬搭', '18', '2', '2012-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('10', 'gvr', '123456', '安安', '45', '2', '1971-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('11', 'htrh', '123456', 'de', '33', '2', '1983-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('12', 'wre', '123456', '馬大哈', '46', '2', '1980-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

9、UserService編寫

package springbootdemo.UserService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import springbootdemo.mapper.UserMapper;
import springbootdemo.pojo.User;

@Service
public class UserService {

    @Autowired(required=false)
    private UserMapper userMapper;

    //根據(jù)id查詢
    public User queryById(Long id) {

        return userMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public void saveUser(User user) {
        System.out.println("新增用戶...");
        userMapper.insertSelective(user);
    }
}

10、測(cè)試結(jié)果

package springbootdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import springbootdemo.UserService.UserService;
import springbootdemo.pojo.User;

import javax.sql.DataSource;

@RestController
public class HelloController {


    @Autowired
    private UserService userService;
    /**
     * 根據(jù)id獲取用戶
     * @param id 用戶id
     * @return 用戶
     */
    @GetMapping("/user/{id}")
    public User queryById(@PathVariable Long id){
        return userService.queryById(id);
    }

}

11、測(cè)試輸出結(jié)果

瀏覽器輸入地址:http://localhost:8080/user/6文章來源地址http://www.zghlxwxcb.cn/news/detail-610025.html

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49e8af71] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@546085857 wrapping com.mysql.jdbc.JDBC4Connection@574e7689] will not be managed by Spring
==>  Preparing: SELECT id,user_name,password,name,age,sex,birthday,created,updated,note FROM tb_user WHERE id = ? 
==> Parameters: 6(Long)
<==    Columns: id, user_name, password, name, age, sex, birthday, created, updated, note
<==        Row: 6, lilei, 123456, 李雷, 23, 1, 1993-08-08, 2014-09-20 11:41:15.0, 2014-09-20 11:41:15.0, 李先生很棒
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49e8af71]

到了這里,關(guān)于spring boot 整合jdbc和事務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Java核心知識(shí)】spring boot整合Mybatis plus + Phoenix 訪問Hbase與使用注意

    為什么Phoenix能讓開發(fā)者通過SQL訪問Hbase而不必使用原生的方式?引用Phoenix官網(wǎng)上的一句話:SQL is just a way of expressing what you want to get not how you want to get it . 即SQL不是一種數(shù)據(jù)操作技術(shù),而是一種特殊的表達(dá)方式。只是表示你需要什么而不是你如何獲得。 一個(gè)集成了Phoenix的Hb

    2024年02月15日
    瀏覽(23)
  • spring boot java項(xiàng)目整合Scala&Spark,接口api調(diào)用方式調(diào)用scala代碼,配置分享

    spring boot java項(xiàng)目整合Scala&Spark,接口api調(diào)用方式調(diào)用scala代碼,配置分享

    版本說明: spring boot: 2.5.9 jdk:1.8 spark:2.4.5 sclala:2.11.12 首先你需要有一個(gè)完美的spring boot項(xiàng)目(java版本)能成功運(yùn)行,這就不贅述了,按照網(wǎng)上的自己搭建吧,然后重要的來了,我搗鼓了兩天時(shí)間,各樣的報(bào)錯(cuò)見過了,網(wǎng)上的處理方法要嘛是不全,要嘛是沒有用,各種辦

    2024年02月10日
    瀏覽(30)
  • 【Spring Boot系列】- Spring Boot事務(wù)應(yīng)用詳解

    事務(wù)(Transaction)是數(shù)據(jù)庫(kù)操作最基本單元,邏輯上一組操作,要么都成功。如果有一個(gè)操作失敗。則事務(wù)操作都失?。ɑ貪L(Rollback))。 事務(wù)的四個(gè)特性(ACID): 1. 原子性(Atomicity) 一個(gè)事務(wù)(Transaction)中的所有操作,要么全部完成,要么全部不完成,不會(huì)結(jié)束在中間

    2024年02月08日
    瀏覽(25)
  • Spring Boot 事務(wù)和事務(wù)傳播機(jī)制

    Spring Boot 事務(wù)和事務(wù)傳播機(jī)制

    事務(wù)定義 將一組操作封裝成一個(gè)執(zhí)行單元 (封裝到一起),這一組的執(zhí)行具備原子性, 那么就要么全部成功,要么全部失敗. 為什么要用事務(wù)? 比如轉(zhuǎn)賬分為兩個(gè)操作: 第一步操作:A 賬戶-100 元。 第二步操作:B賬戶 +100 元。 如果沒有事務(wù),第一步執(zhí)行成功了,第二步執(zhí)行失敗了,

    2024年02月11日
    瀏覽(25)
  • SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis

    SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis

    ??wei_shuo的個(gè)人主頁(yè) ??wei_shuo的學(xué)習(xí)社區(qū) ??Hello World ! Spring Data是一個(gè)用于簡(jiǎn)化數(shù)據(jù)庫(kù)訪問和操作的開源框架,為開發(fā)人員提供了一種通用的方式來處理不同類型的數(shù)據(jù)存儲(chǔ),例如關(guān)系型數(shù)據(jù)庫(kù)(如MySQL、PostgreSQL、Oracle)和非關(guān)系型數(shù)據(jù)庫(kù)(如MongoDB、Cassandra、Redis)等。

    2024年02月12日
    瀏覽(47)
  • spring boot整合第三方微信開發(fā)工具 weixin-java-miniapp 實(shí)現(xiàn)小程序微信登錄

    spring boot整合第三方微信開發(fā)工具 weixin-java-miniapp 實(shí)現(xiàn)小程序微信登錄

    有時(shí)候項(xiàng)目需要用到微信登錄或獲取用戶的手機(jī)號(hào)碼,weixin-java-miniapp是一個(gè)好用的第三方工具,不用我們自己寫httpcline調(diào)用。 導(dǎo)入jar包 添加一個(gè)resource.properties文件,寫上小程序的appid和secret 添加兩個(gè)配置文件 WxMaProperties.java WxMaConfiguration.java 如何使用 小程序給微信發(fā)送消息

    2024年02月16日
    瀏覽(94)
  • 【Spring教程31】SSM框架整合實(shí)戰(zhàn):從零開始學(xué)習(xí)SSM整合配置,如何編寫Mybatis SpringMVC JDBC Spring配置類

    【Spring教程31】SSM框架整合實(shí)戰(zhàn):從零開始學(xué)習(xí)SSM整合配置,如何編寫Mybatis SpringMVC JDBC Spring配置類

    歡迎大家回到《Java教程之Spring30天快速入門》,本教程所有示例均基于Maven實(shí)現(xiàn),如果您對(duì)Maven還很陌生,請(qǐng)移步本人的博文《如何在windows11下安裝Maven并配置以及 IDEA配置Maven環(huán)境》,本文的上一篇為《Rest風(fēng)格簡(jiǎn)介與RESTful入門》 前面我們已經(jīng)把Mybatis、Spring和SpringMVC三個(gè)框架

    2024年02月04日
    瀏覽(24)
  • 2023 最新版IntelliJ IDEA 2023.1創(chuàng)建Java Web前(vue3)后端(spring-boot3)分離 項(xiàng)目詳細(xì)步驟(圖文詳解)

    2023 最新版IntelliJ IDEA 2023.1創(chuàng)建Java Web前(vue3)后端(spring-boot3)分離 項(xiàng)目詳細(xì)步驟(圖文詳解)

    2023 最新版IntelliJ IDEA 2023.1創(chuàng)建Java Web 項(xiàng)目詳細(xì)步驟(圖文詳解) 本篇使用當(dāng)前Java Web開發(fā)主流的spring-boot3框架來創(chuàng)建一個(gè)Java前后端分離的項(xiàng)目,前端使用的也是目前前端主流的vue3進(jìn)行一個(gè)簡(jiǎn)單的項(xiàng)目搭建,讓你距離Java全棧開發(fā)更近一步 ?????。 使用版本: “17.0.1”

    2024年02月12日
    瀏覽(34)
  • spring boot es | spring boot 整合elasticsearch | spring boot整合多數(shù)據(jù)源es

    spring boot es | spring boot 整合elasticsearch | spring boot整合多數(shù)據(jù)源es

    目錄 Spring Boot與ES版本對(duì)應(yīng) Maven依賴 配置類 使用方式 @Test中注入方式 @Component中注入方式 查詢文檔 實(shí)體類 通過ElasticsearchRestTemplate查詢 通過JPA查詢 保存文檔 參考鏈接 項(xiàng)目組件版本: Spring Boot:2.2.13.RELEASE Elasticsearch:6.8.0 JDK:1.8.0_66 Tips: 主要看第3列和第5列,根據(jù)ES版本選擇

    2023年04月18日
    瀏覽(28)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包