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_97
MybatisTraning
Commits
814d2b41
Commit
814d2b41
authored
Dec 09, 2019
by
zhuleizi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no message
parent
4c1c2af7
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
487 additions
and
0 deletions
+487
-0
.project
朱雷子_Mybatis1/MybatisWork/.project
+24
-0
pom.xml
朱雷子_Mybatis1/MybatisWork/pom.xml
+55
-0
People.java
...src/main/java/com/cesgroup/mybatiswork/entity/People.java
+51
-0
MyTypeHandle.java
...in/java/com/cesgroup/mybatiswork/handle/MyTypeHandle.java
+57
-0
Mymapper.xml
朱雷子_Mybatis1/MybatisWork/src/main/resource/Mymapper.xml
+27
-0
db.properties
朱雷子_Mybatis1/MybatisWork/src/main/resource/db.properties
+6
-0
mybatis-config.xml
...Mybatis1/MybatisWork/src/main/resource/mybatis-config.xml
+31
-0
mybatis.sql
朱雷子_Mybatis1/MybatisWork/src/main/resource/mybatis.sql
+32
-0
AppTest.java
...sWork/src/test/java/com/cesgroup/mybatiswork/AppTest.java
+135
-0
MANIFEST.MF
朱雷子_Mybatis1/MybatisWork/target/classes/META-INF/MANIFEST.MF
+6
-0
pom.properties
...es/META-INF/maven/com.cesgroup/mybatiswork/pom.properties
+8
-0
pom.xml
...t/classes/META-INF/maven/com.cesgroup/mybatiswork/pom.xml
+55
-0
People.class
...rget/classes/com/cesgroup/mybatiswork/entity/People.class
+0
-0
MyTypeHandle.class
...lasses/com/cesgroup/mybatiswork/handle/MyTypeHandle.class
+0
-0
AppTest.class
...arget/test-classes/com/cesgroup/mybatiswork/AppTest.class
+0
-0
No files found.
朱雷子_Mybatis1/MybatisWork/.project
0 → 100644
View file @
814d2b41
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>
MybatisWork
</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>
org.eclipse.jdt.core.javabuilder
</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>
org.eclipse.m2e.core.maven2Builder
</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>
org.eclipse.jdt.core.javanature
</nature>
<nature>
org.eclipse.m2e.core.maven2Nature
</nature>
</natures>
</projectDescription>
朱雷子_Mybatis1/MybatisWork/pom.xml
0 → 100644
View file @
814d2b41
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.cesgroup
</groupId>
<artifactId>
mybatiswork
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
mybatiswork
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
8.0.17
</version>
</dependency>
<dependency>
<groupId>
org.mybatis
</groupId>
<artifactId>
mybatis
</artifactId>
<version>
3.2.8
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.54
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
朱雷子_Mybatis1/MybatisWork/src/main/java/com/cesgroup/mybatiswork/entity/People.java
0 → 100644
View file @
814d2b41
package
com
.
cesgroup
.
mybatiswork
.
entity
;
package
com
.
cesgroup
.
mybatiswork
.
entity
;
import
java.io.Serializable
;
import
com.alibaba.fastjson.JSONObject
;
/**
* @author zlz
*
*/
public
class
People
implements
Serializable
{
/**
*
*/
private
static
final
long
serialVersionUID
=
-
9109516444665323692L
;
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
JSONObject
getInfo
()
{
return
info
;
}
public
void
setInfo
(
JSONObject
info
)
{
this
.
info
=
info
;
}
@Override
public
String
toString
()
{
return
"User [id="
+
id
+
", name="
+
name
+
", info="
+
JSONObject
.
toJSONString
(
info
)
+
"]"
;
}
int
id
;
String
name
;
JSONObject
info
;
}
\ No newline at end of file
朱雷子_Mybatis1/MybatisWork/src/main/java/com/cesgroup/mybatiswork/handle/MyTypeHandle.java
0 → 100644
View file @
814d2b41
package
com
.
cesgroup
.
mybatiswork
.
handle
;
package
com
.
cesgroup
.
mybatiswork
.
handle
;
import
java.sql.CallableStatement
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
org.apache.ibatis.type.BaseTypeHandler
;
import
org.apache.ibatis.type.JdbcType
;
import
com.alibaba.fastjson.JSONObject
;
/**
* @author zlz
*
*/
public
class
MyTypeHandle
extends
BaseTypeHandler
<
JSONObject
>
{
@Override
public
JSONObject
getNullableResult
(
ResultSet
rs
,
String
cloumName
)
throws
SQLException
{
String
infoJsonString
=
rs
.
getString
(
cloumName
);
JSONObject
infObject
=
null
;
try
{
infObject
=
JSONObject
.
parseObject
(
infoJsonString
);
// System.out.println("----json串转json对象成功!----");
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"json串转json对象失败!"
);
;
}
return
infObject
;
}
@Override
public
JSONObject
getNullableResult
(
CallableStatement
arg0
,
int
arg1
)
throws
SQLException
{
return
null
;
}
@Override
public
void
setNonNullParameter
(
PreparedStatement
statement
,
int
i
,
JSONObject
infoObject
,
JdbcType
jdbcType
)
throws
SQLException
{
try
{
String
userInfoString
=
JSONObject
.
toJSONString
(
infoObject
);
statement
.
setString
(
i
,
userInfoString
);
// System.out.println("----对象转化字符串成功!----:"+userInfoString);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"----对象转化字符串失败!----"
);
}
}
@Override
public
JSONObject
getNullableResult
(
ResultSet
arg0
,
int
arg1
)
throws
SQLException
{
return
null
;
}
}
朱雷子_Mybatis1/MybatisWork/src/main/resource/Mymapper.xml
0 → 100644
View file @
814d2b41
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper:根标签,namespace:命名空间,随便写,一般保证命名空间唯一 -->
<mapper
namespace=
"MyMapper"
>
<!-- statement,内容:sql语句。id:唯一标识,随便写,在同一个命名空间下保持唯一
resultType:sql语句查询结果集的封装类型,tb_user即为数据库中的表
-->
<select
id=
"selectUser"
resultType=
"com.cesgroup.mybatiswork.entity.User"
>
select * from mybatis where id = #{id}
</select>
<insert
id=
"insert"
parameterType=
"com.cesgroup.mybatiswork.entity.User"
>
INSERT INTO mybatis (id, name, info)
VALUES(#{id}, #{name}, #{info})
</insert>
<insert
id=
"insertBatch"
>
INSERT INTO mybatis
(id, name, info)
VALUES
<foreach
collection =
"list"
item=
"user"
separator =
","
>
(#{user.id}, #{user.name}, #{user.info})
</foreach >
</insert>
</mapper>
\ No newline at end of file
朱雷子_Mybatis1/MybatisWork/src/main/resource/db.properties
0 → 100644
View file @
814d2b41
driver
=
com.mysql.cj.jdbc.Driver
driver
=
com.mysql.cj.jdbc.Driver
url
=
jdbc:mysql://localhost:3306/test
username
=
root
password
=
123456
\ No newline at end of file
朱雷子_Mybatis1/MybatisWork/src/main/resource/mybatis-config.xml
0 → 100644
View file @
814d2b41
<?xml version="1.0" encoding="UTF-8" ?>
<?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=
"db.properties"
></properties>
<typeHandlers>
<typeHandler
handler=
"com.cesgroup.mybatiswork.handle.MyTypeHandle"
></typeHandler>
</typeHandlers>
<!-- 环境,可以配置多个,default:指定采用哪个环境 -->
<environments
default=
"dev"
>
<!-- id:唯一标识 -->
<environment
id=
"dev"
>
<!-- 事务管理器,JDBC类型的事务管理器 -->
<transactionManager
type=
"JDBC"
/>
<!-- 数据源,池类型的数据源 -->
<dataSource
type=
"POOLED"
>
<property
name=
"driver"
value=
"${driver}"
/>
<!-- 配置了properties,所以可以直接引用 -->
<property
name=
"url"
value=
"${url}"
/>
<property
name=
"username"
value=
"${username}"
/>
<property
name=
"password"
value=
"${password}"
/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper
resource=
"MyMapper.xml"
/>
</mappers>
</configuration>
\ No newline at end of file
朱雷子_Mybatis1/MybatisWork/src/main/resource/mybatis.sql
0 → 100644
View file @
814d2b41
/*
/*
Navicat Premium Data Transfer
Source Server : 本机
Source Server Type : MySQL
Source Server Version : 80015
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80015
File Encoding : 65001
Date: 09/12/2019 10:25:06
*/
SET
NAMES
utf8mb4
;
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for mybatis
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mybatis`
;
CREATE
TABLE
`mybatis`
(
`id`
int
(
32
)
NOT
NULL
,
`name`
varchar
(
255
)
CHARACTER
SET
utf8mb4
COLLATE
utf8mb4_0900_ai_ci
NULL
DEFAULT
NULL
,
`info`
varchar
(
1000
)
CHARACTER
SET
utf8mb4
COLLATE
utf8mb4_0900_ai_ci
NULL
DEFAULT
NULL
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8mb4
COLLATE
=
utf8mb4_0900_ai_ci
ROW_FORMAT
=
Dynamic
;
SET
FOREIGN_KEY_CHECKS
=
1
;
朱雷子_Mybatis1/MybatisWork/src/test/java/com/cesgroup/mybatiswork/AppTest.java
0 → 100644
View file @
814d2b41
package
com
.
cesgroup
.
mybatiswork
;
package
com
.
cesgroup
.
mybatiswork
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.annotation.Resources
;
import
org.apache.ibatis.session.ExecutorType
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder
;
import
org.junit.Before
;
import
org.junit.Test
;
import
com.alibaba.fastjson.JSONObject
;
import
com.cesgroup.mybatiswork.entity.People
;
/**
* @author zlz
*
*/
public
class
AppTest
{
private
SqlSession
sqlSession
=
null
;
private
SqlSessionFactory
sqlSessionFactory
;
@Before
public
void
configMybatis
()
{
// 指定全局配置文件
String
resource
=
"mybatis-config.xml"
;
// 读取配置文件
InputStream
inputStream
;
try
{
inputStream
=
Resources
.
getResourceAsStream
(
resource
);
// 构建sqlSessionFactory
sqlSessionFactory
=
new
SqlSessionFactoryBuilder
().
build
(
inputStream
);
// 获取sqlSession
// sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false);
sqlSession
=
sqlSessionFactory
.
openSession
(
false
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
sqlSession
!=
null
)
{
// sqlSession.close();
}
}
}
/**
*
* 自定义解析器查询测试
*
* json串字段为 info
*/
@Test
public
void
testSelectUser
()
{
People
user
=
sqlSession
.
selectOne
(
"MyMapper.selectUser"
,
2
);
System
.
out
.
println
(
user
);
sqlSession
.
close
();
}
/**
*
* 自定义类型解析器插入测试
*/
@Test
public
void
testInsertJsonObject
()
{
JSONObject
userInfObject
=
new
JSONObject
();
userInfObject
.
put
(
"address"
,
"天地软件园"
);
userInfObject
.
put
(
"telphone"
,
"18623323323"
);
People
user
=
new
People
();
user
.
setId
(
3
);
user
.
setName
(
"张三"
);
user
.
setInfo
(
userInfObject
);
int
a
=
sqlSession
.
insert
(
"MyMapper.insert"
,
user
);
sqlSession
.
commit
();
System
.
out
.
println
(
"影响的行数:"
+
a
);
}
/**
* 批量插入方法一(经测试该方法较快) 通过list方式批量插入 500条 378ms 5000条615ms 10000条897ms
*/
@Test
public
void
testInsertBatch
()
{
long
start
=
System
.
currentTimeMillis
();
List
<
People
>
list
=
new
ArrayList
<>();
for
(
int
i
=
1
;
i
<
10000
;
i
++)
{
JSONObject
userInfObject
=
new
JSONObject
();
userInfObject
.
put
(
"address"
,
"天地软件园"
);
userInfObject
.
put
(
"telphone"
,
"18623323323"
);
People
user
=
new
People
();
user
.
setId
(
i
);
user
.
setName
(
"张三"
+
i
);
user
.
setInfo
(
userInfObject
);
list
.
add
(
user
);
}
int
a
=
sqlSession
.
insert
(
"MyMapper.insertBatch"
,
list
);
sqlSession
.
commit
();
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"---------------"
+
"批量插入耗时一:"
+
(
end
-
start
)
+
"---------------"
);
}
/**
* 批量插入方法二 通过改变executor类型
*
* 500条 约375ms 5000条约912ms 10000条 1527ms
*/
@Test
public
void
testInsertBatchTwo
()
{
sqlSession
=
sqlSessionFactory
.
openSession
(
ExecutorType
.
BATCH
,
false
);
long
start
=
System
.
currentTimeMillis
();
for
(
int
i
=
1
;
i
<
10000
;
i
++)
{
JSONObject
userInfObject
=
new
JSONObject
();
userInfObject
.
put
(
"address"
,
"天地软件园"
);
userInfObject
.
put
(
"telphone"
,
"18623323323"
);
People
user
=
new
People
();
user
.
setId
(
i
);
user
.
setName
(
"张三"
+
i
);
user
.
setInfo
(
userInfObject
);
int
a
=
sqlSession
.
insert
(
"MyMapper.insert"
,
user
);
}
sqlSession
.
commit
();
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"---------------"
+
"批量插入耗时二:"
+
(
end
-
start
)
+
"---------------"
);
}
}
朱雷子_Mybatis1/MybatisWork/target/classes/META-INF/MANIFEST.MF
0 → 100644
View file @
814d2b41
Manifest-Version: 1.0
Manifest-Version: 1.0
Built-By: Sakamoto
Build-Jdk: 1.8.0_211
Created-By: Maven Integration for Eclipse
朱雷子_Mybatis1/MybatisWork/target/classes/META-INF/maven/com.cesgroup/mybatiswork/pom.properties
0 → 100644
View file @
814d2b41
#Generated by Maven Integration for Eclipse
#Generated by Maven Integration for Eclipse
#Mon Dec 09 21:05:59 CST 2019
version
=
0.0.1-SNAPSHOT
groupId
=
com.cesgroup
m2e.projectName
=
MybatisWork
m2e.projectLocation
=
D
\:\\
eclipse-workspace
\\
MybatisWork
artifactId
=
mybatiswork
朱雷子_Mybatis1/MybatisWork/target/classes/META-INF/maven/com.cesgroup/mybatiswork/pom.xml
0 → 100644
View file @
814d2b41
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.cesgroup
</groupId>
<artifactId>
mybatiswork
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
mybatiswork
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
8.0.17
</version>
</dependency>
<dependency>
<groupId>
org.mybatis
</groupId>
<artifactId>
mybatis
</artifactId>
<version>
3.2.8
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.54
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
朱雷子_Mybatis1/MybatisWork/target/classes/com/cesgroup/mybatiswork/entity/People.class
0 → 100644
View file @
814d2b41
File added
朱雷子_Mybatis1/MybatisWork/target/classes/com/cesgroup/mybatiswork/handle/MyTypeHandle.class
0 → 100644
View file @
814d2b41
File added
朱雷子_Mybatis1/MybatisWork/target/test-classes/com/cesgroup/mybatiswork/AppTest.class
0 → 100644
View file @
814d2b41
File added
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