编写mapper接口和对应的mapper.xml文件,注意对应的注解

  1. @Mapper
  2. @Repository
  3. public interface StudentMapper {
  4. void insertStudent(Student student);
  5. }

@Mapper注解标注这个接口是个mapper接口

@Repository

mapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC
  3. "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.wang.mapper.StudentMapper">
  6. <insert id="insertStudent" parameterType="student">
  7. insert into student(sname,age) values (#{sname},#{age})
  8. </insert>
  9. </mapper>

创建pojo,注意需要继承serializable接口

  1. public class Student implements Serializable {
  2. private String sname;
  3. private String sex;
  4. private Integer age;
  5. private Integer sid;
  6. public String getSname() {
  7. return sname;
  8. }
  9.  
  10. public void setSname(String sname) {
  11. this.sname = sname;
  12. }
  13.  
  14. public String getSex() {
  15. return sex;
  16. }
  17.  
  18. public void setSex(String sex) {
  19. this.sex = sex;
  20. }
  21.  
  22. public Integer getAge() {
  23. return age;
  24. }
  25.  
  26. public void setAge(Integer age) {
  27. this.age = age;
  28. }
  29.  
  30. public Integer getSid() {
  31. return sid;
  32. }
  33.  
  34. public void setSid(Integer sid) {
  35. this.sid = sid;
  36. }
  37.  
  38. public Student(String sname, String sex, Integer age, Integer sid) {
  39.  
  40. this.sname = sname;
  41. this.sex = sex;
  42. this.age = age;
  43. this.sid = sid;
  44. }
  45. }

service层实现

service接口

  1. public interface studentService {
  2. void insertstudent(Student student);
  3. }  

对应service的实现类

  1. @Service
  2. @Transactional
  3. public class studentserviceImpl implements studentService {
  4.  
  5. @Autowired
  6. private StudentMapper studentMapper;
  7.  
  8. public void insertstudent(Student student) {
  9. studentMapper.insertStudent(student);

@service将service类注册到spring容器

@transactional

注意为了能够操作数据库,这里传过来一个对应的mapper接口

controller编写

  1. @Controller
  2. @RequestMapping("/users")
  3. public class studentController {
  4. @Autowired
  5. studentserviceImpl studentservice;
  6. /**
  7. * 跳转页面
  8. * @param
  9. */
  10. @RequestMapping("/{page}")
  11. public String showpage(@PathVariable String page){
  12. return page;
  13. }
  14.  
  15. @RequestMapping("/adduser")
  16. public String adduser(Student student){
  17. studentservice.insertstudent(student);
  18. return "ok";
  19. }
  20. }

对应的HTML页面

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>yonghu</title>
  7. </head>
  8. <body>
  9. <form th:action="@{/users/adduser}" method="post"> <!-- 注意IDEA中有时候不会补全< />,自己需要注意并加上/ -->
  10. <input type="text" name="sname" /><br/>
  11. <input type="text" name="age" /><br/>
  12. <input type="submit" value="confirm">
  13. </form>
  14. </body>
  15. </html>

这里需要注意,为了能够使用th:标签,需要在html文件中引入命名空间

  1. <html xmlns:th="http://www.thymeleaf.org">

跳转页面的html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. save successfully
  9. </body>
  10. </html>

对应source目录下application.yml文件的配置内容

  1. spring: 数据源的配置,这儿里出现一个问题,使用阿里巴巴对应的jar包的时候出现时区不一致的问题,需要设置时区
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/student?characterEncoding=utf-8
  5. username: root
  6. password:
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. thymeleaf:
  9. suffix: .html
  10. prefix: classpath:/templates/ thymleaf设置前缀和后缀,这样找页面的时候直接写上HTML文件的名字就行了。
  11. mode: HTML
  12. cache: true
  13. encoding: utf-8
  14. mybatis:
  15. type-aliases-package: com.wang.pojo
  16. mapper-locations: classpath:mapper/*Mapper.xml

mybatis需要设置两个东西,一个别名和一个指定mapper.xml文件位置

对应上面红色问题的解决方法

命令行启动MySQL,执行下面语句设置时区

  1. set global time_zone='+8:00';

最后查看对应时区

  1. show variables like '%time_zone%';

最后启动类的编写(这里特别注意启动mapper扫面,里面放上扫描的包路径)

  1. @SpringBootApplication
  2. @MapperScan("com.wang.mapper.*")
  3. public class App {
  4. public static void main(String[] args) {
  5. SpringApplication.run(App.class,args);
  6. }
  7. }

工程结构

需要注意resources/static下面的内容是可以直接被访问的,以后还是需要注意一些类和方法的命名规则。

springboot和mybatis之thymleaf整合简单插入用户数据的更多相关文章

  1. 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力

    Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...

  2. 学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop

    mybatis+spring的整合: 导入的依赖:1.数据库连接:mysql-connector-java 2.连接池:druid 3.servlet:javax.servlet-api 4.jstl ...

  3. MyBatis和spring整合简单实现

    spring和MyBatis整合: 导入spring和MyBatis的整合jar包,以及其依赖jar包: 导入MyBatis和spring的整合jar包. spring的核心jar包. 定义Mybat ...

  4. Springboot与Mybatis整合

    最近自己用springboot和mybatis做了整合,记录一下: 1.先导入用到的jar包 <dependency> <groupId>org.springframework ...

  5. Springboot与MyBatis简单整合

    之前搭传统的ssm框架,配置文件很多,看了几天文档才把那些xml的逻辑关系搞得七七八八,搭起来也是很麻烦,那时我完全按网上那个demo的版本要求(jdk和tomcat),所以最后是各种问题没成功跑起来 ...

  6. SpringBoot+SpringMVC+MyBatis快速整合搭建

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...

  7. springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件

    整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的 ...

  8. SpringBoot系列——MyBatis整合

    前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...

  9. 30分钟带你了解Springboot与Mybatis整合最佳实践

    前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...

随机推荐

  1. python 3.5 import theano ::hypot error

    # win10 , mingw(nuwen,g++ 6.3), python 3.5 , 描述: import theano 时生成动态的 mod.cpp ,然后编译库的时候报 ::hypot 未定义 ...

  2. zombodb 低级api 操作

    zombodb 低级api 允许直接从zombodb 索引中进行insert.delete 文档,同时保留了mvcc 的特性,但是数据没有存储在 pg 中,但是也带来数据上的风险,我们需要注意进行es ...

  3. 《Linux内核原理与分析》第四次作业

    跟踪分析Linux内核的启动过程 使用实验楼的虚拟机打开shell 使用 gdb 跟踪调试内核 使用 qemu qemu -kernel linux-3.18.6 /arch/x86/boot/baI ...

  4. java 各种循环遍历

    遍历方式选择: 实现了 RandomAccess 接口的 list,优先选择普通 for 循环 ,其次 foreach: 未实现 RandomAccess 接口的 list, 优先选择 iterato ...

  5. php 字符串固定长度,不够补充其他字符串

    <?php $input = '456'; $var= str_pad($input,5,10,STR_PAD_LEFT); w3school手冊:http://www.w3school.com ...

  6. delphi字符串分割

    function GetLeft(sText, sepStr: string): string; var p: Integer; begin p := Pos(sepStr, sText); then ...

  7. SpringBoot项目单元测试

    关于SpringBoot的单元测试,描述一下三种单元测试的方式. 1.约定 单元测试代码写在src/test/java目录下单元测试类命名为*Test,前缀为要测试的类名 2. 使用mock方式单元测 ...

  8. Python-实列

    """题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再 ...

  9. 未预期的符号 `$'{\r'' 附近有语法错误

    ../runcmake: 行 2: $'\r': 未找到命令 ../runcmake: 行 3: 未预期的符号 `$'{\r'' 附近有语法错误 考虑到代码是从windows下一直过来的,脚本可能在格 ...

  10. 分布式计算课程补充笔记 part 2

    ▶ 并行计算八字原则:负载均衡,通信极小 ▶ 并行计算基本形式:主从并行.流水线并行.工作池并行.功能分解.区域分解.递归分治 ▶ MPI 主要理念:进程 (process):无共享存储:显式消息传递 ...