ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IDEA mybatis-plus搭建

2021-01-20 02:04:03  阅读:267  来源: 互联网

标签:p4 boot IDEA plus org mybatis import com example


mybatis-plus搭建

新建一个spring-boot项目

如果慢的话,可以更改custom为https://start.aliyun.com/

勾选这些选项

配置文件

pom.xml添加mybatis-plus依赖

切记别写错了!!!

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
</dependency>

pom.xml

<?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>
    <groupId>com.example</groupId>
    <artifactId>p3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>p3</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <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>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.p3.P3Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

目录架构如下

![](file://C:\Users\asus\AppData\Roaming\marktext\images\2021-01-20-01-04-38-image.png)

先创建student实体类

student.java

package com.example.p4.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Table;
import lombok.Data;

/**
 * @TableName student
 */
@Table(name="student")
@Data
public class Student implements Serializable {
    /**
     * 
     *
     * @mbg.generated Wed Jan 20 00:44:50 CST 2021
     */
    @Column(name = "SId")
    private String sid;

    /**
     * 
     *
     * @mbg.generated Wed Jan 20 00:44:50 CST 2021
     */
    @Column(name = "Sname")
    private String sname;

    /**
     * 
     *
     * @mbg.generated Wed Jan 20 00:44:50 CST 2021
     */
    @Column(name = "Sage")
    private Date sage;

    /**
     * 
     *
     * @mbg.generated Wed Jan 20 00:44:50 CST 2021
     */
    @Column(name = "Ssex")
    private String ssex;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table student
     *
     * @mbg.generated Wed Jan 20 00:44:50 CST 2021
     */
    private static final long serialVersionUID = 1L;
}

dao层

studentDao.java

package com.example.p4.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.p4.entity.Student;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Entity com.example.p4.entity.Student
 */
@Mapper
public interface StudentDao extends BaseMapper<Student> {

}

service层接口

StudentService.java

package com.example.p4.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.p4.entity.Student;

public interface StudentService extends IService<Student> {
}

实现层

StudentServiceImpl

package com.example.p4.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.p4.dao.StudentDao;
import com.example.p4.entity.Student;
import com.example.p4.service.StudentService;
import org.springframework.stereotype.Service;

@Service("StudentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
}

mapper.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.example.p4.dao.StudentDao">
  <resultMap id="BaseResultMap" type="com.example.p4.entity.Student">
    <!--@mbg.generated-->
    <result column="SId" jdbcType="VARCHAR" property="sid" />
    <result column="Sname" jdbcType="VARCHAR" property="sname" />
    <result column="Sage" jdbcType="TIMESTAMP" property="sage" />
    <result column="Ssex" jdbcType="VARCHAR" property="ssex" />
  </resultMap>

</mapper>

控制层

StudentController.java

package com.example.p4.controller;

import com.example.p4.entity.Student;
import com.example.p4.service.StudentService;
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 StudentController {

    @Autowired
    StudentService studentService;
    @RequestMapping("/test")
    public List<Student> test(){
        return studentService.list();
    }
}

标签:p4,boot,IDEA,plus,org,mybatis,import,com,example
来源: https://www.cnblogs.com/zx-l/p/14300851.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有