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

Spring boot 整合各種功能

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

1. 回顧

1. springboot框架。簡(jiǎn)化spring項(xiàng)目的初始化搭建和配置的。
2. springboot的配置文件種類: (1)properties (2)yml ===application
3. java讀取springboot配置文件中的內(nèi)容。
4. springboot注冊(cè)web組件。
5. springboot包掃描的原理---自定義包掃描的路徑

2. 正文

1.springboot自動(dòng)裝配原理
2.springboot整合數(shù)據(jù)源--連接數(shù)據(jù)庫(kù)
3.springboot整合mybatis框架。
4.springboot整合pageHelper分頁(yè)插件
5.springboot整合定時(shí)器。
6.springboot整合swagger接口文檔
7.springboot整合mp[mybatis-plus]

3.springboot自動(dòng)裝配原理

我們?cè)谑褂胹pringboot時(shí),并沒(méi)有像之前項(xiàng)目加載我們的前端控制DispatcherServlet,也沒(méi)有寫編碼過(guò)濾器。但是springboot可以完成請(qǐng)求以及編碼的設(shè)置。

原理: 主啟動(dòng)類上的@SpringBootApplication注解上,而該注解是一個(gè)復(fù)合組件,而在復(fù)合注解中存在@EnableAutoConfiguration, 這個(gè)@EnableAutoConfiguration注解也是一個(gè)復(fù)合注解,包含@Import---而@Import注解導(dǎo)入一個(gè)AutoConfigurationImportSelector 【自動(dòng)配置選擇器】,在該類中存在一個(gè)方法getAutoConfigurationEntry --作用:得到自動(dòng)配置類的實(shí)體。而這些自動(dòng)配置類會(huì)完成相應(yīng)的自動(dòng)裝配。

4. springboot整合數(shù)據(jù)源--連接數(shù)據(jù)庫(kù)

連接我們的數(shù)據(jù)庫(kù)----druid數(shù)據(jù)源----默認(rèn)的數(shù)據(jù)源

4.1 設(shè)置默認(rèn)數(shù)據(jù)源

(1)引入相關(guān)依賴

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
   </dependency>

springboot自動(dòng)配置會(huì)加載jdbc的自動(dòng)配置類---讀取springboot配置文件中數(shù)據(jù)源的信息,從而完成數(shù)據(jù)源的自動(dòng)配置。

(2)application配置文件中配置數(shù)據(jù)源.

#配置數(shù)據(jù)源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/aaa?serverTimezone=Asia/Shanghai

(3)測(cè)試

@SpringBootTest
class Qy168Springboot03ApplicationTests {

    @Autowired
    private DataSource dataSource;//springboot幫你完成數(shù)據(jù)源的自動(dòng)裝配
    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getConnection());
    }

}

4.2 設(shè)置Druid數(shù)據(jù)源


        <!--druid數(shù)據(jù)源的依賴-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>

修改配置文件

spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.url=jdbc:mysql://localhost:3306/aaa?serverTimezone=Asia/Shanghai
#初始化的連接對(duì)象的個(gè)數(shù)
spring.datasource.druid.initial-size=5
#最多的連接數(shù)
spring.datasource.druid.max-active=10

測(cè)試:

5. springboot整合mybatis框架

ssm===>spring框架需要配置SqlSesssionFactoryBean對(duì)象,還有配置DAO接口的代理實(shí)現(xiàn)類。

springboot會(huì)自動(dòng)配置SqlSesssionFactoryBean對(duì)象,必須引入starter依賴

(1) 依賴

        <!--mybatis和springboot整合的依賴-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

(2)創(chuàng)建實(shí)體類

@Data
public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Double balance;
}

(3)dao接口

public interface UserDao {
    public List<User> findAll();
}

(4)mapper映射文件

<?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">
<!--namespace必須和dao接口的名稱一模一樣-->
<mapper namespace="com.aaa.qy168springboot03.dao.UserDao">

    <select id="findAll" resultType="com.aaa.qy168springboot03.entity.User">
        select * from t_user 
    </select>
</mapper>

(5)修改配置文件的內(nèi)容

(6)在主啟動(dòng)類上dao接口的掃描配置

(7)測(cè)試

6. springboot整合pageHelper分頁(yè)插件

        <!--pageHelper的依賴-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.5</version>
        </dependency>

測(cè)試:

 @Test
    public void testFindAll(){
        PageHelper.startPage(1,3);//攔截器: select * from t_user
        List<User> all = userDao.findAll();
        //把查詢的結(jié)果封裝到PageInfo類中
        PageInfo<User> pageInfo=new PageInfo<>(all);
        System.out.println("總條數(shù):"+pageInfo.getTotal());
        System.out.println("總頁(yè)數(shù):"+pageInfo.getPages());
        System.out.println("當(dāng)前頁(yè)的記錄:"+pageInfo.getList());
    }

7. 綜合案例

springboot+mybatis+druid+pageHelper: CRUD

(1)創(chuàng)建springboot工程并引入相關(guān)的依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

(2)修改配置文件

#修改端口號(hào)
server.port=8888
#數(shù)據(jù)源
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.url=jdbc:mysql://localhost:3306/qy168?serverTimezone=Asia/Shanghai
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=10

