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
337b5f8b
Commit
337b5f8b
authored
Jan 02, 2020
by
hesz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
何素蒸培训作业
parent
104a9f1e
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
691 additions
and
0 deletions
+691
-0
pom.xml
何素蒸_Mybatis2/pom.xml
+159
-0
readme.md
何素蒸_Mybatis2/readme.md
+16
-0
Application.java
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/Application.java
+21
-0
SqlLogInterceptor.java
...n/java/com/ces/mybatis/Interceptor/SqlLogInterceptor.java
+92
-0
IUserDao.java
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/dao/IUserDao.java
+17
-0
Context.java
...Mybatis2/src/main/java/com/ces/mybatis/model/Context.java
+46
-0
User.java
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/model/User.java
+36
-0
IUserService.java
...2/src/main/java/com/ces/mybatis/service/IUserService.java
+17
-0
UserService.java
...c/main/java/com/ces/mybatis/service/impl/UserService.java
+64
-0
ContentTypeHandler.java
.../java/com/ces/mybatis/typeHandler/ContentTypeHandler.java
+43
-0
application.yml
何素蒸_Mybatis2/src/main/resource/application.yml
+37
-0
IUserDao.xml
何素蒸_Mybatis2/src/main/resource/mapper/IUserDao.xml
+69
-0
AppTest.java
何素蒸_Mybatis2/src/test/java/com/ces/mybatis/AppTest.java
+74
-0
No files found.
何素蒸_Mybatis2/pom.xml
0 → 100644
View file @
337b5f8b
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.ces
</groupId>
<artifactId>
mybatis
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<name>
mybatis
</name>
<!--spring boot 版本 -->
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.5.4.RELEASE
</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<java.version>
1.8
</java.version>
<poi.version>
4.1.0
</poi.version>
<mybatis-spring-boot-starter.version>
1.1.1
</mybatis-spring-boot-starter.version>
<druid.version>
1.0.29
</druid.version>
<fastjson.version>
1.2.32
</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter
</artifactId>
</dependency>
<!-- json处理工具 -->
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
${fastjson.version}
</version>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
druid
</artifactId>
<version>
${druid.version}
</version>
</dependency>
<!-- 数据库连接驱动 -->
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<scope>
runtime
</scope>
</dependency>
<dependency>
<groupId>
org.mybatis.spring.boot
</groupId>
<artifactId>
mybatis-spring-boot-starter
</artifactId>
<version>
1.3.0
</version>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<version>
1.16.22
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
${poi.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<version>
${poi.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml-schemas
</artifactId>
<version>
${poi.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-scratchpad
</artifactId>
<version>
${poi.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
ooxml-schemas
</artifactId>
<version>
1.4
</version>
</dependency>
<dependency>
<groupId>
fr.opensagres.xdocreport
</groupId>
<artifactId>
xdocreport
</artifactId>
<version>
2.0.2
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>
org.jsoup
</groupId>
<artifactId>
jsoup
</artifactId>
<version>
1.12.1
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
4.6.4
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
<dependency>
<groupId>
com.spring4all
</groupId>
<artifactId>
swagger-spring-boot-starter
</artifactId>
<version>
1.9.0.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<scope>
runtime
</scope>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>
org.aspectj
</groupId>
<artifactId>
aspectjweaver
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-configuration-processor
</artifactId>
<optional>
true
</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
何素蒸_Mybatis2/readme.md
0 → 100644
View file @
337b5f8b
1.
项目resource下的application.yml为配置文件,请修改datasource下的url、username、password,保证存在对应的数据库。
1.
项目resource下的application.yml为配置文件,请修改datasource下的url、username、password,保证存在对应的数据库。
生成项目对应的数据表:
/
*Table structure for table `t_sys_user` *
/
DROP TABLE IF EXISTS
`t_sys_user`
;
CREATE TABLE
`t_sys_user`
(
`user_id`
varchar(20) NOT NULL COMMENT '用户id',
`user_name`
varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '用户名',
`password`
varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '密码',
`context`
varchar(1000) COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
PRIMARY KEY (
`user_id`
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='用户信息表';
2.
打开项目执行 test目录下的AppTest测试用例
\ No newline at end of file
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/Application.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
;
package
com
.
ces
.
mybatis
;
import
org.mybatis.spring.annotation.MapperScan
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.annotation.ComponentScan
;
import
com.spring4all.swagger.EnableSwagger2Doc
;
@EnableSwagger2Doc
@SpringBootApplication
@ComponentScan
({
"com.ces"
})
@MapperScan
(
"com.ces.*.dao"
)
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
}
}
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/Interceptor/SqlLogInterceptor.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
Interceptor
;
package
com
.
ces
.
mybatis
.
Interceptor
;
import
java.sql.Statement
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Properties
;
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.Interceptor
;
import
org.apache.ibatis.plugin.Intercepts
;
import
org.apache.ibatis.plugin.Invocation
;
import
org.apache.ibatis.plugin.Plugin
;
import
org.apache.ibatis.plugin.Signature
;
import
org.apache.ibatis.session.ResultHandler
;
import
org.springframework.stereotype.Component
;
import
com.alibaba.fastjson.JSONObject
;
import
com.ces.mybatis.typeHandler.ContentTypeHandler
;
@Intercepts
({
@Signature
(
type
=
StatementHandler
.
class
,
method
=
"query"
,
args
=
{
Statement
.
class
,
ResultHandler
.
class
}),
@Signature
(
type
=
StatementHandler
.
class
,
method
=
"update"
,
args
=
{
Statement
.
class
})
})
@Component
public
class
SqlLogInterceptor
implements
Interceptor
{
final
Log
log
=
LogFactory
.
getLog
(
ContentTypeHandler
.
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
=
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
());
}
log
.
warn
(
"SQL:"
+
sql
+
";"
);
}
}
@Override
public
Object
plugin
(
Object
o
)
{
return
Plugin
.
wrap
(
o
,
this
);
}
@Override
public
void
setProperties
(
Properties
properties
)
{
}
}
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/dao/IUserDao.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
dao
;
package
com
.
ces
.
mybatis
.
dao
;
import
java.util.List
;
import
org.apache.ibatis.annotations.Param
;
import
com.ces.mybatis.model.User
;
public
interface
IUserDao
{
int
insert
(
User
user
);
boolean
insertOrUpdateByBatch
(
@Param
(
"list"
)
List
<
User
>
list
);
int
update
(
User
user
);
int
delete
(
@Param
(
"userId"
)
String
userId
);
List
<
User
>
getAll
();
User
find
(
String
userId
);
}
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/model/Context.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
model
;
package
com
.
ces
.
mybatis
.
model
;
public
class
Context
{
private
String
province
;
private
String
city
;
private
String
district
;
public
Context
()
{
}
public
Context
(
String
username
,
String
password
,
Boolean
enabled
,
String
context
)
{
this
.
province
=
username
;
this
.
city
=
password
;
this
.
district
=
password
;
}
public
String
getProvince
()
{
return
province
;
}
public
void
setProvince
(
String
province
)
{
this
.
province
=
province
;
}
public
String
getCity
()
{
return
city
;
}
public
void
setCity
(
String
city
)
{
this
.
city
=
city
;
}
public
String
getDistrict
()
{
return
district
;
}
public
void
setDistrict
(
String
district
)
{
this
.
district
=
district
;
}
}
\ No newline at end of file
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/model/User.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
model
;
package
com
.
ces
.
mybatis
.
model
;
public
class
User
{
private
String
userId
;
private
String
userName
;
private
String
password
;
private
Context
context
;
public
String
getUserId
()
{
return
userId
;
}
public
void
setUserId
(
String
userId
)
{
this
.
userId
=
userId
;
}
public
String
getUserName
()
{
return
userName
;
}
public
void
setUserName
(
String
userName
)
{
this
.
userName
=
userName
;
}
public
String
getPassword
()
{
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
Context
getContext
()
{
return
context
;
}
public
void
setContext
(
Context
context
)
{
this
.
context
=
context
;
}
}
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/service/IUserService.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
service
;
package
com
.
ces
.
mybatis
.
service
;
import
java.util.List
;
import
org.apache.ibatis.annotations.Param
;
import
com.ces.mybatis.model.User
;
public
interface
IUserService
{
List
<
User
>
getAll
();
int
insert
(
User
user
);
int
delete
(
String
userId
);
int
update
(
User
user
);
User
find
(
String
userId
);
boolean
insertOrUpdateByBatch
(
List
<
User
>
list
);
}
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/service/impl/UserService.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
service
.
impl
;
package
com
.
ces
.
mybatis
.
service
.
impl
;
import
java.util.List
;
import
com.ces.mybatis.model.User
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.StringUtils
;
import
com.ces.mybatis.dao.IUserDao
;
import
com.ces.mybatis.service.IUserService
;
@Service
public
class
UserService
implements
IUserService
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
UserService
.
class
);
@Autowired
private
IUserDao
userDao
;
@Override
public
List
<
User
>
getAll
()
{
return
userDao
.
getAll
();
}
@Override
public
int
insert
(
User
user
)
{
return
userDao
.
insert
(
user
);
}
@Override
public
int
delete
(
String
userId
)
{
return
userDao
.
delete
(
userId
);
}
@Override
public
int
update
(
User
user
)
{
User
userOutVo
=
null
;
if
(
user
!=
null
&&
!
StringUtils
.
isEmpty
(
user
.
getUserId
()))
{
userOutVo
=
userDao
.
find
(
user
.
getUserId
());
if
(
userOutVo
!=
null
)
{
LOGGER
.
info
(
"用户名:"
+
user
.
getUserName
());
userOutVo
.
setUserName
(
user
.
getUserName
());
userOutVo
.
setPassword
(
user
.
getPassword
());
userOutVo
.
setContext
(
user
.
getContext
());
}
}
return
userDao
.
update
(
userOutVo
);
}
@Override
public
User
find
(
String
userId
)
{
return
userDao
.
find
(
userId
);
}
@Override
public
boolean
insertOrUpdateByBatch
(
List
<
User
>
list
)
{
return
userDao
.
insertOrUpdateByBatch
(
list
);
}
}
何素蒸_Mybatis2/src/main/java/com/ces/mybatis/typeHandler/ContentTypeHandler.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
.
typeHandler
;
package
com
.
ces
.
mybatis
.
typeHandler
;
import
com.ces.mybatis.model.*
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.ibatis.type.JdbcType
;
import
org.apache.ibatis.type.MappedJdbcTypes
;
import
org.apache.ibatis.type.MappedTypes
;
import
org.apache.ibatis.type.TypeHandler
;
import
java.sql.CallableStatement
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
@MappedTypes
({
Context
.
class
})
@MappedJdbcTypes
({
JdbcType
.
VARCHAR
})
public
class
ContentTypeHandler
implements
TypeHandler
<
Context
>
{
@Override
public
void
setParameter
(
PreparedStatement
preparedStatement
,
int
i
,
Context
context
,
JdbcType
jdbcType
)
throws
SQLException
{
String
s
=
JSONObject
.
toJSONString
(
context
);
preparedStatement
.
setString
(
i
,
s
);
}
@Override
public
Context
getResult
(
ResultSet
rs
,
String
columnName
)
throws
SQLException
{
String
string
=
rs
.
getString
(
columnName
);
return
JSONObject
.
parseObject
(
string
,
Context
.
class
);
}
@Override
public
Context
getResult
(
ResultSet
rs
,
int
columnIndex
)
throws
SQLException
{
String
string
=
rs
.
getString
(
columnIndex
);
return
JSONObject
.
parseObject
(
string
,
Context
.
class
);
}
@Override
public
Context
getResult
(
CallableStatement
cs
,
int
columnIndex
)
throws
SQLException
{
String
string
=
cs
.
getString
(
columnIndex
);
return
JSONObject
.
parseObject
(
string
,
Context
.
class
);
}
}
何素蒸_Mybatis2/src/main/resource/application.yml
0 → 100644
View file @
337b5f8b
spring
:
spring
:
datasource
:
driver-class-name
:
com.mysql.jdbc.Driver
url
:
jdbc:mysql://127.0.0.1:3306/lsgz?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Shanghai
username
:
root
password
:
ces123
druid
:
validationQuery
:
SELECT 1
initialSize
:
10
minIdle
:
10
maxActive
:
200
minEvictableIdleTimeMillis
:
180000
testOnBorrow
:
false
testWhileIdle
:
true
removeAbandoned
:
true
removeAbandonedTimeout
:
1800
logAbandoned
:
true
poolPreparedStatements
:
true
maxOpenPreparedStatements
:
100
server
:
port
:
81
servlet-path
:
/
mybatis
:
mapper-locations
:
-
classpath*:/mapper/*.xml
-
classpath*:/com/ces/*/dao/*.xml
typeAliasesPackage
:
com.ces.*.model
type-handlers-package
:
com.ces.mybatis.typeHandler
configuration
:
map-underscore-to-camel-case
:
true
lazy-loading-enabled
:
false
auto-mapping-behavior
:
full
useGeneratedKeys
:
true
useColumnLabel
:
true
mapUnderscoreToCamelCase
:
true
\ No newline at end of file
何素蒸_Mybatis2/src/main/resource/mapper/IUserDao.xml
0 → 100644
View file @
337b5f8b
<?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.ces.mybatis.dao.IUserDao"
>
<resultMap
id=
"BaseResultMap"
type=
"com.ces.mybatis.model.User"
>
<id
column=
"user_id"
property=
"userId"
/>
<result
column=
"user_name"
property=
"userName"
/>
<result
column=
"password"
property=
"password"
/>
<result
column=
"context"
property=
"context"
typeHandler=
"com.ces.mybatis.typeHandler.ContentTypeHandler"
/>
</resultMap>
<insert
id=
"insert"
useGeneratedKeys=
"true"
keyProperty=
"userId"
keyColumn=
"user_id"
parameterType=
"com.ces.mybatis.model.User"
>
insert into t_sys_user(user_id,user_name,password,context) values(#{userId},#{userName},#{password},#{context})
</insert>
<insert
id=
"insertOrUpdateByBatch"
keyProperty=
"userId"
useGeneratedKeys=
"true"
keyColumn=
"user_id"
>
insert into t_sys_user (user_id,user_name,password,context)
VALUES
<foreach
item=
"user"
index=
"index"
collection =
"list"
separator =
"),("
open=
"("
close=
")"
>
user.userId,user.userName,user.password,user.context
</foreach>
ON DUPLICATE KEY UPDATE
user_name = VALUES(user_name)
password = VALUES(password)
context = VALUES(context)
</insert>
<update
id=
"update"
parameterType=
"com.ces.mybatis.model.User"
>
update t_sys_user
<set>
<if
test=
"userName != null"
>
user_name=#{userName},
</if>
<if
test=
"password != null"
>
password=#{password},
</if>
<if
test=
"context != null"
>
context=#{context},
</if>
</set>
where user_id=#{userId}
</update>
<delete
id=
"delete"
>
delete from t_sys_user where user_id=#{userId}
</delete>
<select
id=
"getAll"
resultType=
"com.ces.mybatis.model.User"
>
select user_id,user_name,password,context from t_sys_user order by user_id desc
</select>
<select
id=
"find"
resultType=
"com.ces.mybatis.model.User"
>
select user_id,user_name,password,context from t_sys_user where user_id = #{userId}
</select>
</mapper>
\ No newline at end of file
何素蒸_Mybatis2/src/test/java/com/ces/mybatis/AppTest.java
0 → 100644
View file @
337b5f8b
package
com
.
ces
.
mybatis
;
package
com
.
ces
.
mybatis
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.apache.ibatis.session.ExecutorType
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.apache.ibatis.session.TransactionIsolationLevel
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
com.ces.mybatis.dao.IUserDao
;
import
com.ces.mybatis.model.Context
;
import
com.ces.mybatis.model.User
;
import
com.ces.mybatis.service.IUserService
;
/**
* Unit test for simple App.
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
Application
.
class
)
public
class
AppTest
{
private
static
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
"application.yml"
);
// 注入“Converter”bean
@Autowired
private
IUserService
userService
;
/**
* 1. 自定义TypeHandler类
*/
@Test
public
void
test1
(){
List
<
User
>
user
=
userService
.
getAll
();
System
.
out
.
println
(
user
);
}
/**
* 2. Mybatis 批量插入数据库代码
*/
@Test
public
void
test2
(){
SqlSessionFactory
sqlSessionFactory
=
context
.
getBean
(
"sqlSessionFactory"
,
SqlSessionFactory
.
class
);
SqlSession
sqlSession
=
sqlSessionFactory
.
openSession
(
ExecutorType
.
BATCH
,
TransactionIsolationLevel
.
READ_COMMITTED
);
IUserDao
mapper
=
sqlSession
.
getMapper
(
IUserDao
.
class
);
List
<
User
>
users
=
new
ArrayList
<
User
>();
for
(
int
i
=
0
;
i
<
3
;
i
++){
User
user
=
new
User
();
user
.
setUserId
(
String
.
valueOf
(
i
));
user
.
setUserName
(
"admin"
+
i
);
user
.
setPassword
(
"123"
);
Context
context
=
new
Context
();
context
.
setCity
(
"city"
);
context
.
setProvince
(
"province"
);
context
.
setDistrict
(
"district"
);
user
.
setContext
(
context
);
users
.
add
(
user
);
}
boolean
isSuccess
=
mapper
.
insertOrUpdateByBatch
(
users
);
System
.
out
.
println
(
isSuccess
);
// 刷新
sqlSession
.
flushStatements
();
}
}
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