您的位置:首页 > 其它

MyBatis 配置及实现 CURD 操作

2013-06-05 10:47 495 查看
本文摘抄自网络

MyBatis的前身就是iBatis。是一个数据持久层(ORM)框架。

每个MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。SqlSessionFactoryBuilder可以从一个xml配置文件或者一个预定义的配置类的实例获得。

一、前提条件

建立表:admin_login_id

数据库结构

intlog_id主键

intadmin_id

varcharlogin_timedomain:

查看源码

打印?

01
package
com;
02
03
import

java.util.Date;
04
05
/**
06
*@function:
07
*@author:jy
08
*@company:万里网
09
*@date:2011-7-31
10
*/
11
public
class
Logs{
12
public

int
log_id;
13
public

int
admin_id;
14
public

Datelogin_time;
15
16
public

int
getLog_id(){

17
return

log_id;
18
}

19
public

void
setLog_id(
int

log_id){
20
this
.log_id=log_id;
21
}

22
public

int
getAdmin_id(){

23
return

admin_id;
24
}

25
public

void
setAdmin_id(
int

admin_id){
26
this
.admin_id=admin_id;
27
}

28
public

DategetLogin_time(){
29
return

login_time;
30
}

31
public

void
setLogin_time(Datelogin_time){
32
this
.login_time=login_time;
33
}

34
@Override
35
public

StringtoString(){
36
return

"Logs[log_id="
+log_id+
",admin_id="
+admin_id+
",login_time="

+login_time+
"]"
;
37
}

38
39
}
二、下面开始配置MyBatis

1.MyBatis总配置文件

查看源码

打印?

01
<?
xml

version
=
"1.0"

encoding
=
"UTF-8"

?>
02
<!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN"
03
"http://mybatis.org/dtd/mybatis-3-config.dtd">
04
05
<
configuration
>
06
<
typeAliases
>
07
<
typeAlias

alias
=
"Logs"

type
=
"com.Logs"
/>
08
</
typeAliases
>
09
<
environments

default
=
"development"
>
10
<
environment

id
=
"development"
>
11
<
transactionManager

type
=
"JDBC"
/>
12
<
dataSource

type
=
"POOLED"
>
13
<
property

name
=
"driver"

value
=
"com.mysql.jdbc.Driver"
/>
14
<
property

name
=
"url"

value
=
"jdbc:mysql://localhost:3306/manage?characterEncoding=UTF-8"
/>
15
<
property

name
=
"username"

value
=
"root"
/>
16
<
property

name
=
"password"

value
=
""
/>
17
</
dataSource
>
18
</
environment
>
19
</
environments
>
20
21
<
mappers
>
22
<
mapper

resource
=
"com/LogsMapper.xml"

/>
23
</
mappers
>
24
</
configuration
>
2.实体映射文件

查看源码

打印?

01
<?
xml

version
=
"1.0"

encoding
=
"UTF-8"
?>
02
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN"
03
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
04
05
<
mapper

namespace
=
"com.LogsDAO"

>
06
07
<
select

id
=
"queryAllLogs"

resultType
=
"com.Logs"
>
08
SELECT
09
log_id,
10
admin_id,
11
login_time
12
FROMadmin_login_log
13
</
select
>
14
15
<
select

id
=
"queryStudentById"

resultType
=
"Logs"

parameterType
=
"int"
>
16
SELECT
17
log_id,
18
admin_id,
19
login_time
20
FROMadmin_login_log
21
WHERE
22
log_id=#{log_id}
23
</
select
>
24
25
<
insert

id
=
"addLog"