#mybatis映射文件的路徑
mybatis.mapper-locations=classpath:/mapper/*.xml

(3)實(shí)體類

tbl_dept

@Data
public class Dept {
    private Integer did;
    private String dname;
    private String loc;
}

tbl_emp

@Data
public class Emp {
    private Integer id;
    private String name;
    private double salary;
    private Date birthday;
    private String headImg;
    private Integer deptId;

    //查詢員工時(shí)需要攜帶該員工對(duì)應(yīng)的部門信息.
    private Dept dept;
}

(4)dao接口和映射文件

public interface EmpDao {
    
    //增加
    public int insert(Emp emp);
    
    //刪除
    public int deleteById(Integer id);
    
    //修改
    public int update(Emp emp);
    
    //根據(jù)id查詢員工信息
    public Emp selectById(Integer id);
    
    //查詢所有
    public List<Emp> selectAll();
}

<?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">
<!--namespace必須和dao接口的名稱一模一樣-->
<mapper namespace="com.ykq.dao.EmpDao">

    <!--返回你添加時(shí)數(shù)據(jù)庫(kù)生成的主鍵值-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into tbl_emp values(null,#{name},#{salary},#{birthday},#{deptId},#{headImg})
    </insert>
    <update id="update">
        update tbl_emp set name=#{name},salary=#{salary},birthday=#{birthday},dept_id=#{deptId},headImg=#{headImg}
        where id=#{id}
    </update>
    <delete id="deleteById">
        delete from tbl_emp where id=#{id}
    </delete>
    <!--sql片段-->
    <sql id="empSql">
        id,name,salary,birthday,dept_id deptId,headImg
    </sql>
    <select id="selectById" resultType="com.ykq.entity.Emp">
         select
          <include refid="empSql"/>
          from tbl_emp where id=#{id}
    </select>

    <!--查詢所有員工攜帶部門信息-->
    <resultMap id="baseEmpMapper" type="com.ykq.entity.Emp" autoMapping="true">
          <id property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <association property="dept" javaType="com.ykq.entity.Dept" autoMapping="true">
             <id property="did" column="did"/>
             <result property="dname" column="d_name"/>
        </association>
    </resultMap>
    <select id="selectAll" resultMap="baseEmpMapper">
        select * from tbl_emp e join tbl_dept d on e.dept_id=d.did
    </select>


</mapper>

(5)service業(yè)務(wù)層

public interface EmpService {
    
    public Result deleteById(int id);
    
    public Result update(Emp emp);
    
    public Result insert(Emp emp);
    
    public Result findById(Integer id);
    
    public Result findAll(Integer current,Integer pageSize);
}
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Override
    @Transactional
    public Result deleteById(int id) {
        int row = empDao.deleteById(id);
        return row>0?new Result(200,"刪除成功",null):new Result(500,"刪除失敗",null);
    }

    @Override
    public Result update(Emp emp) {
        int row = empDao.update(emp);
        return row>0?new Result(200,"刪除成功",null):new Result(500,"刪除失敗",null);
    }

    @Override
    public Result insert(Emp emp) {
        int row = empDao.insert(emp);
        return row>0?new Result(200,"刪除成功",emp):new Result(500,"刪除失敗",null);
    }

    @Override
    public Result findById(Integer id) {
        Emp emp = empDao.selectById(id);
        return new Result(200,"查詢成功",emp);
    }

    @Override
    public Result findAll(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        List<Emp> emps = empDao.selectAll();
        PageInfo<Emp> pageInfo=new PageInfo<>(emps);
        return new Result(200,"查詢成功",pageInfo);
    }
}

(6)controller接口層

package com.zjw.controller;

import com.zjw.entity.Emp;
import com.zjw.service.EmpService;
import com.zjw.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @DeleteMapping
    public Result deleteById(Integer id){
        return empService.deleteById(id);
    }

    @PostMapping
    public Result insert(@RequestBody Emp emp){
        return empService.insert(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        return empService.update(emp);
    }

    @GetMapping
    public Result getById(Integer id){
        return empService.findById(id);
    }


    @GetMapping("/getAll")
    public Result getAll(Integer current,Integer pageSize){
        return empService.findAll(current, pageSize);
    }
}

8. springboot整合定時(shí)器。

可以在規(guī)定的時(shí)間內(nèi)執(zhí)行相應(yīng)的代碼。

比如: OSS文件上傳---OSS的文件冗余的文件。----OSS的浪費(fèi)。---->指定的時(shí)間自動(dòng)刪除。[夜間刪除]

比如: 下單--->30分鐘未支付---取消訂單。

(1)引入定時(shí)器的依賴--


        <!--引入定時(shí)器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

(2)編寫定義的業(yè)務(wù)代碼

@Component
public class My {

    @Autowired
    private EmpDao empDao;
    //定時(shí)任務(wù)執(zhí)行的代碼
    @Scheduled(cron = "0 0 2 * * ?")
    public void show(){
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
        //可以寫自己的寫完代碼
        empDao.delete();
    }
}

(3)開啟定時(shí)的注解驅(qū)動(dòng)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-665944.html

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

本文來(lái)自互聯(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)文章

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包