idea创建springboot+mybatis项目

9 篇文章 0 订阅

1.新建项目

file==》new==》project
在这里插入图片描述
弹出界面,点击next就行
在这里插入图片描述
然后一般修改项目名称和地址
在这里插入图片描述

目前添加这几个基本的依赖
在这里插入图片描述
在这里插入图片描述
修改maven仓库
可以用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

或者

在这里插入图片描述
配置application.yml
在这里插入图片描述

#配置启动服务
server:
  port: 8080 #访问端口,自定义,写啥都行
  servlet:
    context-path: /mybatis1 #访问路径,可以省略不写


#配置数据库
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/education?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
    #education是数据库名字,根据自己的去改变
    username: root
    password: root
    #账号密码也是一样的
    driver-class-name: com.mysql.cj.jdbc.Driver


#配置mybatis映射文件
mybatis:
  mapper-locations: classpath:/mappers/*.xml

创建数据库

CREATE TABLE user (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(64) DEFAULT ‘’ COMMENT ‘账号’,
password VARCHAR(64) DEFAULT ‘’ COMMENT ‘密码’,
PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

INSERT INTO USER(username,PASSWORD) VALUES (‘admin’,‘123456’);

项目基本目录文件
在这里插入图片描述
实体类User

package com.example.mybatis1.entity;

public class User {
    private Integer id;

    private String username;

    private Integer password;


    public Integer getId() {
        return id;
    }

    public void setUserId(Integer id) {
        this.id = id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getPassword() {
        return password;
    }

    public void setPassword(Integer password) {
        this.password = password;
    }
}

映射文件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.mybatis1.mapper.UserMapper">

    <select id="getUserById" parameterType="Integer" resultType="com.example.mybatis1.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>

</mapper>

接收的,接口文件UserMapper

package com.example.mybatis1.mapper;

import com.example.mybatis1.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {

    User getUserById(Integer id);
}

service接口层UserService

package com.example.mybatis1.service;

import com.example.mybatis1.entity.User;

public interface  UserService {
    public User getUserById(Integer id);
}

impl实现UserServiceImpl

package com.example.mybatis1.service.impl;

import com.example.mybatis1.entity.User;
import com.example.mybatis1.mapper.UserMapper;
import com.example.mybatis1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }
}

controller层UserController

package com.example.mybatis1.controller;

import com.example.mybatis1.entity.User;
import com.example.mybatis1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findId/{id}")
    public User getUser(@PathVariable("id") Integer id){
        return userService.getUserById(id);
    }

}

启动类
一定要加上@MapperScan(“com.example.mybatis1.mapper”)注解

package com.example.mybatis1;

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

@SpringBootApplication
@MapperScan("com.example.mybatis1.mapper")//扫描mapper下面的包。mapper文件就不需要加注解了
public class Mybatis1Application {

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

}

然后启动项目
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值