SpringBoot入门
结构![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f15b2b9aedab1e4064dd42004efdcce3.png)
结构介绍
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