idea创建spring boot项目(整合ssm)

spring boot项目(整合ssm框架

  • 创建一个spring boot项目
    • 添加mysql依赖
    • 项目结构
    • 测试

创建一个spring boot项目

idea创建spring boot项目(整合ssm)_第1张图片
idea创建spring boot项目(整合ssm)_第2张图片

添加mysql依赖


            mysql
            mysql-connector-java
            runtime

添加配置文件

server.port=8080

spring.datasource.url=jdbc:mysql://localhost:3306/test?noAccessToProcedureBodies=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.userName=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

项目结构

目录
idea创建spring boot项目(整合ssm)_第3张图片

dao层方法

package com.example.demo.dao;

import com.example.demo.dao.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface Dao {

    @Select("SELECT * FROM  user where  name= #{name} ")
    User findUser(User user);
}

service层方法

package com.example.demo.service;

import com.example.demo.dao.Dao;
import com.example.demo.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    Dao dao;
    @Override
    public User selectUser(User user) {
        return dao.findUser(user);
    }
}

contoller层方法

package com.example.demo.controller;

import com.example.demo.dao.entity.User;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
public class DemoController {
    @Autowired
    DemoService service;

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public User test(@RequestBody User user){
        user = service.selectUser(user);
        return user;
    }
}

实体类

package com.example.demo.dao.entity;

public class User {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

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

数据库设计
idea创建spring boot项目(整合ssm)_第4张图片
idea创建spring boot项目(整合ssm)_第5张图片

测试

通过postman测试接口
idea创建spring boot项目(整合ssm)_第6张图片
返回值
idea创建spring boot项目(整合ssm)_第7张图片

你可能感兴趣的:(idea创建spring boot项目(整合ssm))