SpringBoot整合MyBatis完成添加用户

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

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

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) 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);
    }
}

运行启动类

 

 

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

原文地址:https://www.cnblogs.com/braveym/p/11349409.html