ibaits的入门引用 引用:点击打开链接
步骤如下:
Below dependencies are required for adding iBatis to project.
I am using MySQL as database. So adding database connecting driver support.
For logging purpose, add log4j support.
Step 3) Create new database demoDB in mysql and create a new table USERINFO. We will use this table for data persistence.
Step 4) Create the model class which will map to created table in database. This class is essentially a POJO and it will have exactly one field for corresponding column in database table.
package com.howtodoinjava.ibatis.demo.dto;
public class UserTEO
{
private Integer id;
private String name;
private String email;
private String password;
private int status;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Step 5) Write Dao layer
This will be implemented using an interface UserDao.java and implementation class UserDaoIbatis.java. It can be different names based on your choice. I am not writing code for UserDao.java due to space constraint but it is available in attached source code. UserDaoIbatis.java is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package
com.howtodoinjava.ibatis.demo.dao;
import
com.howtodoinjava.ibatis.demo.dto.UserTEO;
import
com.ibatis.sqlmap.client.SqlMapClient;
public
class
UserDaoIbatis
implements
UserDao
{
@Override
public
UserTEO addUser(UserTEO user, SqlMapClient sqlmapClient) {
try
{
Integer id = (Integer)sqlmapClient.queryForObject(
"user.getMaxId"
);
id = id ==
null
?
1
: id +
1
;
user.setId(id);
user.setStatus(
1
);
sqlmapClient.insert(
"user.addUser"
, user);
user = getUserById(id, sqlmapClient);
return
user;
}
catch
(Exception e)
{
e.printStackTrace();
}
return
null
;
}
@Override
public
UserTEO getUserById(Integer id, SqlMapClient sqlmapClient) {
try
{
UserTEO user = (UserTEO)sqlmapClient.queryForObject(
"user.getUserById"
, id);
return
user;
}
catch
(Exception e)
{
e.printStackTrace();
}
return
null
;
}
@Override
public
void
deleteUserById(Integer id, SqlMapClient sqlmapClient) {
try
{
sqlmapClient.delete(
"user.deleteUserById"
, id);
}
catch
(Exception e)
{
e.printStackTrace();
}
}
}
|
Step 6) Write sqlmaps and sqlmap config
This is the main part. In sqlmap config, we need to give config settings like databse connection properties and path to sqlmaps where we write real sql queries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
<
sqlMapConfig
>
<
settings
useStatementNamespaces
=
"true"
/>
<
transactionManager
type
=
"JDBC"
>
<
dataSource
type
=
"SIMPLE"
>
<
property
name
=
"JDBC.Driver"
value
=
"com.mysql.jdbc.Driver"
/>
<
property
name
=
"JDBC.Username"
value
=
"root"
/>
<
property
name
=
"JDBC.Password"
value
=
"root"
/>
</
dataSource
>
</
transactionManager
>
<
sqlMap
resource
=
"user.xml"
/>
</
sqlMapConfig
>
|
Lets write sqlmap with queries in it and map the result to UserTEO.java.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<
sqlMap
namespace
=
"user"
>
<
typeAlias
alias
=
"USER"
type
=
"com.howtodoinjava.ibatis.demo.dto.UserTEO"
/>
<
resultMap
id
=
"userResultMap"
class
=
"USER"
>
<
result
property
=
"id"
column
=
"ID"
/>
<
result
property
=
"name"
column
=
"NAME"
/>
<
result
property
=
"email"
column
=
"EMAIL"
/>
<
result
property
=
"password"
column
=
"PASSWORD"
/>
<
result
property
=
"status"
column
=
"STATUS"
/>
</
resultMap
>
<
select
id
=
"getUserById"
parameterClass
=
"java.lang.Integer"
resultMap
=
"userResultMap"
>
SELECT * FROM USERINFO WHERE ID = #value#
</
select
>
<
select
id
=
"getMaxId"
resultClass
=
"java.lang.Integer"
>
SELECT MAX(ID) FROM USERINFO
</
select
>
<
insert
id
=
"addUser"
parameterClass
=
"USER"
>
INSERT INTO USERINFO (ID,NAME,EMAIL,PASSWORD,STATUS)
VALUES(#id#,#name#,#email#,#password#,#status#);
</
insert
>
<
delete
id
=
"deleteUserById"
parameterClass
=
"java.lang.Integer"
>
DELETE FROM USERINFO WHERE ID = #value#
</
delete
>
</
sqlMap
>
|
Step 7) Test the code
To test the code, we need to create sqlmapClient which will act as EntityManager in JPA. It will connect to database and execute the required queries and update the result back to data mappers.
SQLMapClient can be constructed in different ways in different frameworks, but as this is hello world application, I am creating it using code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package
com.howtodoinjava.ibatis.demo;
import
java.io.Reader;
import
com.howtodoinjava.ibatis.demo.dao.UserDao;
import
com.howtodoinjava.ibatis.demo.dao.UserDaoIbatis;
import
com.howtodoinjava.ibatis.demo.dto.UserTEO;
import
com.ibatis.common.resources.Resources;
import
com.ibatis.sqlmap.client.SqlMapClient;
import
com.ibatis.sqlmap.client.SqlMapClientBuilder;
public
class
TestMain {
public
static
void
main(String[] args)
throws
Exception
{
//Initialize dao
UserDao manager =
new
UserDaoIbatis();
//Create the SQLMapClient
Reader reader = Resources.getResourceAsReader(
"sql-maps-config.xml"
);
SqlMapClient sqlmapClient = SqlMapClientBuilder.buildSqlMapClient (reader);
//Create a new user to persist
UserTEO user =
new
UserTEO();
user.setId(
1
);
user.setName(
"Demo User"
);
user.setPassword(
"password"
);
user.setEmail(
"demo-user@howtodoinjava.com"
);
user.setStatus(
1
);
//Add the user
manager.addUser(user,sqlmapClient);
//Fetch the user detail
UserTEO createdUser = manager.getUserById(
1
, sqlmapClient);
System.out.println(createdUser.getEmail());
//Lets delete the user
manager.deleteUserById(
1
, sqlmapClient);
}
}
|
Running above main method will execute the sql command and this can be verified using logs generated.