栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot的helloworld

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot的helloworld


        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表数据全部打印出来

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/865229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号