Java Spring Boot环境搭建

SpringBoot搭建

1.新建

 

 

 

2.配置mybatis生成器

在resources下创建文件generatorconfig.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>
    <!-- 数据库驱动-->
<!--    <classPathEntry  location="mysql-connector-java-5.1.32-bin.jar"/>-->
    <context id="MysqlContext"  targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <commentGenerator>
            <property name="javaFileEncoding" value="UTF-8"/>
            <property name="suppressDate" value="false"/>
            <!--是否去除自动生成的注释 true:是 : false:否  -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库:驱动类,链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/spring?useUnicode=true&amp;characeterEncoding=utf-8&amp;serverTimezone=UTC"
                        userId="root"
                        password="123456">
            <property name="useInformationSchema" value="true"></property>
        </jdbcConnection>
        <!--默认为false,把JDBC DECIMAL和NUMERIC 类型解析为Integer ,为true 时把JDBC DECIMAL和NUMERIC解析为java.math.BigDecimal-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型entity的包名和位置-->
        <javaModelGenerator targetPackage="com.gf.springboot.bean"
                            targetProject="src/main/java">
            <!--是否让schema作为包的后缀-->
            <property name="enableSubPackages" value="false"/>
            <!--从数据库返回的值被清理前后的空格-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成mapper映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.gf.springboot.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->

        <table tableName="spring_account"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">

        </table>



    </context>
</generatorConfiguration>

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gf</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <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.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.15</version>
                    </dependency>
                </dependencies>
            </plugin>




        </plugins>
    </build>

</project>

使用mybatis-generator

https://blog.csdn.net/shiqianxishiqianxi/article/details/109518403

报错1:

Unable to load authentication plugin 'caching_sha2_password'.

解决:

https://blog.csdn.net/w605283073/article/details/88096598

报错2:

xml文件http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd标红解决办法

解决2:

1.先去这个网址下载这个文件,保存到自己指定的目录下面

2.在idea中进行设置

file-setting-schemas and dtds

报错3:

配置都没有错但是生成器不起作用

 [WARNING] Table configuration with catalog null, schema null, and table wc_noticemodelcenter did not resolve to any tables

解决:

xml文件配置里面数据库表明没写

3.点击生成

就会在bean和mapper文件下生成对应的文件

4.写一个小例子,根据id查询

SpringbootApplication文件添加注解扫描

package com.gf.springboot;

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

@SpringBootApplication
@MapperScan("com.gf.springboot.mapper")
public class SpringbootApplication {

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

}

创建service文件夹并创建SpringAccountService文件

package com.gf.springboot.service;

import com.gf.springboot.bean.SpringAccount;
import com.gf.springboot.mapper.SpringAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class SpringAccountService {

    @Autowired
    private SpringAccountMapper springAccountMapper;

    public SpringAccount getById(Integer id){
        return Optional.ofNullable(springAccountMapper.selectByPrimaryKey(id)).orElse(null);
    }
}

创建controller文件夹并创建SpringAccountController文件

package com.gf.springboot.controller;

import com.gf.springboot.bean.SpringAccount;
import com.gf.springboot.service.SpringAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

@Controller
@RequestMapping("/spring")
public class SpringAccountController {
    @Autowired
    private SpringAccountService springAccountService;

    @RequestMapping("/account/{id}")
    @ResponseBody
    public String accountNum(@PathVariable(value = "id") Integer id){
        return Optional.ofNullable(springAccountService.getById(id)).map(SpringAccount::toString).orElse("empty String");

    }

}

在bean文件夹下的SpringAcoount文件中添加toString方法

package com.gf.springboot.bean;

public class SpringAccount {
    private Integer id;

    private Integer accountno;

    private Integer balance;

    private Integer password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAccountno() {
        return accountno;
    }

    public void setAccountno(Integer accountno) {
        this.accountno = accountno;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    public Integer getPassword() {
        return password;
    }

    public void setPassword(Integer password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "SpringAccount{" +
                "id=" + id +
                ", accountno=" + accountno +
                ", balance=" + balance +
                ", password=" + password +
                '}';
    }
}

启动Application,在浏览器输入http://localhost:8080/spring/account/1

成功~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值