org.springframework.boot
spring-boot-starter-parent
2.4.3
UTF-8
UTF-8
11
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-aop
com.alibaba
druid-spring-boot-starter
1.2.6
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
org.springframework.boot
spring-boot-starter-thymeleaf
2.5.4
org.projectlombok
lombok
1.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表数据全部打印出来
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)