Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybatis_self
Project
Project
Details
Activity
Releases
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
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
刘凯
mybatis_self
Commits
b670cd8e
Commit
b670cd8e
authored
Feb 25, 2020
by
刘凯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
b8ac6908
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
592 additions
and
365 deletions
+592
-365
pom.xml
pom.xml
+383
-365
Main.java
src/main/java/com/liukai/demo/Main.java
+44
-0
MyStringHandler.java
src/main/java/com/liukai/demo/MyStringHandler.java
+50
-0
Role.java
src/main/java/com/liukai/demo/Role.java
+36
-0
RoleMapper.java
src/main/java/com/liukai/demo/RoleMapper.java
+11
-0
RoleMapper.xml
src/main/java/com/liukai/demo/RoleMapper.xml
+31
-0
log4j.properties
src/main/resources/log4j.properties
+5
-0
mybatis-config.xml
src/main/resources/mybatis-config.xml
+32
-0
No files found.
pom.xml
View file @
b670cd8e
This diff is collapsed.
Click to expand it.
src/main/java/com/liukai/demo/Main.java
0 → 100644
View file @
b670cd8e
package
com
.
liukai
.
demo
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder
;
import
java.io.IOException
;
import
java.io.InputStream
;
/**
* @author liukai
* @Description: TODO
* @date 2020/2/254:41 下午
*/
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
String
resource
=
"mybatis-config.xml"
;
InputStream
inputStream
=
null
;
try
{
inputStream
=
Resources
.
getResourceAsStream
(
resource
);
}
catch
(
IOException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
SqlSessionFactory
sqlSessionFactory
=
null
;
sqlSessionFactory
=
new
SqlSessionFactoryBuilder
().
build
(
inputStream
);
SqlSession
sqlSession
=
null
;
try
{
sqlSession
=
sqlSessionFactory
.
openSession
();
RoleMapper
roleMapper
=
sqlSession
.
getMapper
(
RoleMapper
.
class
);
Role
role
=
roleMapper
.
getRole
(
1L
);
System
.
out
.
println
(
role
.
getId
()+
":"
+
role
.
getRoleName
()+
":"
+
role
.
getNote
());
sqlSession
.
commit
();
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
sqlSession
.
rollback
();
e
.
printStackTrace
();
}
finally
{
sqlSession
.
close
();
}
}
}
src/main/java/com/liukai/demo/MyStringHandler.java
0 → 100644
View file @
b670cd8e
package
com
.
liukai
.
demo
;
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
org.apache.log4j.Logger
;
import
java.sql.CallableStatement
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
/**
* @author liukai
* @Description: TODO
* @date 2020/2/254:41 下午
*/
@MappedTypes
({
String
.
class
})
@MappedJdbcTypes
(
JdbcType
.
VARCHAR
)
public
class
MyStringHandler
implements
TypeHandler
<
String
>
{
private
Logger
log
=
Logger
.
getLogger
(
MyStringHandler
.
class
);
@Override
public
String
getResult
(
ResultSet
rs
,
String
colName
)
throws
SQLException
{
log
.
info
(
"使用我的TypeHandler,ResultSet列名获取字符串"
);
return
rs
.
getString
(
colName
);
}
@Override
public
String
getResult
(
ResultSet
rs
,
int
index
)
throws
SQLException
{
log
.
info
(
"使用我的TypeHandler,ResultSet下标获取字符串"
);
return
rs
.
getString
(
index
);
}
@Override
public
String
getResult
(
CallableStatement
cs
,
int
index
)
throws
SQLException
{
log
.
info
(
"使用我的TypeHandler,CallableStatement下标获取字符串"
);
return
cs
.
getString
(
index
);
}
@Override
public
void
setParameter
(
PreparedStatement
ps
,
int
index
,
String
value
,
JdbcType
arg3
)
throws
SQLException
{
log
.
info
(
"使用我的TypeHandler"
);
ps
.
setString
(
index
,
value
);
}
}
src/main/java/com/liukai/demo/Role.java
0 → 100644
View file @
b670cd8e
package
com
.
liukai
.
demo
;
/**
* @author liukai
* @Description: TODO
* @date 2020/2/254:44 下午
*/
public
class
Role
{
private
long
id
;
private
String
roleName
;
private
String
note
;
public
long
getId
()
{
return
id
;
}
public
void
setId
(
long
id
)
{
this
.
id
=
id
;
}
public
String
getRoleName
()
{
return
roleName
;
}
public
void
setRoleName
(
String
roleName
)
{
this
.
roleName
=
roleName
;
}
public
String
getNote
()
{
return
note
;
}
public
void
setNote
(
String
note
)
{
this
.
note
=
note
;
}
}
src/main/java/com/liukai/demo/RoleMapper.java
0 → 100644
View file @
b670cd8e
package
com
.
liukai
.
demo
;
/*
* @author gethin
*/
public
interface
RoleMapper
{
public
Role
getRole
(
Long
id
);
public
Role
findRole
(
String
roleName
);
public
int
deleteRole
(
Long
id
);
public
int
insertRole
(
Role
role
);
}
src/main/java/com/liukai/demo/RoleMapper.xml
0 → 100644
View file @
b670cd8e
<?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.liukai.demo.RoleMapper"
>
<resultMap
type=
"role"
id=
"roleMap"
>
<id
column=
"id"
property=
"id"
javaType=
"long"
jdbcType=
"BIGINT"
/>
<result
column=
"role_name"
property=
"roleName"
javaType=
"string"
jdbcType=
"VARCHAR"
/>
<result
column=
"note"
property=
"note"
typeHandler=
"com.liukai.demo.MyStringHandler"
/>
</resultMap>
<select
id=
"getRole"
parameterType=
"long"
resultMap=
"roleMap"
>
select
id,role_name as roleName,note from role where id=#{id}
</select>
<select
id=
"findRole"
parameterType=
"long"
resultMap=
"roleMap"
>
select
id,role_name,note from role where role_name like CONCAT('%',#{roleName
javaType=string,
jdbcType=VARCHAR,typeHandler=com.liukai.demo.MyStringHandler},'%')
</select>
<insert
id=
"insertRole"
parameterType=
"role"
>
insert into
role(role_name,note) value(#{roleName},#{note})
</insert>
<delete
id=
"deleteRole"
parameterType=
"long"
>
delete from role where
id=#{id}
</delete>
</mapper>
\ No newline at end of file
src/main/resources/log4j.properties
0 → 100644
View file @
b670cd8e
log4j.rootLogger
=
DEBUG,stdout
log4j.logger.org.mybatis
=
DUBUG
log4j.appender.stdout
=
org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
=
org.apache.log4j.PatternLayout
log4j.appender.logDailyFile.layout.ConversionPattern
=
%5p %d %C:%m%n
src/main/resources/mybatis-config.xml
0 → 100644
View file @
b670cd8e
<?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>
<!--<settings>
<!– <setting name="lazyLoadingEnabled" value="true"/>–>
<!– <setting name="aggressiveLazyLoading" value="false"/> –>
</settings>-->
<typeAliases>
<typeAlias
alias=
"role"
type=
"com.liukai.demo.Role"
/>
</typeAliases>
<typeHandlers>
<typeHandler
jdbcType=
"VARCHAR"
javaType=
"string"
handler=
"com.liukai.demo.MyStringHandler"
/>
</typeHandlers>
<!-- 定义数据库的信息,默认使用development数据库构建环境 -->
<environments
default=
"development"
>
<environment
id=
"development"
>
<transactionManager
type=
"JDBC"
/>
<dataSource
type=
"POOLED"
>
<property
name=
"driver"
value=
"com.mysql.jdbc.Driver"
/>
<property
name=
"url"
value=
"jdbc:mysql://dev.config.duibar.com:3306/test"
/>
<property
name=
"username"
value=
"dev"
/>
<property
name=
"password"
value=
"dev_fas015"
/>
</dataSource>
</environment>
</environments>
<!-- 定义映射器 -->
<mappers>
<package
name=
"com.liukai.demo"
/>
</mappers>
</configuration>
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