Commit 03b0a19f authored by lifeng's avatar lifeng

作业提交——李峰

parent eee98e4c
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="ProjectDictionaryState">
<dictionary name="dingsea" />
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" native2AsciiForPropertiesFiles="true" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
<option name="TOP_LEVEL_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="INNER_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="METHOD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
</value>
</option>
<option name="FIELD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="IGNORE_DEPRECATED" value="false" />
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
<option name="myAdditionalJavadocTags" value="date" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State />
</expanded-state>
<selected-state>
<State>
<id>Android</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/MybatisTraning.iml" filepath="$PROJECT_DIR$/.idea/MybatisTraning.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?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.cesgroup.homwork</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</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>
执行测试类:`com.cesgroup.homwork.mybatis.Test`
执行测试类:`com.cesgroup.homwork.mybatis.Test`
\ No newline at end of file
/*
/*
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 13:34:49
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for city
-- ----------------------------
DROP TABLE IF EXISTS `city`;
CREATE TABLE `myEntity` (
`id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT 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;
package com.cesgroup.homwork.mybatis;
package com.cesgroup.homwork.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
package com.cesgroup.homwork.mybatis.sample.config;
package com.cesgroup.homwork.mybatis.sample.config;
import com.cesgroup.homwork.mybatis.sample.type.MapTypeHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisConfig {
@Bean
MapTypeHandler mapTypeHandler() {
return new MapTypeHandler();
}
}
\ No newline at end of file
package com.cesgroup.homwork.mybatis.sample.entity;
package com.cesgroup.homwork.mybatis.sample.entity;
import java.util.Map;
public class MyEntity {
private String id;
private Map<String,Object> context;
public String getId() {
return id;
}
public MyEntity setId(String id) {
this.id = id;
return this;
}
public Map<String, Object> getContext() {
return context;
}
public MyEntity setContext(Map<String, Object> context) {
this.context = context;
return this;
}
}
\ No newline at end of file
package com.cesgroup.homwork.mybatis.sample.mapper;
package com.cesgroup.homwork.mybatis.sample.mapper;
import com.cesgroup.homwork.mybatis.sample.entity.MyEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface MyEntityMapper {
@Insert("INSERT INTO myEntity (id,context) VALUES(#{id},#{context})")
void insert(MyEntity myEntity);
@Select("SELECT id,context FROM myEntity WHERE id = #{id}")
MyEntity findById(String id);
}
\ No newline at end of file
package com.cesgroup.homwork.mybatis.sample.service;
package com.cesgroup.homwork.mybatis.sample.service;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import com.cesgroup.homwork.mybatis.sample.entity.MyEntity;
import com.cesgroup.homwork.mybatis.sample.mapper.MyEntityMapper;
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;
@Service
public class BatchService {
private SqlSession sqlSession;
public BatchService(SqlSessionFactory sqlSessionFactory) {
sqlSession = new SqlSessionTemplate(sqlSessionFactory,ExecutorType.BATCH) ;
}
public int batchInsertCity(List<MyEntity> cities){
MyEntityMapper myEntityMapper = sqlSession.getMapper(MyEntityMapper.class);
//批量保存执行前时间
long start=System.currentTimeMillis();
for (MyEntity MyEntity : cities) {
MyEntity.setId(IdUtil.simpleUUID());
myEntityMapper.insert(city);
}
long end= System.currentTimeMillis();
long time2= end-start;
//批量保存执行后的时间
Console.log("批量执行时长"+time2);
return cities.size();
}
}
package com.cesgroup.homwork.mybatis.sample.type;
package com.cesgroup.homwork.mybatis.sample.type;
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;
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);
}
}
\ No newline at end of file
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=1234
mybatis.type-aliases-package=com.cesgroup.homwork.mybatis
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30
package com.cesgroup.homwork.mybatis;
package com.cesgroup.homwork.mybatis;
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.cesgroup.homwork.mybatis.sample.entity.MyEntity;
import com.cesgroup.homwork.mybatis.sample.mapper.MyEntityMapper;
import com.cesgroup.homwork.mybatis.sample.service.BatchService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisApplicationTests {
@Resource
private MyEntityMapper myEntityMapper;
@Autowired
private BatchService batchService;
@Test
void contextLoads() {
MyEntity myEntity = new MyEntity();
Map<String, Object> context = new HashMap<>();
myEntity.put("a","aaa");
myEntity.put("b","bb");
myEntity.put("c","c");
myEntity.put("d","dddddddddd");
myEntity.setId(IdUtil.simpleUUID())
.setName(RandomUtil.randomString(4))
.setContext(context);
myEntityMapper.insert(myEntity);
}
@Test
void select() {
MyEntity byId = myEntityMapper.findById("857b193a846a4246aba221jsf937191");
Console.log(byId);
Console.log(JSONUtil.toJsonStr(byId));
}
@Test
void batch() {
List<MyEntity> myEntityList = new ArrayList<City>();
for (int i = 0; i < 10000; i++) {
MyEntity myEntity = new MyEntity();
myEntity.setName(RandomUtil.randomString("测试测试测试测试测试",4));
myEntityList.add(myEntity);
}
batchService.batchInsertCity(myEntityList);
this.noBath(myEntityList);
}
void noBath(List<myEntityList> myEntityListList){
//批量保存执行前时间
long start=System.currentTimeMillis();
for (MyEntity myEntity : myEntityList) {
myEntity.setId(IdUtil.simpleUUID());
myEntityMapper.insert(myEntity);
}
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