三大框架整合SSM--2

目录

整体思路

在这里插入图片描述

Controller层接受请求和反馈请求

Service层执行业务逻辑

Mapper层/Dao层,根据业务逻辑操作数据库

创建格式如下图:

 

1.创建启动类

@SpringBootApplication--标志这是一个启动类

@MapperScan("****")--开启包扫描,让Spring容器来管理对象,引号中的数值代表路径

package com.jt;

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

@SpringBootApplication
@MapperScan("com.jt.mapper")
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class, args);
    }
}

2.创建pojo包,并在改包下新建实体类--User

@Data--Lomok自动生成set().get().hashcode().eqlus()方法

@Accessors(chain=true)--开启链式编程

@TableName("demo_user")--mybatis-plus中,单表自动映射(ORM)的表名

@TableId(type = IdType.AUTO)--确定表里的主键约束为ID

package com.jt.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

@Data
@Accessors(chain = true)
@TableName("demo_user")
public class User implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}

3.在resources文件下新建application.yml文件

#端口配置
server:
  port: 8090

#配置数据源
spring:
  datasource:
    #如果使用高版本驱动 则添加cj
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

#Spring整合Mybatis
mybatis-plus:
  #定义别名包
  type-aliases-package: com.jt.pojo
  #导入映射文件
  mapper-locations: classpath:/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true
#当程序执行sql时,把plus中的sql语言打印到控制台日志中,方便后续查看sql语言是否符合业务需要
logging:
  level:
    com.jt.mapper: debug

4.在resources文件下新建mappers包,创建新建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.jt.mapper.UserMapper">

</mapper>

5.创建mapper包,新建UserMapper接口

package com.jt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;

public interface UserMapper extends BaseMapper <User>{

}

特别注意:在继承BaseMapper类时,一定要添加泛型<T>,不然程序启动时一定报错!!!

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jt.mapper.UserMapper.selectList] with root cause 

大致意思就是,在自动映射时,找不到类型。

6.创建controller包,新建UserContrloller类

@RestController--接受前端请求的Contrloller类
@Autowired--DI依赖注入
@RequestMapping("/findAll")--需要反馈的请求的服务地址
package com.jt.controller;

import com.jt.pojo.User;
import com.jt.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("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
}

7.创建service包,新建UserService接口和UserServiceImpl类

UserService接口:

package com.jt.service;

import com.jt.pojo.User;

import java.util.List;

public interface UserService {
     List<User> findAll();
}

UserServiceImpl实现类

package com.jt.service;


import com.jt.mapper.UserMapper;
import com.jt.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> findAll() {
        return userMapper.selectList(null);
    }
}

        8.测试结果如下:

 

GIt仓库源码地址


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值