Commit 9a431a75 authored by cuixiaowei's avatar cuixiaowei

崔小伟作业

parent 5893d87a
/*
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 80013
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 80013
File Encoding : 65001
Date: 2019-12-09 16:34:49
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for city
-- ----------------------------
DROP TABLE IF EXISTS `entity`;
CREATE TABLE `entity` (
`id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(255) DEFAULT NULL,
`context` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.demo;
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.mybatis;
package com.example.demo.mybatis;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
*
* </p>
*
* @author shyane
* @date 2019/12/9
*/
@Service
public class BatchService {
private SqlSession sqlSession;
public BatchService(SqlSessionFactory sqlSessionFactory) {
sqlSession = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
}
public int batchInsertCity(List<Entity> entities) {
EntityMapper entityMapper = sqlSession.getMapper(EntityMapper.class);
//批量保存执行前时间
long start = System.currentTimeMillis();
for (Entity entity : entities) {
entity.setId(IdUtil.simpleUUID());
entity.setName("ces");
entityMapper.insert(entity);
}
long end = System.currentTimeMillis();
long time = end - start;
//批量保存执行后的时间
Console.log("批量执行时长" + time);
return cities.size();
}
}
package com.example.demo.mybatis;
package com.example.demo.mybatis;
import java.util.Map;
/**
* <p>
*
* </p>
*
* @author shyane
* @date 2019/12/9
*/
public class Entity {
private String id;
private String name;
private Map<String,Object> context;
public String getId() {
return id;
}
public Entity setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public Entity setName(String name) {
this.name = name;
return this;
}
public Map<String, Object> getContext() {
return context;
}
public Entity setContext(Map<String, Object> context) {
this.context = context;
return this;
}
@Override
public String toString() {
return "Entity{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", context=" + context +
'}';
}
}
package com.example.demo.mybatis;
package com.example.demo.mybatis;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* <p>
*
* </p>
*
* @author shyane
* @date 2019/12/9
*/
@Mapper
public interface EntityMapper {
@Insert("INSERT INTO entity (id, name, context) VALUES(#{id}, #{name}, #{context})")
void insert(Entity entity);
@Select("SELECT id, name, context FROM entity WHERE id = #{id}")
City findById(String id);
}
package com.example.demo.mybatis;
package com.example.demo.mybatis;
import cn.hutool.json.JSONUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
/**
* <p>
* map类型转换器
* </p>
*
* @author shyane
* @date 2019/12/9
*/
public class MapTypeHandler extends BaseTypeHandler<Map> {
public MapTypeHandler() {
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Map map, JdbcType jdbcType) throws SQLException {
ps.setString(i,JSONUtil.toJsonStr(map));
}
@Override
public Map getNullableResult(ResultSet rs, String columnName) throws SQLException {
String result = rs.getString(columnName);
return rs.wasNull() ? null : JSONUtil.parseObj(result);
}
@Override
public Map getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String result = rs.getString(columnIndex);
return rs.wasNull() ? null : JSONUtil.parseObj(result);
}
@Override
public Map getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String result = cs.getString(columnIndex);
return cs.wasNull() ? null : JSONUtil.parseObj(result);
}
}
package com.example.demo.mybatis;
package com.example.demo.mybatis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <p>
*
* </p>
*
* @author shyane
* @date 2019/12/9
*/
@Configuration
public class MyBatisConfig {
@Bean
MapTypeHandler mapTypeHandler() {
return new MapTypeHandler();
}
}
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT-8
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis.type-aliases-package=com.example.demo.mybatis
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30
package com.example.demo;
package com.example.demo;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.mybatis.BatchService;
import com.example.demo.mybatis.Entity;
import com.example.demo.mybatis.EntityMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class DemoApplicationTests {
@Resource
private EntityMapper entityMapper;
@Autowired
private BatchService batchService;
// 测试类型转换
@Test
void contextLoads() {
Entity entity = new Entity();
Map<String, Object> context = new HashMap<>();
context.put("111", "111");
context.put("222", "222");
context.put("333", "333");
context.put("444", "444");
entity.setId(IdUtil.simpleUUID())
.setName(RandomUtil.randomString(4))
.setContext(context);
entityMapper.insert(entity);
}
@Test
void select() {
Entity id = entityMapper.findById("de7b193a8d6aa6aw73w46a5vb6");
Console.log(id);
Console.log(JSONUtil.toJsonStr(id));
}
// 测试批量处
@Test
void batch() {
List<Entity> entities = new ArrayList<Entity>();
for (int i = 0; i < 10000; i++) {
Entity entity = new Entity();
entity.setName(RandomUtil.randomString("ces测试", 4));
entities.add(entity);
}
batchService.batchInsertEntity(entities);
this.noBath(entities);
}
void noBath(List<Entity> entities) {
//批量保存执行前时间
long start = System.currentTimeMillis();
for (Entity entity : entities) {
entity.setId(IdUtil.simpleUUID());
entityMapper.insert(entity);
}
long end = System.currentTimeMillis();
long time2 = end - start;
//批量保存执行后的时间
Console.log("执行时长" + time2);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment