怎么创建项目就不说了,可以参考:https://www.cnblogs.com/braveym/p/11321559.html

打开本地的mysql数据库,创建表

CREATE TABLE `users` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
`age` int() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在项目里面新建Users.java类

package com.gongspringmvcmybatis.pojo;

public class Users {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }

创建 mapper 接口以及映射配置文件

接口

package com.gongspringmvcmybatis.mapper;

import com.gongspringmvcmybatis.pojo.Users;

public interface UsersMapper {

    void insertUser(Users users);
}

映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gongspringmvcmybatis.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(name,age) values(#{name},#{age})
</insert>
</mapper>

创建业务层

package com.gongspringmvcmybatis.service;

import com.gongspringmvcmybatis.pojo.Users;

public interface UsersService {

    void addUser(Users users);
}

package com.gongspringmvcmybatis.service.impl;

import com.gongspringmvcmybatis.mapper.UsersMapper;
import com.gongspringmvcmybatis.pojo.Users;
import com.gongspringmvcmybatis.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Service
@Transactional
public class UsersServiceImpl implements UsersService { @Autowired
private UsersMapper usersMapper; @Override
public void addUser(Users users) {
this.usersMapper.insertUser(users);
}
}

创建 Controller

package com.gongspringmvcmybatis.controller;

import com.gongspringmvcmybatis.pojo.Users;
import com.gongspringmvcmybatis.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/users")
public class UsersController { @Autowired
private UsersService usersService; /**
* 页面跳转
*/
@ResponseBody
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
} /**
* 添加用户
*/
@RequestMapping("/addUser")
public String addUser(Users users){
this.usersService.addUser(users);
return "ok";
}
}

新建input.html页面文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body> <form th:action="@{/users/addUser}" method="post">
用户姓名:<input type="text" name="name"/><br/>
用户年龄:<input type="text" name="age"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>

创建ok.html页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
操作成功!!!
</body>
</html>

创建启动类

package com.gongspringmvcmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.gongspringmvcmybatis.mapper") //@MapperScan 用户扫描MyBatis的Mapper接口
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

运行启动类

浏览器打印出这个结果,具体原因我也不清楚了

SpringBoot整合MyBatis完成添加用户的更多相关文章

  1. 001 SringBoot基础知识及SpringBoot整合Mybatis

    1.原有Spring优缺点分析 (1)优点 Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品.无需开发重量级的Enterprise J ...

  2. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  3. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例

    1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...

  4. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  5. 三、SpringBoot 整合mybatis 多数据源以及分库分表

    前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程.但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现.我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接 ...

  6. springBoot 整合 mybatis 项目实战

    二.springBoot 整合 mybatis 项目实战   前言 上一篇文章开始了我们的springboot序篇,我们配置了mysql数据库,但是我们sql语句直接写在controller中并且使用 ...

  7. springBoot整合Mybatis,Junit

    笔记源码:https://gitee.com/ytfs-dtx/SpringBoot 整合Mybatis SpringBoot的版本:2.2.5.RELEASE Mybatis版本:mybatis-s ...

  8. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  9. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

随机推荐

  1. access denied

    背景: 想要使用nginx转发 实现一个输出PHPinfo的页面, 比如: 访问  aaa.com/phpinfo  浏览器显示phpinfo的信息, 因为有的时候需要查看phpinfo, 所以想单独 ...

  2. filter(expr|obj|ele|fn)筛选出与指定表达式匹配的元素集合。

    filter(expr|obj|ele|fn) 概述 筛选出与指定表达式匹配的元素集合. 这个方法用于缩小匹配的范围.用逗号分隔多个表达式 参数 exprStringV1.0 字符串值,包含供匹配当前 ...

  3. 2018CCPC桂林站JStone Game

    题目描述 Alice and Bob are always playing game! The game today is about taking out stone from the stone ...

  4. 用python来抓取“煎蛋网”上面的美女图片,尺度很大哦!哈哈

    所用Python环境为:python 3.3.2   用到的库为:urllib.request    re 废话不多说,先上代码: import urllib.request import re #获 ...

  5. 如何检测域名是否被微信屏蔽 微信域名检测接口API是如何实现

    微信域名检测技术的主要用户是微信域名防封,大家知道拼多多这种网站,靠诱导分享方式在微信里面摇身一变已经估值160亿美元,身价仅次于京东了 ,这是何等的速度,简直是惊为天人,but 如果你想玩微信病毒营 ...

  6. 【Android】【踩坑日记】解决Error:SSL peer shut down incorrectly

    前提条件 http://services.gradle.org/distributions/ 复制到浏览浏览器能打开 下一步 打开Android studiogradle version 配置文件进行 ...

  7. iOS (APP)进程间8中常用通信方式总结

    1 URL Scheme 2 Keychain 3 UIPasteboard 4 UIDocumentInteractionController 5 local socket 6 AirDrop 7 ...

  8. Nginx之HTTP过滤模块

    1. HTTP 过滤模块 ngx_http_not_modified_module 仅对 HTTP 头部做处理.在返回 200 成功时,根据请求中 If-Modified-Since 或者 If-Un ...

  9. Twisted框架学习

    Twisted是用Python实现的基于事件驱动的网络引擎框架,是python中一个强大的异步IO库.理解twisted的一个前提是弄清楚twisted中几个核心的概念: reactor, Proto ...

  10. k8s部署03-----常用运维命令

    kubectl常用命令 kubectl get nodes #查看集群中有多少个node kubectl describe node <node_name> #查看某个node的详细信息 ...