一、配置

进入IDEA后选择 File-->Setting-->Plugins

选择Marketplace,输入Database,点击Insall进行下载

如果一直转圈,请借鉴IDEA 离线安装插件_程序员柒七的博客-CSDN博客_idea离线安装插件

下载后配置Database

1.先配置对应数据库的包 

2.配置时区:在"Advanced"标签页"VM options"增加:-Duser.timezone=PRC

 3.添加数据库

其中Url:

Mysql 8  url为:其中test为数据库名

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

Mysql 5 url为:其中test为数据库名

jdbc:mysql://localhost:3306/test

4.配置成功效果

 二、使用

1.安装Mybaisx插件

选择Marketplace,输入Mybatisx,点击Insall进行下载

2.选择要生成的数据库表 

 3.填写下面信息

4.自动生成结果

5.UserService集成IService报错因为缺少依赖 通过AIt+Enter 进行添加依赖 (一般很少出现)

6.测试

(1)配置pom和yml文件

        pom:

<dependencies>
        <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <!--jdbc依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

 <build>
<!--    配置自动扫描yml、properties、xml-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

        yml:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/hms?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
  thymeleaf:
    cache: false  #设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
#修改端口号默认是8080
server:
  port: 8888
#mybatis-plus相关配置
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml #扫描mapper下的所有xml文件
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-aliases-package: com.zwl.entity   #扫描实体类包、配置别名

        controller:

import com.zwl.entity.User;
import com.zwl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    public List<User> users(){
        List<User> userList=userService.selectList();
        return userList;
    }
}

UserService和UserServiceImpl:

import com.zwl.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

public interface UserService extends IService<User> {

    List<User> selectList();
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwl.entity.User;
import com.zwl.service.UserService;
import com.zwl.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> selectList() {
        return userMapper.selectList();
    }
}

        UserMapper:

import com.zwl.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

public interface UserMapper extends BaseMapper<User> {

    List<User> selectList();

}

        UserMapper.xml

<?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">
<mapper namespace="com.zwl.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.zwl.entity.User">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="password" column="password" jdbcType="VARCHAR"/>
            <result property="phone" column="phone" jdbcType="VARCHAR"/>
            <result property="state" column="state" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,name,password,
        phone,state
    </sql>
    <select id="selectList" resultType="com.zwl.entity.User">
        select * from user;
    </select>
</mapper>

运行结果出现所有信息证明配置成功!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