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
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
training
MybatisTraning
Commits
360f92b7
Commit
360f92b7
authored
Jan 02, 2020
by
zhangshuhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
# Mybatis2
parent
6403fea2
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
375 additions
and
0 deletions
+375
-0
.gitignore
.idea/.gitignore
+4
-0
Project_Default.xml
.idea/inspectionProfiles/Project_Default.xml
+37
-0
pom.xml
张书毫_Mybatis/pom.xml
+64
-0
User.java
张书毫_Mybatis/src/main/java/cesgroup/mybatis2/entity/User.java
+44
-0
MyTypeHandle.java
.../src/main/java/cesgroup/mybatis2/handle/MyTypeHandle.java
+60
-0
MyMybatisLogInterceptor
...ava/cesgroup/mybatis2/interceptor/MyMybatisLogInterceptor
+99
-0
MyMapper.xml
张书毫_Mybatis/src/main/resource/MyMapper.xml
+12
-0
db.properties
张书毫_Mybatis/src/main/resource/db.properties
+6
-0
mybatis-config.xml
张书毫_Mybatis/src/main/resource/mybatis-config.xml
+36
-0
mybatis.sql
张书毫_Mybatis/src/main/resource/mybatis.sql
+13
-0
No files found.
.idea/.gitignore
0 → 100644
View file @
360f92b7
# Default ignored files
/workspace.xml
\ No newline at end of file
.idea/inspectionProfiles/Project_Default.xml
0 → 100644
View file @
360f92b7
<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
张书毫_Mybatis/pom.xml
0 → 100644
View file @
360f92b7
<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>
qinwen_Mybatis2
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
qinwen_Mybatis2
</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>
<resources>
<resource>
<directory>
src/main/resource
</directory>
<includes>
<include>
*.*
</include>
</includes>
</resource>
</resources>
</build>
</project>
张书毫_Mybatis/src/main/java/cesgroup/mybatis2/entity/User.java
0 → 100644
View file @
360f92b7
package
com
.
cesgroup
.
mybatis2
.
entity
;
package
com
.
cesgroup
.
mybatis2
.
entity
;
import
java.io.Serializable
;
import
com.alibaba.fastjson.JSONObject
;
/**
*
* @author zsh
* @date 2020/01/02
*/
public
class
User
implements
Serializable
{
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
;
}
张书毫_Mybatis/src/main/java/cesgroup/mybatis2/handle/MyTypeHandle.java
0 → 100644
View file @
360f92b7
package
com
.
cesgroup
.
mybatis2
.
handle
;
package
com
.
cesgroup
.
mybatis2
.
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 zsh
* @date 2020/01/02
*/
public
class
MyTypeHandle
extends
BaseTypeHandler
<
JSONObject
>{
/**
* str转json对象
*/
@Override
public
JSONObject
getNullableResult
(
ResultSet
rs
,
String
cloumName
)
throws
SQLException
{
String
infoJsonString
=
rs
.
getString
(
cloumName
);
JSONObject
infObject
=
null
;
try
{
infObject
=
JSONObject
.
parseObject
(
infoJsonString
);
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"将json串转成json对象失败!"
);;
}
return
infObject
;
}
@Override
public
JSONObject
getNullableResult
(
CallableStatement
arg0
,
int
arg1
)
throws
SQLException
{
return
null
;
}
/**
* json对象转str
*/
@Override
public
void
setNonNullParameter
(
PreparedStatement
statement
,
int
i
,
JSONObject
infoObject
,
JdbcType
jdbcType
)
throws
SQLException
{
try
{
String
userInfoString
=
JSONObject
.
toJSONString
(
infoObject
);
statement
.
setString
(
i
,
userInfoString
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"将对象转化为字符串失败!"
);
}
}
@Override
public
JSONObject
getNullableResult
(
ResultSet
arg0
,
int
arg1
)
throws
SQLException
{
return
null
;
}
}
张书毫_Mybatis/src/main/java/cesgroup/mybatis2/interceptor/MyMybatisLogInterceptor
0 → 100644
View file @
360f92b7
package
com
.
cesgroup
.
mybatis2
.
interceptor
;
package
com
.
cesgroup
.
mybatis2
.
interceptor
;
import
com
.
alibaba
.
fastjson
.
JSONObject
;
import
com
.
cesgroup
.
mybatis2
.
handle
.
MyTypeHandle
;
import
org
.
apache
.
ibatis
.
executor
.
statement
.
StatementHandler
;
import
org
.
apache
.
ibatis
.
logging
.
Log
;
import
org
.
apache
.
ibatis
.
logging
.
LogFactory
;
import
org
.
apache
.
ibatis
.
mapping
.
BoundSql
;
import
org
.
apache
.
ibatis
.
mapping
.
ParameterMapping
;
import
org
.
apache
.
ibatis
.
plugin
.*;
import
org
.
apache
.
ibatis
.
session
.
ResultHandler
;
import
java
.
sql
.
Statement
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
Properties
;
/**
*
*
@
author
zsh
*
@
date
2020
/
01
/
02
*/
@
Intercepts
({
@
Signature
(
type
=
StatementHandler
.
class
,
method
=
"query"
,
args
=
{
Statement
.
class
,
ResultHandler
.
class
}),
@
Signature
(
type
=
StatementHandler
.
class
,
method
=
"update"
,
args
=
{
Statement
.
class
}),
@
Signature
(
type
=
StatementHandler
.
class
,
method
=
"batch"
,
args
=
{
Statement
.
class
})
})
public
class
MyMybatisLogInterceptor
implements
Interceptor
{
final
Log
log
=
LogFactory
.
getLog
(
MyTypeHandle
.
class
);
@
Override
public
Object
intercept
(
Invocation
invocation
)
throws
Throwable
{
Object
target
=
invocation
.
getTarget
();
StatementHandler
statementHandler
=
(
StatementHandler
)
target
;
try
{
return
invocation
.
proceed
();
}
finally
{
BoundSql
boundSql
=
statementHandler
.
getBoundSql
();
String
sql
=
boundSql
.
getSql
();
Object
parameterObject
=
boundSql
.
getParameterObject
();
List
<
ParameterMapping
>
parameterMappingList
=
boundSql
.
getParameterMappings
();
//
格式化
Sql
语句,去除换行符,替换参数
sql
=
formatSql
(
sql
,
parameterObject
,
parameterMappingList
);
log
.
warn
(
"SQL:"
+
sql
+
";"
);
}
}
private
String
formatSql
(
String
sql
,
Object
parameterObject
,
List
<
ParameterMapping
>
parameterMappingList
)
{
sql
=
sql
.
replaceAll
(
"
\r
|
\n
|
\t
"
,
" "
).
replaceAll
(
"
\\
s+"
,
" "
);
if
(
parameterObject
==
null
){
sql
=
sql
.
replaceAll
(
"
\\
?"
,
" null"
);
return
sql
;
}
if
(
Map
.
class
.
isAssignableFrom
(
parameterObject
.
getClass
())){
Map
pb
=
(
Map
)
parameterObject
;
for
(
ParameterMapping
temp
:
parameterMappingList
){
if
(
pb
.
get
(
temp
.
getProperty
())
!= null){
if
(
pb
.
get
(
temp
.
getProperty
()).
getClass
().
isAssignableFrom
(
JSONObject
.
class
)){
JSONObject
infObject
=
(
JSONObject
)
pb
.
get
(
temp
.
getProperty
());
sql
=
sql
.
replaceFirst
(
"
\\
?"
,
"'"
+
infObject
.
toJSONString
()
+
"'"
);
}
else
{
sql
=
sql
.
replaceFirst
(
"
\\
?"
,
"'"
+
pb
.
get
(
temp
.
getProperty
())
+
"'"
);
}
}
else
{
sql
=
sql
.
replaceFirst
(
"
\\
?"
,
"'null'"
);
}
}
}
if
(
String
.
class
.
isAssignableFrom
(
parameterObject
.
getClass
()))
{
sql
=
sql
.
replaceFirst
(
"
\\
?"
,
parameterObject
==
null
?
"null"
:
"'"
+
parameterObject
+
"'"
);
}
if
(
Number
.
class
.
isAssignableFrom
(
parameterObject
.
getClass
()))
{
sql
=
sql
.
replaceFirst
(
"
\\
?"
,
parameterObject
==
null
?
"null"
:
parameterObject
.
toString
());
}
return
sql
;
}
@
Override
public
Object
plugin
(
Object
o
)
{
return
Plugin
.
wrap
(
o
,
this
);
}
@
Override
public
void
setProperties
(
Properties
properties
)
{
}
}
张书毫_Mybatis/src/main/resource/MyMapper.xml
0 → 100644
View file @
360f92b7
<?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=
"MyMapper"
>
<select
id=
"selectUser"
resultType=
"com.cesgroup.mybatiswork.entity.User"
>
select * from user where id = #{id}
</select>
<insert
id=
"insert"
parameterType=
"com.cesgroup.mybatiswork.entity.User"
>
INSERT INTO user (id, name, info) VALUES (#{id}, #{name}, #{info})
</insert>
</mapper>
\ No newline at end of file
张书毫_Mybatis/src/main/resource/db.properties
0 → 100644
View file @
360f92b7
driver
=
com.mysql.cj.jdbc.Driver
driver
=
com.mysql.cj.jdbc.Driver
url
=
jdbc:mysql://localhost:3306/mybatis2?seUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username
=
root
password
=
root
\ No newline at end of file
张书毫_Mybatis/src/main/resource/mybatis-config.xml
0 → 100644
View file @
360f92b7
<?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>
<plugins>
<plugin
interceptor=
"com.cesgroup.mybatiswork.interceptor.MyMybatisLogInterceptor"
>
</plugin>
</plugins>
<!-- 环境,可以配置多个,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>
张书毫_Mybatis/src/main/resource/mybatis.sql
0 → 100644
View file @
360f92b7
SET
NAMES
utf8mb4
;
SET
NAMES
utf8mb4
;
SET
FOREIGN_KEY_CHECKS
=
0
;
DROP
TABLE
IF
EXISTS
`mybatis`
;
CREATE
TABLE
`user`
(
`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
;
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