Java MyBatis 插入单个/ 使用 foreach传递 List插入多个, 批量插入 亲测

Java MyBatis 插入单个/ 使用 foreach传递 List插入多个, 批量插入

  • 1. 相关数据表
  • 2. User.java
  • 3. UserMapper.xml
  • 4. DAO(Data Access Object)数据访问接口 UserDao.java和实现类 UserDaoImpl.java
  • 5. UserController.java(入口)

1. 相关数据表


CREATE DATABASE `test` DEFAULT CHARSET utf8;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `memo` varchar(2000) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

2. User.java


import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 8755803182642361875L;

    private Long id;

    private String name;

    private String memo;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }
}

3. UserMapper.xml






	
	
		INSERT INTO
		`user`
		(`name`,`memo`)
		VALUES
		(#{name},#{memo})
	

	
	
		INSERT INTO
		`user`
		(`name`,`memo`)
		VALUES
		
		(
			#{item.name},
			#{item.memo}
		)
		
	



4. DAO(Data Access Object)数据访问接口 UserDao.java和实现类 UserDaoImpl.java


import com.test.web3.entity.User;
import java.util.List;

public interface UserDao {
    void create(final User user);
    void create(final List users);
}

import com.test.web3.entity.User;
import com.test.web3.dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private SqlSession sqlSession;

    static final String MAPPER = "com.test.web3.UserMapper.";

    @Override
    public void create(final User user) {
        sqlSession.insert(MAPPER + "create", user);
    }

    @Override
    public void create(final List users) {
        sqlSession.insert(MAPPER + "create2", users);
    }

}

5. UserController.java(入口)


import com.test.web3.dao.OrderDao;
import com.test.web3.dao.UserDao;
import com.test.web3.entity.Order;
import com.test.web3.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/createUser1", method = RequestMethod.GET)
    public @ResponseBody Map createUser1() {
        /**
         * 创建单个用户
         * */
        final User user = new User();
        user.setName("测试用户1");
        user.setMemo("1号用户的笔记");
        userDao.create(user);
        System.out.println("插入后自增编号: " + user.getId());

        Map result = new HashMap<>();
        result.put("message", "创建成功!");
        return result;
    }

    @RequestMapping(value = "/createUser2", method = RequestMethod.GET)
    public @ResponseBody Map createUser2() {
        /**
         * 创建多个用户
         * */
        final List users = new ArrayList<>();

        final User user2 = new User();
        user2.setName("测试用户2");
        user2.setMemo("2号用户的笔记");
        users.add(user2);

        final User user3 = new User();
        user3.setName("测试用户3");
        user3.setMemo("3号用户的笔记");
        users.add(user3);

        userDao.create(users);

        Map result = new HashMap<>();
        result.put("message", "创建成功!");
        return result;
    }
    
}

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!

你可能感兴趣的:(Spring,Boot,MyBatis,Java)