美文网首页
SpringBoot笔记2:Rest页面及数据库

SpringBoot笔记2:Rest页面及数据库

作者: 二枚目 | 来源:发表于2018-09-14 17:38 被阅读67次

    SpringBoot Rest风格接口

    使用Spring Tool Suite工具选择Web Starter开发Rest风格Web项目
    使用静态页面html,在resources建立一个static目录和index.htm静态文件,访问地址http://localhost:8080/index.html
    如果想做动态效果,即请求服务器,访问后台应用程序,然后带着返回数据再转向到页面。SpringBoot可以使用Thymeleaf来做动态页面。

    在pom.xml  中添加Thymeleaf组件:

    <dependency>
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency>  

    Controller控制器若要返回页面,则不能使用RestController,@RestController注解相当于@ResponseBody + @Controller合在一起的作用,如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp或者html页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return里的内容。如果需要返回到指定页面,则需要用@Controller配合视图解析器InternalResourceViewResolver才行。如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

    @Controller 
    public class TemplatesController {  
        @GetMapping("/templates") 
        String test(HttpServletRequest request) {
            // 向页面转参数 key和value
            request.setAttribute("key", "hello world"); 
            return "index"; 
        }   
    }   

    return "index"表示默认跳转到 templates/index.html  动态页面,templates目录为spring boot默认配置的动态页面路径。这里注意Springboot中关于 static 和 templates 两个默认文件夹的区别: 默认情况下, 网页存放于static目录下, 默认的"/"指向的是~/resouces/static/index.html,如果引入了thymeleaf, 则默认指向的地址为~/resouces/templates/index.html。在引入thymeleaf后, 如果仍需要访问~/static/index.html, 则可以使用重定向 , return"redirect:/index.html"。

    index.html页面代码:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Index</title>
    </head>
    <body>
        <span th:text="${key}"></span>
    </body>
    </html>

    访问http://localhost:8080/templates:


    SpringBoot properties参数配置

    SpringBoot默认的配置文件是 application.properties,也可以自己定义一个或多个配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 src\main\resources 、src\main\resources\config 。application.properties默认为空,其实是SpringBoot都用默认的配置帮我们配好了,所以直接新建一个项目可以不用写任何配置,需要改动配置时写 application.properties 配置文件即可。

    SpringBoot JdbcTemplate访问数据库

    先在pom.xml添加 jdbc模块和 mysql的依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    然后在application.properties中设置数据库的参数,springboot可以根据url可以识别并自动加载mysql驱动,自动创建数据库实例,自动实现连接池等等。

    spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456

    新建一个Dao,用@Repository 注释类,@Autowired 自动装配一个JdbcTemplate。写一个方法
    public void test() throws Exception {    jdbcTemplate.update("insert into t_user values(15, ?, ?)", "aaa", "bbb");    }
    这里直接在控制层,也就是上文写好的TemplatesController 中直接调用Dao的test方法,注意在controller层不管调用 service层还是 dao都要通过注入(@Autowired)的方式引入资源,不然会报错空指针异常。运行Application 访问http://localhost:8080/templates 触发TemplatesController的test方法调用Dao的test方法,查看数据库,新增一条记录,测试成功。

    相关文章

      网友评论

          本文标题:SpringBoot笔记2:Rest页面及数据库

          本文链接:https://www.haomeiwen.com/subject/vkwogftx.html