parameterType
=
"Logs"
>
26
INSERTINTOadmin_login_log
27
(log_id,admin_id,login_time)
28
VALUES
29
(log_id,#{admin_id},#{login_time})
30
</
insert
>
31
32
<
delete

id
=
"deleteLogById"

parameterType
=
"int"
>
33
DELETEFROM
34
admin_login_log
35
WHERE
36
log_id=#{log_id}
37
</
delete
>
38
39
<
select

id
=
"queryLogsByAdminId"

parameterType
=
"string"

resultType
=
"Logs"
>
40
SELECT
41
log_id,
42
admin_id,
43
login_time
44
FROM
45
admin_login_log
46
WHERE
47
admin_idlike#{admin_id}
48
</
select
>
49
</
mapper
>
三、建立DAO接口

查看源码

打印?

01
package
com;
02
03
import

java.util.List;
04
05
/**
06
*@function:
07
*@author:jy
08
*@company:万里网
09
*@date:2011-7-31
10
*/
11
public
interface
LogsDAO{
12
public

void
addLog(Logslog);
13
14
public

void
deleteLogById(
int

id);
15
16
public

void
updateStudentById(Logslog);
17
18
public

List<Logs>queryAllLogs();
19
20
public

LogsqueryStudentById(
int

id);
21
22
public

List<Logs>queryLogsByAdminId(Stringadmin_id);
23
}
四、建立SqlSessionFactory实例,并实现LogsDAO接口

查看源码

打印?

01
package
com;
02
03
import

java.io.IOException;
04
import

java.io.Reader;
05
import

java.util.List;
06
07
import

org.apache.ibatis.io.Resources;
08
import

org.apache.ibatis.session.SqlSession;
09
import

org.apache.ibatis.session.SqlSessionFactory;
10
import

org.apache.ibatis.session.SqlSessionFactoryBuilder;
11
12
/**
13
*@function:
14
*@author:jy
15
*@company:万里网
16
*@date:2011-7-31
17
*/
18
public
class
LogsDAOImpl
implements

LogsDAO{
19
private

static
SqlSessionsession=
null
;
20
private

static
LogsDAOmapper=
null
;
21
22
static
{
23
Stringresoure=
"com/ibatisConfiguration.xml"
;
24
Readerreader=
null
;
25
try

{
26
reader=Resources.getResourceAsReader(resoure);
27
}
catch
(IOExceptione){
28
e.printStackTrace();
29
}
30
31
SqlSessionFactorysqlSessionFactory=
new
SqlSessionFactoryBuilder().build(reader);
32
session=sqlSessionFactory.openSession();
33
mapper=session.getMapper(LogsDAO.
class
);
34
35
try

{
36
reader.close();
37
}
catch
(IOExceptione){
38
e.printStackTrace();
39
}
40
}

41
42
/*(non-Javadoc)
43
*@seecom.LogsDAO#addLog(com.Logs)
44
*/
45
@Override
46
publicvoidaddLog(Logslog){
47
mapper.addLog(log);
48
49
session.commit();
50
}
51
52
/*(non-Javadoc)
53
*@seecom.LogsDAO#deleteLogById(int)
54
*/
55
@Override
56
publicvoiddeleteLogById(intid){
57
mapper.deleteLogById(id);
58
59
session.commit();
60
}
61
62
/*(non-Javadoc)
63
*@seecom.LogsDAO#updateStudentById(com.Logs)
64
*/
65
@Override
66
publicvoidupdateStudentById(Logslog){
67
68
69
}
70
71
/*(non-Javadoc)
72
*@seecom.LogsDAO#queryAllLogs()
73
*/
74
@Override
75
publicList<Logs>queryAllLogs(){
76
List<Logs>logs=mapper.queryAllLogs();
77
78
returnlogs;
79
}
80
81
/*(non-Javadoc)
82
*@seecom.LogsDAO#queryStudentById(int)
83
*/
84
@Override
85
publicLogsqueryStudentById(intid){
86
Logslogs=mapper.queryStudentById(id);
87
88
returnlogs;
89
}
90
91
/*(non-Javadoc)
92
*@seecom.LogsDAO#queryLogsByAdminId(java.lang.String)
93
*/
94
@Override
95
public

List<Logs>queryLogsByAdminId(Stringadmin_id){

96
return

mapper.queryLogsByAdminId(admin_id);
97
}

98
99
}
五、测试

查看源码

打印?

01
package

test;
02
03
import

java.io.IOException;
04
05
import

com.Logs;
06
import

com.LogsDAOImpl;
07
08
/**
09
*@function:
10
*@author:jy
11
*@company:万里网
12
*@date:2011-7-31
13
*/
14
public
class
Test{
15
16
/**
17
*@paramargs
18
*@throwsIOException
19
*/
20
public

static
void

main(String[]args)
throws

IOException{
21
//TODOAuto-generatedmethodstub
22
LogsDAOImpllogs=
new
LogsDAOImpl();
23
24
//System.out.println(logs.queryAllLogs());
25
26
//Logslog=logs.queryStudentById(2);
27
28
//System.out.println(log);

29
30
31
//addlog
32
Logslog=
new
Logs();

33
34
log.setAdmin_id(
1
);
35
36
log.setLog_id(
10
);
37
38
log.setLogin_time(
new

java.util.Date());
39
40
logs.addLog(log);
41
42
//deleteLog
43
logs.deleteLogById(
5
);
44
45
System.out.println(logs.queryLogsByAdminId(
"%1%"
));
46
}

47
48
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: