老师,我想通过list方式insert进数据表,但是遇到一个问题
-------------------------classes.xml------------------------------ <?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="schools"> <insert id="classInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" keyColumn="id"> insert into classes(classno,name,major) values <foreach collection="list" item="item" separator=","> (#{item.classno},#{item.name},#{item.major}) </foreach> </insert> </mapper> -------------------------Testor------------------------------------- public class Testor { @Test public void testClassesInsert(){ SqlSession session = null; try{ session = MyBatisUtils.openSession(); List<Classes> list = new ArrayList(); list.add(new Classes("Class001","一班","计算机")); list.add(new Classes("Class002","二班","计算机")); list.add(new Classes("Class003","三班","会计")); list.add(new Classes("Class004","四班","会计")); for (Classes classes : list) { System.out.println(classes.toString()); } int num = session.insert("schools.classInsert",list); // session.commit(); }catch(Exception e){ e.printStackTrace(); if(session!=null) { session.rollback(); } }finally { MyBatisUtils.closeSession(session); } } }
结果却是重复的数据,请问是哪里出了问题呢。
3
收起
正在回答 回答被采纳积分+1
2回答
好帮手慕小尤
2023-06-21 10:42:37
同学你好,1、静态属性是类属性,通过类名就可以调用,不用再new一个对象去调用属性。静态属性是会随着类的加载而加载,并且只加载一次,全局唯一。在
2、同学代码中每一次实例化,都会将属性值进行修改,并会将之前的属性值覆盖。从而导致出现一样的值,建议去除静态修饰。如下所示:
public class Classes { private Integer id; private String classno; private String name; private String major; public Classes() { } public Classes(String classno,String name,String major){ this.classno = classno; this.name = name; this.major = major; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getClassno() { return classno; } public void setClassno(String classno) { this.classno = classno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } @Override public String toString() { return id+" "+classno+" "+name+" "+major; } }
祝学习愉快!
会java泰酷辣
2023-06-21 10:39:18
这是classes类的代码
package com.imooc.mybatis.entity; public class Classes { private static Integer id; private static String classno; private static String name; private static String major; public Classes() { } public Classes(String classno,String name,String major){ this.classno = classno; this.name = name; this.major = major; } public static Integer getId() { return id; } public static void setId(Integer id) { Classes.id = id; } public static String getClassno() { return classno; } public static void setClassno(String classno) { Classes.classno = classno; } public static String getName() { return name; } public static void setName(String name) { Classes.name = name; } public static String getMajor() { return major; } public static void setMajor(String major) { Classes.major = major; } @Override public String toString() { return id+" "+classno+" "+name+" "+major; } }
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星