快速创建一个springboot项目

﹥>﹥吖頭↗ 提交于 2020-02-07 02:44:29

创建一个springboot项目

一.创建项目

二.配置文件

application.properties

#端口号,不配置,默认为8080
server.port=8080

#数据源必填项
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/yydemo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.username = root
spring.datasource.password = 123456

#加载Mybatis配置文件
mybatis.mapper-locations = classpath:mapper/*Mapper.xml

三.代码

1.结构

2.代码

domain层:Student
package com.yy.springbootdemo.domain;

import lombok.Data;

@Data
public class Student {
    private Long id;
    private String username;
    private Integer age;
}

dao层:StudentDao

package com.yy.springbootdemo.dao;

import com.yy.springbootdemo.domain.Student;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface StudentDao {
    List<Student> selectAll();
}

service层:StudentService

package com.yy.springbootdemo.service;

import com.yy.springbootdemo.domain.Student;
import java.util.List;

public interface StudentService {
    List<Student> selectAll();
}
service.impl层:StudentServiceImpl
package com.yy.springbootdemo.service.impl;

import com.yy.springbootdemo.dao.StudentDao;
import com.yy.springbootdemo.domain.Student;
import com.yy.springbootdemo.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;

    @Override
    public List<Student> selectAll() {
        List<Student> list = studentDao.selectAll();
        return list;
    }
}
controller层:StudentController
package com.yy.springbootdemo.controller;

import com.yy.springbootdemo.domain.Student;
import com.yy.springbootdemo.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @RequestMapping("selectAll")
    public List<Student> selectAll(){
        List<Student> list = studentService.selectAll();
        return list;
    }
}
控制:SpringBootApplication
package com.yy.springbootdemo;

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(basePackages = {"com.yy.springbootdemo.dao"})
@ComponentScan(basePackages = {"com.yy.springbootdemo.domain","com.yy.springbootdemo.dao","com.yy.springbootdemo.service","com.yy.springbootdemo.controller"})
public class SpringBootDemoApplication {

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

}

mapper:StudentMapper.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.yy.springbootdemo.dao.StudentDao">

<select id="selectAll"  resultType="com.yy.springbootdemo.domain.Student">
        select * from student
    </select>

</mapper>

四.数据库

五:调取

http://localhost:8080/student/selectAll

返回结果:

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!