创建表的sql(下表是基于Oracle创建的)
CREATE TABLE "SYSTEM"."STUDENT" (
"ID" NUMBER(10, 0),
"NAME" VARCHAR2(20 BYTE),
"ADDRES" CLOB,
PRIMARY KEY ( "ID" )
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL
DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT )
TABLESPACE "SYSTEM"
ENABLE
)
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL
DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT )
TABLESPACE "SYSTEM"
LOB ( "ADDRES" ) STORE AS BASICFILE (
TABLESPACE "SYSTEM"
ENABLE STORAGE IN ROW
CHUNK 8192
RETENTION
NOCACHE LOGGING
STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL
DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT )
);
表结构-具体使用什么数据库都行以自己的项目为准
pom
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.0
com.example
demo
0.0.1-SNAPSHOT
mybatis-demo
测试mybaits
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
com.oracle.database.jdbc
ojdbc8
12.2.0.1
application
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=oracle
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
代码
student表操作类
@Service
public class studentDaoTest {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public void inster(){
Connection connection = null;
SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
connection=sqlSession.getConnection();
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO student (id, name, addres) VALUES (?, ?, ?)");
try (PreparedStatement statement = connection.prepareStatement(sql.toString())) {
for (int i = 1; i <= 100; i++) {
statement.setInt(1, i);
statement.setString(2, "Name " + i);
statement.setString(3, "Addres " + i);
statement.addBatch();
}
statement.executeBatch();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
测试类
@SpringBootTest
class MybatisDemoApplicationTests {
@Autowired
private studentDaoTest studentDaoTest;
@Test
void contextLoads() {
studentDaoTest.inster();
}
}
结果
public String testSQlAppendBatch(List
// 拼接sql
private String sqlAppend(List
//设置预处理参数
private void sqlAppendtemp(PreparedStatement preparedStatement,List
package com.example.demo;
import com.example.demo.test.studentDaoTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class MybatisDemoApplicationTests {
@Autowired
private studentDaoTest studentDaoTest;
@Test
void contextLoads() {
List