这是一个小白写的demo :注意Oracle 和Mysql 批量保存的方式不一样 这里Mysql 批量保存的方式对Oracle 不适用
项目结构:
数据库:Student表 需求就是批量加入学生信息:
数据库配置文件和基础配置文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql_demo
jdbc.username=root
jdbc.password=root
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="conf/jdbc.properties"/>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
<typeAliases>
<package name="entity"/>
</typeAliases>
<environments default="mybatis-demo">
<environment id="mybatis-demo">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<databaseIdProvider type="DB_VENDOR">
<property name="Oracle" value="oracle"/>
<property name="MySQL" value="mysql"/>
</databaseIdProvider>
<mappers>
<package name="mapper"/>
</mappers>
</configuration>
学生实体类:
package entity;
import org.apache.ibatis.type.Alias;
@Alias("student")
public class Student {
private Integer sid;
private String sName;
private String Ssex;
private Teacher teacher;
private Integer Sagenum;
private Integer tid;
public Student() {
}
public Student(Integer sid, String sName, String ssex, Integer sagenum, Integer tid) {
this.sid = sid;
this.sName = sName;
Ssex = ssex;
Sagenum = sagenum;
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sName='" + sName + '\'' +
", Ssex='" + Ssex + '\'' +
", teacher=" + teacher +
", Sagenum=" + Sagenum +
", tid=" + tid +
'}';
}
public Student(Integer sid, String sName, String ssex, Teacher teacher, Integer sagenum, Integer tid) {
this.sid = sid;
this.sName = sName;
Ssex = ssex;
this.teacher = teacher;
Sagenum = sagenum;
this.tid = tid;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String ssex) {
Ssex = ssex;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Integer getSagenum() {
return Sagenum;
}
public void setSagenum(Integer sagenum) {
Sagenum = sagenum;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
}
mapper :
接口:
package mapper;
import entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentMapper {
public void addStu(@Param("Stul") List<Student> Stul) ;/*这里用@Param("Stul")给参数List<Student> Stul 起别名 方便后面调用*/
}
对应的xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.StudentMapper">
<insert id="addStu">
INSERT INTO sTUDENT (sID,SNAME,SSEX,SAGENUM,TID)
VALUES
<foreach collection="Stul" item="stu" separator=","><--解释下这里几个标签
foreach 是一个循环语句 作用是遍历集合 这里也就是遍历Stul item 配置的是循环中当前的元素 可以自定义 但是但是要与下面#{}中的元素(stu)一样 separator 是各个元素之间的分隔符-->
(null,#{stu.sName},#{stu.Ssex},#{stu.Sagenum},#{stu.tid}) <--sid是自增主键 所以设为null-->
</foreach>
</insert>
</mapper>
测试类:
import entity.Student;
import mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Main {
@Test
public void add() throws Exception {
SqlSessionFactory sqlSessionFactory = null;
String resource = "conf/mybatis-config.xml";
InputStream inputStream;
SqlSession sqlSession=null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
List<Student> studentList=new ArrayList<>();
studentList.add(new Student(null,"LI1","男",12,12));
studentList.add(new Student(null,"LI2","男",12,12));
studentList.add(new Student(null,"LI3","男",12,12));
studentMapper.addStu(studentList);
sqlSession.commit();//修改数据库的要提交
} catch (IOException e) {
} finally {
sqlSession.close();
}
}
}
数据库保存结果: