spring+springmvc+mybatis基础框架整合

一,项目的结构(我用springboot创建项目,好处是jar包不用自己管理了,由maven仓库管理方便很多)
在这里插入图片描述
二,pom.xml文件(核心配置文件):

    <dependencies>
        <!--jar包文件的依赖   如果需要某些功能,则使用依赖进行导入
            按需导入.
            大部分都进了定义,但是个别的需要手动导入(需要自定版本)		-->
        <dependency>
            <groupId>org.springframework.boot</groupId>

            <!--
                spring-boot-starter: springboot启动项
                SpringBoot官网针对于该框架已经完成了"配置" 用户只需要简单的配置
                既可以使用该功能.
            -->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入lombok包-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        
        <!--引入数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!--springBoot数据库连接  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        
        <!--spring整合mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
    </dependencies>
    <!--maven通过插件对maven版本的springboot进行管理
        体现: 项目打包/发布/测试等相关操作.
    -->
    <build>
        <plugins>
            <!--springboot专门为springboot的工程开发了一款项目管理插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

三 , 核心配置文件(application.yml)文件:

spring:
  datasource:
    #如果使用高版本的数据库则添加cj
    url: jdbc:mysql:///user?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: xxxx
    #密码不能以0开头  如果0开头 则使用"012345"
    password: xxxx

#MP 只做增强不做改变.
mybatis-plus:
  #定义别名包
  type-aliases-package: com.jc.pojo
  #加载mapper的映射文件
  mapper-locations: classpath:/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

四:pojo中的User类

package com.jc.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;


@Data    //默认生成get/set/toString.....
@Accessors(chain = true)  //开启链式加载
@TableName("demo_user")   //mybatis-plus的注解:实现对象与表的映射
public class User extends SecurityManager{
    //标识主键,主键自增
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}

五,mapper中的UserMapper

package com.jc.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.jc.pojo.User;

/**
 * UserMapper中继承了BaseMapper,这个类就不用再定义接口,继承后直接就可以用BaseMapper中的方法了
 */
public interface UserMapper extends BaseMapper<User> {
}

六 , service包中的UserService

package com.jc.service;

import com.jc.pojo.User;

import java.util.List;

public interface UserService {
    /**
     * @return
     * 查询数据库表中所有的数据展示
     */
    List<User> getAll();
}

七 , service包中的UserServiceI

package com.jc.service;

import com.jc.mapper.UserMapper;
import com.jc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

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

八 , UserController

package com.jc.controller;

import com.jc.pojo.User;
import com.jc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/user/")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("a")
    public List<User> getAll() {
        List<User> all = userService.getAll();
        return all;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值