org.springframework.boot spring-boot-starter-parent2.4.3 UTF-8 UTF-8 11
org.springframework.boot spring-boot-starter-webmysql mysql-connector-javaorg.springframework.boot spring-boot-starter-aopcom.alibaba druid-spring-boot-starter1.2.6 org.mybatis.spring.boot mybatis-spring-boot-starter2.2.0 org.springframework.boot spring-boot-starter-thymeleaf2.5.4 org.projectlombok lombok1.18.20 provided
org.springframework.boot spring-boot-maven-plugin
application.properties
#数据库 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.password=admin spring.datasource.username=root spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
前端thymeleaf页面编写
$Title$
后端编写
controller层
@Controller
public class HelloController {
@Autowired
private ISchoolService schoolService;
@RequestMapping("/hello")
public String hello2(Model model) {
model.addAttribute("schools",schoolService.query());
return "/hello";
}
}
service层
@Service
public class SchoolServiceImpl implements ISchoolService {
@Autowired
private SchoolMapper schoolMapper;
@Override
public List query() {
return schoolMapper.selectAll();
}
}
public interface ISchoolService {
List query();
}
mapper层
@Repository
public interface SchoolMapper {
List selectAll();
}
mapper.xml
实体类
@Getter
@Setter
public class School {
private Long id;
private String name;
}
启动类
@SpringBootApplication
@MapperScan(basePackages = "mapper包所在包名")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
按照以上编写,可以将MySQL中test库里的school表数据全部打印出来



