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

SpringBoot学习13之springboot-mybatis整合

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

SpringBoot学习13之springboot-mybatis整合

第一步:在pom.xml加入mybatis的jar包和数据库连接方面的jar包


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.pp
    thyleaf
    0.0.1-SNAPSHOT
    thyleaf
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
            1.1.9
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


第二步:在application.properties配置数据库相关的内容和mybaties相关的内容
#配置端口号
server.port=8080

#配置thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML

#配置数据库
spring.datasource.name=springboottest
spring.datesource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboottest?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
#配置mybatis相关的东西
mybatis.mapper-locations=classpath:mapper
    @RequestMapping("/postform")
    public String postform(Student student){
        System.out.println(student.toString());
        return "redirect:/th";
    }
}
  • 这里重点关注注解@RequestMapping("/th")的方法。
  • 使用注解@Autowired对全部变量private StudentDao dao注解,这样调用的时候就不需要new。
  • 在方法里面我们新建了一个Student对象,然后给该对象赋值,最后利用map.addAttribute(“student”,student)将该对象保存起来。
  • 后面使用前面定义的全局变量dao,调用dao.getlist()查询student所有的数据,返回了一个数组对象getlist,在使用map.addAttribute(“studentlist”,getlist)然后将getlist添加起来。studenglist算是它的别名。
  • 使用return "test"将页面返回到test.html
第九步:在resource/templates下写test.html



    
    Title



讲解一:thymeleaf


姓名 年龄 生日

讲解二:thymeleaf


讲解三:springboot-mybatis整合


姓名 年龄 生日
  • 上面已经用一级标题对它进行了划分
  • 讲解一:thymeleaf在SpringBoot学习11之thymeleaf有详细讲解
  • 讲解二:thymeleaf在SpringBoot学习12之thymeleaf2有详细讲解
  • 讲解三:springboot-mybatis整合在这里进行分析
    1. 定义stu遍历循环studentlist,然后在后面依次输出,这里的studentlist就是后台map.addAttribute(“studentlist”,getlist)将getlist的变量传送到studentlist。
    2.的使用就是遍历stu的name。
    3.的使用就是遍历stu的age。
    4.对返回的stu.birthday进行格式化输出。
第十步:点击运行按钮,然后在浏览器中输入127.0.0.1:8080/th查看结果

第十步:在数据库中查看数据

项目结构图如下


源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/SpringBoot/thyleaf

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

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

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