个人博客——创建后端项目

用idea创建一个springboot项目

在这里插入图片描述
在这里插入图片描述
初始项目结构如下
在这里插入图片描述

添加依赖和配置

  1. 在pom.xml文件的dependencies中添加需要的依赖
    <dependencies>
<!--web项目起步依赖-->
        <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 的驱动依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>

        <!--MyBatis 整合 SpringBoot 的起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!-- 热部署依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>
  1. 在application.properties文件,写上数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai
spring.datasource.username=用户名
spring.datasource.password=密码

配置逆向工程文件

  1. 在com.example.myblog目录下添加controller、mapper、pojo、service四个包
    在这里插入图片描述
  2. 在resources文件下添加generatorMapper.xml文件
    在这里插入图片描述
    文件配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration

        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="E:\xx\xxx\mysql-connector-java-8.0.19.jar"/>

    <!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
    <context id="tables" targetRuntime="MyBatis3">
        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 配置数据库连接信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai"
                        userId="用户名"
                        password="密码">

            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>


        <!-- 生成 pojo类,targetPackage 指定 pojo类的包名, targetProject 指定
        生成的pojo放在 IDEA 的哪个工程下面-->
        <javaModelGenerator targetPackage="com.example.放置pojo包的包名.pojo"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>


        <!-- 生成 MyBatisMapper.xml 文件,targetPackage 指定 mapper.xml 文件的
        包名, targetProject 指定生成的 mapper.xml 放在 IDEA 的哪个工程下面 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>


        <!-- 生成 MyBatisMapper 接口类文件,targetPackage 指定 Mapper 接口类的包
        名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.放置mapper包的包名.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>


        <!-- 数据库表名及对应的 Java pojo类名,有几个表写几个table标签 -->
        <table tableName="comment" domainObjectName="Comment"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="message" domainObjectName="Message"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="passage" domainObjectName="Passage"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="role" domainObjectName="Role"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="sort" domainObjectName="Sort"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
               
       <table tableName="user" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

    </context>
</generatorConfiguration>
  1. 在pom.xml文件的plugins中添加逆向工程用到是插件
<plugins>
            <!--逆向工程代码自动生成插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generatorMapper.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
  1. 完成以上配置后点击右侧maven->Plugins->mybatis-generator,然后左键双击mybatis-generator:generate
    在这里插入图片描述
    等待一段时间后,完成项目文件的自动生成
    自动生成的mapper、pojo、mapper.xml文件
    上图为自动生成的mapper、pojo、mapper.xml文件

从数据库中读取数据并展示

  1. 在service文件夹下添加RoleService接口和impl文件夹(存放接口实现类的包)
    在这里插入图片描述
    2.在RoleService中添加要实现的方法
package com.example.myblog.service;

import com.example.myblog.pojo.Role;

public interface RoleService {
    Role selectByPrimaryKey(Integer roleId);
}

3.在impl/RoleServiceImpl中实现该接口RoleService中的方法

package com.example.myblog.service.impl;

import com.example.myblog.mapper.RoleMapper;
import com.example.myblog.pojo.Role;
import com.example.myblog.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role selectByPrimaryKey(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }
}

4.在controller文件夹中添加RoleController
在这里插入图片描述
RoleController的内容如下

package com.example.myblog.controller;


import com.example.myblog.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/role")
    @ResponseBody
    public String getRole(){
        System.out.println("555");
        return roleService.selectByPrimaryKey(2).toString();

    }
}


在MyblogApplication添加注解@MapperScan其中的值指定mapper包路径,这样就会扫描该包下的所有xxMapper.java并将其添加到spring容器中。

package com.example.myblog;

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

@SpringBootApplication
@MapperScan("com.example.myblog.mapper")
public class MyblogApplication {

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

}

6.因为xxxMapper和xxxMapper.xml不在同级目录下,在application.properties添加mybatis.mapper-locations指定映射文件的路径

mybatis.mapper-locations=classpath:mapper/*.xml

在这里插入图片描述

运行项目,访问http://localhost:8080/role
在这里插入图片描述
数据读取成功
至此,后端项目的大体框架搭建完毕,接下来写博客需要用到的接口

参考文章

https://blog.csdn.net/qq_51476492/article/details/121212244

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值