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);
    }
}

 

 

 

運行啟動類

 

 

 

 

 

瀏覽器打印出這個結果,具體原因我也不清楚了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM