首页 > 其他分享 >spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——保存操作——int类型

spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——保存操作——int类型

时间:2023-01-03 22:11:32浏览次数:44  
标签:int spring boot example user MyUser org import public

mapper文件:

<?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="org.example.mapper.UserMapper">
    <select id="findAll" resultType="org.example.entity.MyUser">
        select * from users
    </select>

    <insert id="saveUser" parameterType="org.example.entity.MyUser">
        insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

    <insert id="saveUser2" parameterType="org.example.entity.MyUser">
        insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

</mapper>

 

 

 

 

 

 

 

mapper接口:

package org.example.mapper;

import org.example.entity.MyUser;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper
{

    public List<MyUser> findAll();

    void saveUser(MyUser user);

    int saveUser2(MyUser user);

}

 

 

 

 

 

 

 

 

 

 

dao接口:

package org.example.service;


import org.example.entity.MyUser;

import java.util.List;

public interface UserService
{
    List<MyUser> findAll();

    void saveUser(MyUser user);

    int saveUser2(MyUser user);

}

 

 

 

 

 

 

 

 

实现dao接口:

package org.example.service.Impl;


import org.example.entity.MyUser;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    UserMapper userMapper;

    @Override
    public List<MyUser> findAll()
    {
        return userMapper.findAll();
    }

    @Override
    public void saveUser(MyUser user)
    {
        userMapper.saveUser(user);
    }

    public int saveUser2(MyUser user)
    {
        return userMapper.saveUser2(user);
    }
}

 

 

 

 

 

 

 

 

 

 

控制器:

package org.example.controller;

import java.util.List;
import org.example.entity.MyUser;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyUserController
{
    @Autowired
    private UserService userService;

    @GetMapping(value = "/1")
    public List<MyUser> home1()
    {
        // 查询所有网站
        List<MyUser> users = userService.findAll();

        for (MyUser user : users)
        {
            System.out.println(user.getId()+"  "+user.getName()+"  "+user.getAge());
        }

        return users;
    }

    @GetMapping(value = "/2")
    public void home2()
    {
        MyUser user2 = new MyUser();
        user2.setName("TTT");
        user2.setAge(7878);

        userService.saveUser(user2);

    }

    @GetMapping(value = "/3")
    public int home3()
    {
        MyUser user2 = new MyUser();
        user2.setName("RRR");
        user2.setAge(6666);

        int a = userService.saveUser2(user2);

        System.out.println(a);

        return a;

    }

}

 

 

 

 

 

 

 

 

 

 

之前数据如下:

 

 

 

 

 

 

 

 

 

运行后如下:

 

 

 

 

 

 

 

标签:int,spring,boot,example,user,MyUser,org,import,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17023501.html

相关文章