搭建springboot框架

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

做一个项目之前首先需要搭建起来框架,在这里总结一下搭建springboot框架和mybatis整合连接mysql数据库的案例。

首先新建一个项目
在这里插入图片描述
选择jdk1.8,然后下一步
在这里插入图片描述
写groupId,下一步
在这里插入图片描述
选择需要的内容(这里需要勾选的三个)这里勾选的内容等完成项目创建之后再pom文件会自动添加对应的依赖并且下载。
在这里插入图片描述
在这里插入图片描述
然后点击下一步,填写项目名,忽略,最后点击完成。
在这里插入图片描述
在resources文件夹下的application.properties文件中填写配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
mybatis.mapper-locations=classpath\:mapper/*.xml
server.port=8080

如果文件是application.yml,则填写格式如下
在这里插入图片描述
以上配置为最基本的配置,仅总结了连接数据库和mybatis的mapper文件位置。
在java下面新建几个文件夹
在这里插入图片描述
启动类添加注解

@SpringBootApplication
@MapperScan("com.qianlong.dao")//扫描Mapper接口的包
@ComponentScan(value = "com.qianlong")//开启包扫描
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

现在框架就已经搭好了,开始写内容(就写一个简单的查操作)
先写一个实体,实体类的各个属性与数据库中的字段相对应

public class Stu {
    private Integer id;
    private String name;
    private Integer age;
    private String address;
    private Date time;
//set和get方法省略
}

再写mapper接口,我比较推从使用注解写sql语句,所以这里是注解

public interface SelectMapper {
    @Select(value = "select * from stu")
    List<Stu> getList();
}

service层忽略。直接来controller层

@RestController
@RequestMapping("/demo")
public class SelectController {
    @Autowired
    private SelectService selectService;
    @RequestMapping("/getList")
    public List<Stu> getList(){
        List<Stu> list = selectService.getList();
        System.out.println(list);
        return list;
    }
}

然后在浏览器输入localhost:8080/demo/getList
在这里插入图片描述
查询成功!

猜你喜欢

转载自blog.csdn.net/weixin_44001965/article/details/92587206
今日推荐