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
2e361c12
Commit
2e361c12
authored
Jan 02, 2020
by
chenpengtao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
陈澎涛作业
parent
cbdde133
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
1064 additions
and
0 deletions
+1064
-0
README.md
陈澎涛_Mybatis2/README.md
+9
-0
b_article.sql
陈澎涛_Mybatis2/b_article.sql
+35
-0
pom.xml
陈澎涛_Mybatis2/pom.xml
+94
-0
Bootstrap.java
陈澎涛_Mybatis2/src/main/java/com/cesgroup/Bootstrap.java
+16
-0
DataSourceConfig.java
...2/src/main/java/com/cesgroup/config/DataSourceConfig.java
+30
-0
JSONHandler.java
...batis2/src/main/java/com/cesgroup/config/JSONHandler.java
+113
-0
MybatisLogInterceptor.java
.../main/java/com/cesgroup/config/MybatisLogInterceptor.java
+229
-0
ArticleController.java
.../main/java/com/cesgroup/controller/ArticleController.java
+45
-0
Article.java
陈澎涛_Mybatis2/src/main/java/com/cesgroup/entity/Article.java
+23
-0
ContentJson.java
...batis2/src/main/java/com/cesgroup/entity/ContentJson.java
+15
-0
ArticleMapper.java
...tis2/src/main/java/com/cesgroup/mapper/ArticleMapper.java
+19
-0
ArticleService.java
...s2/src/main/java/com/cesgroup/service/ArticleService.java
+16
-0
ArticleServiceImpl.java
...in/java/com/cesgroup/service/impl/ArticleServiceImpl.java
+53
-0
JacksonUtils.java
...batis2/src/main/java/com/cesgroup/utils/JacksonUtils.java
+197
-0
application.yml
陈澎涛_Mybatis2/src/main/resources/application.yml
+36
-0
logback-boot.xml
陈澎涛_Mybatis2/src/main/resources/logback-boot.xml
+80
-0
article.xml
陈澎涛_Mybatis2/src/main/resources/mappers/article.xml
+30
-0
SpringbootMybatisApplicationTests.java
.../java/com/cesgroup/SpringbootMybatisApplicationTests.java
+24
-0
No files found.
陈澎涛_Mybatis2/README.md
0 → 100644
View file @
2e361c12
############
############
##--建表语句如:b_article.sql
############
##--然后执行controller相应接口
############自定义日志输出################
##--http://localhost:8766/article/queryAll?id=1
陈澎涛_Mybatis2/b_article.sql
0 → 100644
View file @
2e361c12
/*
/*
Navicat MySQL Data Transfer
Source Server : 个人
Source Server Version : 50556
Source Host : localhost:3306
Source Database : cesgroup
Target Server Type : MYSQL
Target Server Version : 50556
File Encoding : 65001
Date: 2019-12-03 17:09:46
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for b_article
-- ----------------------------
DROP
TABLE
IF
EXISTS
`b_article`
;
CREATE
TABLE
`b_article`
(
`id`
int
(
10
)
unsigned
NOT
NULL
AUTO_INCREMENT
COMMENT
'主键 自增'
,
`title`
varchar
(
255
)
DEFAULT
NULL
,
`content`
varchar
(
255
)
DEFAULT
NULL
,
`create_time`
timestamp
NULL
DEFAULT
NULL
,
`update_time`
timestamp
NULL
DEFAULT
NULL
ON
UPDATE
CURRENT_TIMESTAMP
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
2
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of b_article
-- ----------------------------
INSERT
INTO
`b_article`
VALUES
(
'1'
,
'测试一'
,
'{
\"
age
\"
:
\"
18
\"
,
\"
name
\"
:
\"
chen
\"
}'
,
'2019-12-02 17:16:59'
,
'2019-12-02 17:18:24'
);
陈澎涛_Mybatis2/pom.xml
0 → 100644
View file @
2e361c12
<?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.1.RELEASE
</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>
com.cesgroup
</groupId>
<artifactId>
springboot-mybatis
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<name>
springboot-mybatis
</name>
<description>
Demo project for Spring Boot
</description>
<properties>
<java.version>
1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.mybatis.spring.boot
</groupId>
<artifactId>
mybatis-spring-boot-starter
</artifactId>
<version>
2.1.1
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-jdbc
</artifactId>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
druid
</artifactId>
<version>
1.0.27
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.46
</version>
</dependency>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<scope>
runtime
</scope>
<version>
8.0.11
</version>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.46
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
4.6.4
</version>
</dependency>
<dependency>
<groupId>
com.google.guava
</groupId>
<artifactId>
guava
</artifactId>
<version>
28.1-jre
</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
陈澎涛_Mybatis2/src/main/java/com/cesgroup/Bootstrap.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
;
package
com
.
cesgroup
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.annotation.ComponentScan
;
@SpringBootApplication
@ComponentScan
(
basePackages
=
"com.cesgroup"
)
public
class
Bootstrap
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Bootstrap
.
class
,
args
);
}
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/config/DataSourceConfig.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
config
;
package
com
.
cesgroup
.
config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.jdbc.DataSourceBuilder
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.jdbc.datasource.DataSourceTransactionManager
;
import
javax.sql.DataSource
;
/**
* Created by chenpt on 2019/7/27.
*/
@Configuration
public
class
DataSourceConfig
{
@Bean
(
name
=
"dataSource"
)
@ConfigurationProperties
(
prefix
=
"spring.datasource"
)
public
DataSource
dataSource
(){
return
DataSourceBuilder
.
create
().
build
();
}
// 配置事物管理器
@Bean
(
name
=
"transactionManager"
)
public
DataSourceTransactionManager
transactionManager
(){
return
new
DataSourceTransactionManager
(
dataSource
());
}
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/config/JSONHandler.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
config
;
package
com
.
cesgroup
.
config
;
import
com.cesgroup.entity.ContentJson
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.core.JsonParser
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.ibatis.type.BaseTypeHandler
;
import
org.apache.ibatis.type.JdbcType
;
import
org.apache.ibatis.type.MappedJdbcTypes
;
import
org.apache.ibatis.type.MappedTypes
;
import
java.io.IOException
;
import
java.sql.CallableStatement
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
/**
* 自定义类型处理器
* Created by chenpt on 2019/12/2.
*/
@MappedJdbcTypes
(
JdbcType
.
VARCHAR
)
@MappedTypes
(
ContentJson
.
class
)
@Slf4j
public
class
JSONHandler
<
T
extends
Object
>
extends
BaseTypeHandler
<
T
>
{
private
static
final
ObjectMapper
mapper
=
new
ObjectMapper
();
private
Class
<
T
>
clazz
;
static
{
mapper
.
configure
(
JsonParser
.
Feature
.
ALLOW_MISSING_VALUES
,
false
);
mapper
.
setSerializationInclusion
(
JsonInclude
.
Include
.
NON_NULL
);
}
public
JSONHandler
(
Class
<
T
>
clazz
)
{
if
(
clazz
==
null
)
{
throw
new
NullPointerException
(
"Type argument cannot be null"
);
}
this
.
clazz
=
clazz
;
}
/**
* object转json string
* @param object
* @return
*/
private
String
toJSON
(
T
object
)
{
try
{
String
string
=
mapper
.
writeValueAsString
(
object
);
log
.
info
(
">>>> json handler string:{} <<<<"
,
string
);
return
string
;
}
catch
(
Exception
e
)
{
log
.
error
(
">>>> covert object to json string failed, error message: <<<<"
,
e
.
getMessage
());
}
return
null
;
}
/**
* json转object
* @param json
* @param clazz
* @return
* @throws IOException
*/
private
T
toObject
(
String
json
,
Class
<
T
>
clazz
)
throws
IOException
{
if
(
json
!=
null
&&
json
!=
""
)
{
return
mapper
.
readValue
(
json
,
clazz
);
}
return
null
;
}
@Override
public
void
setNonNullParameter
(
PreparedStatement
preparedStatement
,
int
i
,
T
t
,
JdbcType
jdbcType
)
throws
SQLException
{
try
{
preparedStatement
.
setString
(
i
,
toJSON
(
t
));
}
catch
(
Exception
e
)
{
log
.
error
(
">>>> preparedStatement set string failed, error message:{} <<<<"
,
e
.
getMessage
());
}
}
@Override
public
T
getNullableResult
(
ResultSet
resultSet
,
String
s
)
throws
SQLException
{
try
{
return
toObject
(
resultSet
.
getString
(
s
),
clazz
);
}
catch
(
IOException
e
)
{
log
.
error
(
">>>> convert json string to object failed, error message:{} <<<<"
,
e
.
getMessage
());
}
return
null
;
}
@Override
public
T
getNullableResult
(
ResultSet
resultSet
,
int
i
)
throws
SQLException
{
try
{
return
toObject
(
resultSet
.
getString
(
i
),
clazz
);
}
catch
(
IOException
e
)
{
log
.
error
(
">>>> convert json string to object failed, error message:{} <<<<"
,
e
.
getMessage
());
}
return
null
;
}
@Override
public
T
getNullableResult
(
CallableStatement
callableStatement
,
int
i
)
throws
SQLException
{
try
{
return
toObject
(
callableStatement
.
getString
(
i
),
clazz
);
}
catch
(
IOException
e
)
{
log
.
error
(
">>>> convert json string to object failed, error message:{} <<<<"
,
e
.
getMessage
());
}
return
null
;
}
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/config/MybatisLogInterceptor.java
0 → 100644
View file @
2e361c12
This diff is collapsed.
Click to expand it.
陈澎涛_Mybatis2/src/main/java/com/cesgroup/controller/ArticleController.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
controller
;
package
com
.
cesgroup
.
controller
;
import
com.cesgroup.entity.Article
;
import
com.cesgroup.service.ArticleService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Created by chenpt on 2019/12/2.
*/
@Controller
@RequestMapping
(
"/article"
)
public
class
ArticleController
{
@Autowired
private
ArticleService
articleService
;
@RequestMapping
(
"/queryAll"
)
@ResponseBody
public
List
<
Article
>
queryAll
(
Integer
id
){
return
articleService
.
queryAll
(
id
);
}
@RequestMapping
(
"/batchAdd"
)
@ResponseBody
public
Integer
batchAdd
(){
List
<
Article
>
articles
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
1000
;
i
++){
Article
article
=
new
Article
();
article
.
setTitle
(
"title-"
+
i
);
article
.
setContent
(
"content-"
+
i
);
articles
.
add
(
article
);
}
return
articleService
.
batchAdd
(
articles
);
}
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/entity/Article.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
entity
;
package
com
.
cesgroup
.
entity
;
import
lombok.Data
;
import
lombok.ToString
;
import
java.util.Date
;
/**
* Created by chenpt on 2019/12/2.
*/
@Data
@ToString
public
class
Article
{
private
Integer
id
;
private
String
title
;
private
String
content
;
private
ContentJson
contentJson
;
private
Date
createTime
;
private
Date
updateTime
;
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/entity/ContentJson.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
entity
;
package
com
.
cesgroup
.
entity
;
import
lombok.Data
;
/**
* Created by chenpt on 2019/12/2.
*/
@Data
public
class
ContentJson
{
private
String
age
;
private
String
name
;
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/mapper/ArticleMapper.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
mapper
;
package
com
.
cesgroup
.
mapper
;
import
com.cesgroup.entity.Article
;
import
org.apache.ibatis.annotations.Mapper
;
import
java.util.List
;
/**
* Created by chenpt on 2019/12/2.
*/
@Mapper
public
interface
ArticleMapper
{
List
<
Article
>
queryAll
(
Integer
id
);
Integer
batchAdd
(
List
<
Article
>
list
);
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/service/ArticleService.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
service
;
package
com
.
cesgroup
.
service
;
import
com.cesgroup.entity.Article
;
import
java.util.List
;
/**
* Created by chenpt on 2019/12/2.
*/
public
interface
ArticleService
{
List
<
Article
>
queryAll
(
Integer
id
);
Integer
batchAdd
(
List
<
Article
>
list
);
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/service/impl/ArticleServiceImpl.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
service
.
impl
;
package
com
.
cesgroup
.
service
.
impl
;
import
com.cesgroup.entity.Article
;
import
com.cesgroup.mapper.ArticleMapper
;
import
com.cesgroup.service.ArticleService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.ibatis.session.ExecutorType
;
import
org.apache.ibatis.session.SqlSession
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
/**
* Created by chenpt on 2019/12/2.
*/
@Service
@Slf4j
public
class
ArticleServiceImpl
implements
ArticleService
{
@Autowired
private
ArticleMapper
articleMapper
;
@Autowired
private
SqlSessionTemplate
sqlSessionTemplate
;
@Override
public
List
<
Article
>
queryAll
(
Integer
id
)
{
return
articleMapper
.
queryAll
(
id
);
}
@Override
public
Integer
batchAdd
(
List
<
Article
>
list
)
{
// return articleMapper.batchAdd(list);//默认写法
Integer
res
=
0
;
long
time1
=
System
.
currentTimeMillis
();
log
.
info
(
"*****开始插入*****{}"
,
time1
);
SqlSession
sqlSession
=
sqlSessionTemplate
.
getSqlSessionFactory
().
openSession
(
ExecutorType
.
BATCH
,
false
);
//关闭session的自动提交;
try
{
res
=
sqlSession
.
insert
(
"com.cesgroup.mapper.ArticleMapper.batchAdd"
,
list
);
sqlSession
.
commit
();
}
catch
(
Exception
e
){
log
.
error
(
"批量插入失败:{}"
,
e
);
}
finally
{
sqlSession
.
close
();
}
long
time2
=
System
.
currentTimeMillis
();
log
.
info
(
"*****插入结束*****{}"
,
time2
);
log
.
info
(
"*************累计耗时*************: "
+
(
time2
-
time1
));
return
res
;
}
}
陈澎涛_Mybatis2/src/main/java/com/cesgroup/utils/JacksonUtils.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
.
utils
;
package
com
.
cesgroup
.
utils
;
import
com.alibaba.fastjson.TypeReference
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.JavaType
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* Created by chenpengtao on 2019/6/19.
*/
public
class
JacksonUtils
{
private
final
static
ObjectMapper
objectMapper
=
new
ObjectMapper
();
private
JacksonUtils
()
{
}
public
static
ObjectMapper
getInstance
()
{
return
objectMapper
;
}
/**
* javaBean、列表数组转换为json字符串
*/
public
static
String
obj2json
(
Object
obj
)
throws
Exception
{
return
objectMapper
.
writeValueAsString
(
obj
);
}
/**
* javaBean、列表数组转换为json字符串,忽略空值
*/
public
static
String
obj2jsonIgnoreNull
(
Object
obj
)
throws
Exception
{
ObjectMapper
mapper
=
new
ObjectMapper
();
mapper
.
setSerializationInclusion
(
JsonInclude
.
Include
.
NON_NULL
);
return
mapper
.
writeValueAsString
(
obj
);
}
/**
* json 转JavaBean
*/
public
static
<
T
>
T
json2pojo
(
String
jsonString
,
Class
<
T
>
clazz
)
throws
Exception
{
objectMapper
.
configure
(
DeserializationFeature
.
ACCEPT_SINGLE_VALUE_AS_ARRAY
,
true
);
return
objectMapper
.
readValue
(
jsonString
,
clazz
);
}
/**
* json 转JavaBean
* 忽略多余字段
*/
public
static
<
T
>
T
json3pojo
(
String
jsonString
,
Class
<
T
>
clazz
)
throws
Exception
{
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
return
objectMapper
.
readValue
(
jsonString
,
clazz
);
}
/**
* json字符串转换为map
*/
public
static
<
T
>
Map
<
String
,
Object
>
json2map
(
String
jsonString
)
throws
Exception
{
ObjectMapper
mapper
=
new
ObjectMapper
();
mapper
.
setSerializationInclusion
(
JsonInclude
.
Include
.
NON_NULL
);
return
mapper
.
readValue
(
jsonString
,
Map
.
class
);
}
/**
* 深度转换json成map
*
* @param json
* @return
*/
public
static
Map
<
String
,
Object
>
json2mapDeeply
(
String
json
)
throws
Exception
{
return
json2MapRecursion
(
json
,
objectMapper
);
}
/**
* 把json解析成list,如果list内部的元素存在jsonString,继续解析
*
* @param json
* @param mapper 解析工具
* @return
* @throws Exception
*/
private
static
List
<
Object
>
json2ListRecursion
(
String
json
,
ObjectMapper
mapper
)
throws
Exception
{
if
(
json
==
null
)
{
return
null
;
}
List
<
Object
>
list
=
mapper
.
readValue
(
json
,
List
.
class
);
for
(
Object
obj
:
list
)
{
if
(
obj
!=
null
&&
obj
instanceof
String
)
{
String
str
=
(
String
)
obj
;
if
(
str
.
startsWith
(
"["
))
{
obj
=
json2ListRecursion
(
str
,
mapper
);
}
else
if
(
obj
.
toString
().
startsWith
(
"{"
))
{
obj
=
json2MapRecursion
(
str
,
mapper
);
}
}
}
return
list
;
}
/**
* 把json解析成map,如果map内部的value存在jsonString,继续解析
*
* @param json
* @param mapper
* @return
* @throws Exception
*/
private
static
Map
<
String
,
Object
>
json2MapRecursion
(
String
json
,
ObjectMapper
mapper
)
throws
Exception
{
if
(
json
==
null
)
{
return
null
;
}
Map
<
String
,
Object
>
map
=
mapper
.
readValue
(
json
,
Map
.
class
);
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
Object
obj
=
entry
.
getValue
();
if
(
obj
!=
null
&&
obj
instanceof
String
)
{
String
str
=
((
String
)
obj
);
if
(
str
.
startsWith
(
"["
))
{
List
<?>
list
=
json2ListRecursion
(
str
,
mapper
);
map
.
put
(
entry
.
getKey
(),
list
);
}
else
if
(
str
.
startsWith
(
"{"
))
{
Map
<
String
,
Object
>
mapRecursion
=
json2MapRecursion
(
str
,
mapper
);
map
.
put
(
entry
.
getKey
(),
mapRecursion
);
}
}
}
return
map
;
}
/**
* 与javaBean json数组字符串转换为列表
*/
public
static
<
T
>
List
<
T
>
json2list
(
String
jsonArrayStr
,
Class
<
T
>
clazz
)
throws
Exception
{
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
JavaType
javaType
=
getCollectionType
(
ArrayList
.
class
,
clazz
);
List
<
T
>
lst
=
(
List
<
T
>)
objectMapper
.
readValue
(
jsonArrayStr
,
javaType
);
return
lst
;
}
/**
* 获取泛型的Collection Type
*
* @param collectionClass 泛型的Collection
* @param elementClasses 元素类
* @return JavaType Java类型
* @since 1.0
*/
public
static
JavaType
getCollectionType
(
Class
<?>
collectionClass
,
Class
<?>...
elementClasses
)
{
return
objectMapper
.
getTypeFactory
().
constructParametricType
(
collectionClass
,
elementClasses
);
}
/**
* map 转JavaBean
*/
public
static
<
T
>
T
map2pojo
(
Map
map
,
Class
<
T
>
clazz
)
{
return
objectMapper
.
convertValue
(
map
,
clazz
);
}
/**
* map 转json
*
* @param map
* @return
*/
public
static
String
mapToJson
(
Map
map
)
{
try
{
return
objectMapper
.
writeValueAsString
(
map
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
""
;
}
/**
* map 转JavaBean
*/
public
static
<
T
>
T
obj2pojo
(
Object
obj
,
Class
<
T
>
clazz
)
{
return
objectMapper
.
convertValue
(
obj
,
clazz
);
}
}
陈澎涛_Mybatis2/src/main/resources/application.yml
0 → 100644
View file @
2e361c12
server
:
server
:
port
:
8766
sessionTimeout
:
300000
servlet
:
contextPath
:
/
spring
:
datasource
:
driver-class-name
:
com.mysql.jdbc.Driver
jdbc-url
:
'
jdbc:mysql://localhost:3306/cesgroup?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8'
userName
:
'
root'
password
:
'
root'
hikari
:
minimum-idle
:
5
maximum-pool-size
:
20
auto-commit
:
true
idle-timeout
:
60000
pool-name
:
DatebookHikariCP
max-lifetime
:
1800000
connection-timeout
:
30000
connection-test-query
:
SELECT 1
servlet
:
multipart
:
max-request-size
:
1000MB
max-file-size
:
100MB
devtools
:
restart
:
additional-paths
:
src/main/java
exclude
:
static/**
mybatis
:
mapperLocations
:
classpath:mappers/**/*.xml
typeAliasesPackage
:
com.chenpt.entity
configuration
:
mapUnderscoreToCamelCase
:
true
logging
:
config
:
classpath:logback-boot.xml
陈澎涛_Mybatis2/src/main/resources/logback-boot.xml
0 → 100644
View file @
2e361c12
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->
<!--<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n"/>-->
<property
name=
"CONSOLE_LOG_PATTERN"
value=
"%yellow(%-12(%d{HH:mm:ss.SSS})) |-%highlight(%-5level) [%thread] %cyan(%c [%L]) -| %msg%n"
/>
<!--文件存放目录-->
<property
name=
"LOG_HOME"
value=
"/usr/bwh/logs"
/>
<!--文件存放天数-->
<property
name=
"log_info_max"
value=
"7"
></property>
<property
name=
"debug_info_max"
value=
"7"
></property>
<property
name=
"error_info_max"
value=
"7"
></property>
<!-- 控制台 -->
<appender
name=
"CONSOLE"
class=
"ch.qos.logback.core.ConsoleAppender"
>
<filter
class=
"ch.qos.logback.classic.filter.ThresholdFilter"
>
<level>
debug
</level>
</filter>
<encoder>
<pattern>
${CONSOLE_LOG_PATTERN}
</pattern>
<!--<charset>utf8</charset>-->
</encoder>
</appender>
<appender
name=
"FILE"
class=
"ch.qos.logback.core.rolling.RollingFileAppender"
>
<File>
${LOG_HOME}/bwh.log
</File>
<rollingPolicy
class=
"ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"
>
<fileNamePattern>
${LOG_HOME}/bwh.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>
1MB
</maxFileSize>
<maxHistory>
60
</maxHistory>
<totalSizeCap>
100GB
</totalSizeCap>
</rollingPolicy>
<append>
true
</append>
<encoder
charset=
"UTF-8"
>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n
</pattern>
</encoder>
</appender>
<appender
name=
"FILE-ERROR"
class=
"ch.qos.logback.core.rolling.RollingFileAppender"
>
<filter
class=
"ch.qos.logback.classic.filter.LevelFilter"
>
<level>
error
</level>
<onMatch>
ACCEPT
</onMatch>
<onMismatch>
DENY
</onMismatch>
</filter>
<File>
${LOG_HOME}/bwh-error.log
</File>
<!-- 按照时间来分页,格式:【yyyy-MM-dd_HH_mm_ss】 日志文件输出的文件名 -->
<rollingPolicy
class=
"ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"
>
<fileNamePattern>
${LOG_HOME}/bwh-error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>
1MB
</maxFileSize>
<maxHistory>
60
</maxHistory>
<totalSizeCap>
100GB
</totalSizeCap>
</rollingPolicy>
<append>
true
</append>
<encoder
charset=
"UTF-8"
>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n
</pattern>
</encoder>
</appender>
<logger
name=
"org.springframework"
level=
"INFO"
/>
<logger
name=
"org.hibernate"
level=
"INFO"
/>
<logger
name=
"org.mybatis"
level=
"INFO"
/>
<logger
name=
"org.quartz"
level=
"INFO"
/>
<logger
name=
"org.apache.cxf"
level=
"INFO"
/>
<logger
name=
"java.sql"
level=
"DEBUG"
/>
<logger
name=
"com.cesgroup"
level=
"DEBUG"
/>
<root
level=
"INFO"
>
<appender-ref
ref=
"CONSOLE"
/>
<appender-ref
ref=
"FILE"
/>
<appender-ref
ref=
"FILE-ERROR"
/>
</root>
</configuration>
\ No newline at end of file
陈澎涛_Mybatis2/src/main/resources/mappers/article.xml
0 → 100644
View file @
2e361c12
<?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=
"com.cesgroup.mapper.ArticleMapper"
>
<resultMap
id=
"BaseResultMap"
type=
"com.cesgroup.entity.Article"
>
<id
column=
"id"
property=
"id"
/>
<result
column=
"title"
property=
"title"
/>
<result
column=
"content"
property=
"content"
/>
<result
column=
"content"
property=
"contentJson"
javaType=
"com.cesgroup.entity.ContentJson"
jdbcType=
"LONGVARCHAR"
typeHandler=
"com.cesgroup.config.JSONHandler"
/>
<result
column=
"create_time"
property=
"createTime"
/>
<result
column=
"update_time"
property=
"updateTime"
/>
</resultMap>
<sql
id=
"BaseSql"
>
id,title,content,create_time,update_time
</sql>
<select
id=
"queryAll"
resultMap=
"BaseResultMap"
>
SELECT
<include
refid=
"BaseSql"
/>
FROM b_article WHERE id=#{id}
</select>
<insert
id=
"batchAdd"
parameterType=
"java.util.List"
>
INSERT INTO b_article
(id, title, content,create_time,update_time)
VALUES
<foreach
collection =
"list"
item=
"item"
separator =
","
>
(#{item.id}, #{item.title}, #{item.content},now(),now())
</foreach>
</insert>
</mapper>
陈澎涛_Mybatis2/src/test/java/com/cesgroup/SpringbootMybatisApplicationTests.java
0 → 100644
View file @
2e361c12
package
com
.
cesgroup
;
package
com
.
cesgroup
;
import
com.cesgroup.mapper.ArticleMapper
;
import
org.junit.jupiter.api.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
javax.annotation.Resource
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
class
SpringbootMybatisApplicationTests
{
@Resource
private
ArticleMapper
mapper
;
@Test
void
contextLoads
()
{
mapper
.
queryAll
(
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