SpringBoot連接數(shù)據(jù)庫(kù)SQL語(yǔ)句編寫(注解與配置文件)

原文鏈接:https://www.pianshen.com/article/1425420266/

springboot 注解及xml方式操作數(shù)據(jù)庫(kù)。注解方式簡(jiǎn)便。

首先我們需要進(jìn)行一些配置,讓項(xiàng)目可以連接我們的數(shù)據(jù)庫(kù)(這里我數(shù)據(jù)庫(kù)用的mySql)

image

1、引入jar

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

        <!--mySql的驅(qū)動(dòng)-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <!--阿里巴巴的druid連接池(一定要給版本)-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

2、配置properties文件

#配置數(shù)據(jù)庫(kù)信息
#配置數(shù)據(jù)庫(kù)驅(qū)動(dòng)名稱
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#JDBC連接的URL
spring.datasource.url=jdbc:mysql://localhost:3306/muzi?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
#配置數(shù)據(jù)庫(kù)賬號(hào)及密碼
spring.datasource.username=root
spring.datasource.password=muzi

#數(shù)據(jù)源druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#為我們定義的實(shí)體類文件夾下的實(shí)體類自動(dòng)起別名
mybatis.type-aliases-package=com.muzi.book.Pojo

#開(kāi)啟駝峰命名
mybatis.configuration.map-underscore-to-camel-case=true

3、數(shù)據(jù)庫(kù)建表,以及創(chuàng)建對(duì)象實(shí)體類

image
image

//==============================================================================//

4、接下來(lái)我們先編寫注解的方法

 mapper:
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper    //必須要聲明的,不然系統(tǒng)不知道這個(gè)類是作為底層交互的
public interface UserMapper {
    @Select("insert into muzi_user(name,age,sex) values (#{name},#{age},#{sex})")
    void insertUser(String name,int age ,int sex);
}

Service:

package com.muzi.book.Service;

import com.muzi.book.Pojo.muziUser;

public interface UserService {
    void insertUser(muziUser muziUser);
}

ServiceImpl:

package com.muzi.book.Service.ServiceImpl;

import com.muzi.book.Pojo.muziUser;
import com.muzi.book.Service.UserService;
import com.muzi.book.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service    //必須聲明的,不然系統(tǒng)不知道此為邏輯層
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void insertUser(muziUser muziUser) {
        userMapper.insertUser(muziUser.getName(),muziUser.getAge(),muziUser.getSex());
    }
}

Controller:

package com.muzi.book.Controller;

import com.muzi.book.Pojo.muziUser;
import com.muzi.book.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 跳轉(zhuǎn)到用戶注冊(cè)頁(yè)面
     */
    @RequestMapping("/showUser")
    public String showUser(){
        muziUser muziUser=new muziUser();
        muziUser.setAge(4);
        muziUser.setName("4");
        muziUser.setSex(4);
        userService.insertUser(muziUser);
        return null;
    }
}

我新寫了一個(gè)項(xiàng)目用于測(cè)試:

image

結(jié)果:插入成功

image

//==============================================================================//

//==============================================================================//

//==============================================================================//

接下來(lái)進(jìn)行配置文件的編寫

只需要改動(dòng)mapper即可

1、首先我們修改mapper的注解,將sql注解語(yǔ)句刪除

package com.muzi.book.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    //@Select("insert into muzi_user(name,age,sex) values (#{name},#{age},#{sex})")
    void insertUser(String name,int age ,int sex);
}

2、我們刪除了注解模式就需要引入配置文件模式,在mapper下創(chuàng)建一個(gè)配置文件.xml

請(qǐng)注意parameterType定義的是類的路徑,因?yàn)槲以趐roperties中定義好了mybatis.type-aliases-package所以只需要寫類名就好,系統(tǒng)會(huì)自動(dòng)拼接

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mabatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.xxx.mapper.userMapper">
    <!--添加用戶-->
    <insert id="insertUser" parameterType="user">
        insert into muzi_user(name,age,sex) values (#{name},#{age},#{sex})
    </insert>
</mapper>

3、如果報(bào)錯(cuò)無(wú)法找到xxxx的配置文件,優(yōu)先檢查自己的xml配置的路徑,文件名等是否不一致,如果確定一致請(qǐng)參考:

https://blog.csdn.net/qq_38005982/article/details/90375414

4、數(shù)據(jù)庫(kù)插入成功

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

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

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