搭建SpringBoot入门

SpringBoot入门

结构在这里插入图片描述

结构介绍

templates:存放动态页面
static:静态页面、资源

application.yml
#配置端口号
server:
  port: 8888

#设置模板引擎后缀名、数据库连接资源、显示sql语句
spring:
  thymeleaf:
    suffix: .html
  datasource:
    url: jdbc:mysql://localhost:3306/fsblog
    username: root
    password: ok
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    show-sql: true

#配置mybatis 扫描mapper包,设置别名
mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.example.bean

bean
package com.example.bean;

import java.util.Date;

public class Tag {
    private Integer id;
    private String name;
    private Date gmt_create;
    private Date gmt_modified;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Date getGmt_create() {
        return gmt_create;
    }

    public void setGmt_create(Date gmt_create) {
        this.gmt_create = gmt_create;
    }

    public Date getGmt_modified() {
        return gmt_modified;
    }

    public void setGmt_modified(Date gmt_modified) {
        this.gmt_modified = gmt_modified;
    }
}

dao
@Mapper
public interface TagMapper {
    List<Tag> getAll();
}
mapper.xml
<mapper namespace="com.example.dao.TagMapper">
    <select id="getAll" resultType="Tag">
        select * from Tag
    </select>
</mapper>
service
public interface TagService {
    List<Tag> getAll();
}
serviceimpl
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    @Override
    public List<Tag> getAll() {
        return tagMapper.getAll();
    }
}
controller
@Controller
public class HelloController {

    @Autowired
    private TagService service;

    @GetMapping("/hello")
    @ResponseBody
    public ModelAndView  index(){
        List<Tag> all = service.getAll();
        for (Tag tag : all) {
            System.out.println(tag.getName());
        }
    }
}
启动类
@SpringBootApplication
@MapperScan("com.example.dao")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

自定义错误

404、500

  • 在templates下面创建一个error文件加文件夹中存放404、500页面

总结

  • 启动类记得写@MapperScan("com.example.dao")
  • application.yml中
mybatis:
	  #先写
      mapper-locations: classpath:mapper/*.xml
      #后写
      type-aliases-package: com.example.bean
  • dao接口中记得写@Mapper
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值