Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
MybatisTraning
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
zero
MybatisTraning
Commits
9a431a75
Commit
9a431a75
authored
Dec 09, 2019
by
cuixiaowei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
崔小伟作业
parent
5893d87a
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
388 additions
and
0 deletions
+388
-0
entity.sql
崔小伟_Mybatis1/entity.sql
+28
-0
pom.xml
崔小伟_Mybatis1/pom.xml
+67
-0
DemoApplication.java
...atis1/src/main/java/com/example/demo/DemoApplication.java
+14
-0
BatchService.java
.../src/main/java/com/example/demo/mybatis/BatchService.java
+45
-0
Entity.java
...batis1/src/main/java/com/example/demo/mybatis/Entity.java
+54
-0
EntityMapper.java
.../src/main/java/com/example/demo/mybatis/EntityMapper.java
+25
-0
MapTypeHandler.java
...rc/main/java/com/example/demo/mybatis/MapTypeHandler.java
+46
-0
MyBatisConfig.java
...src/main/java/com/example/demo/mybatis/MyBatisConfig.java
+21
-0
application.properties
崔小伟_Mybatis1/src/main/resources/application.properties
+9
-0
DemoApplicationTests.java
.../src/test/java/com/example/demo/DemoApplicationTests.java
+79
-0
No files found.
崔小伟_Mybatis1/entity.sql
0 → 100644
View file @
9a431a75
/*
/*
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
;
崔小伟_Mybatis1/pom.xml
0 → 100644
View file @
9a431a75
<?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>
崔小伟_Mybatis1/src/main/java/com/example/demo/DemoApplication.java
0 → 100644
View file @
9a431a75
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
);
}
}
崔小伟_Mybatis1/src/main/java/com/example/demo/mybatis/BatchService.java
0 → 100644
View file @
9a431a75
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
();
}
}
崔小伟_Mybatis1/src/main/java/com/example/demo/mybatis/Entity.java
0 → 100644
View file @
9a431a75
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
+
'}'
;
}
}
崔小伟_Mybatis1/src/main/java/com/example/demo/mybatis/EntityMapper.java
0 → 100644
View file @
9a431a75
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
);
}
崔小伟_Mybatis1/src/main/java/com/example/demo/mybatis/MapTypeHandler.java
0 → 100644
View file @
9a431a75
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
);
}
}
崔小伟_Mybatis1/src/main/java/com/example/demo/mybatis/MyBatisConfig.java
0 → 100644
View file @
9a431a75
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
();
}
}
崔小伟_Mybatis1/src/main/resources/application.properties
0 → 100644
View file @
9a431a75
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
崔小伟_Mybatis1/src/test/java/com/example/demo/DemoApplicationTests.java
0 → 100644
View file @
9a431a75
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment