极尽详细springBoot整合mybatis后台项目搭建

springBoot是实现前后台分离的利器,其“约定大于配置”的思想带给我们快速开发的非凡体验,结合网上教程和自己搭建过程,将一个简单的整合分享如下:
按照我的傻瓜式介绍来,几分钟你也会顺利跑起来一个简单的springboot+mybatis的的后台。

第一步搭建项目骨架:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述这是大家常用的三层结构,创建好。后面粘代码。

第二步:配置数据源application.properties

#mybatis
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crm?useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis.typeAliasesPackage=com.example.entity
mybatis.mapper-locations=classpath:mapper/*.xml

第三步:各层代码 我们从controller开始
1、controller
package com.example.controller;

import com.example.entity.User;
import com.example.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    private final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;

    @RequestMapping("/getUser")
    private User getUser() {
        User user = testService.getUser("张三");
        logger.info("查询人员");
        return user;
    }
}

2、service

package com.example.service;

import com.example.entity.User;

public interface TestService {

    User getUser(String name);
}

package com.example.service.impl;

import com.example.dao.TestDao;
import com.example.entity.User;
import com.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestDao testDao;

    @Override
    public User getUser(String name) {
        return testDao.getUser(name);
    }
}

3、dao

package com.example.dao;

import com.example.entity.User;

public interface TestDao {
    User getUser(String name);
}

4、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.example.dao.TestDao">
    <resultMap id="BaseResultMap" type="com.example.entity.User">
        <result column="name" jdbcType="VARCHAR" property="name"></result>
        <result column="age" jdbcType="INTEGER" property="age"></result>
    </resultMap>

    <select id="getUser" parameterType="string" resultMap="BaseResultMap">
        select * from user where name = #{name}
    </select>
</mapper>

4、User类

package com.example.entity;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 3092592238015885727L;

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

5、启动类

package com.example;

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

@SpringBootApplication
@MapperScan("com.example.dao")
public class SpringbootApplication {

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

}

好了,跑去吧

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值