pom.xml
1
2
3
4
5
|
<
dependency
>
<
groupId
>org.apache.ibatis</
groupId
>
<
artifactId
>ibatis-sqlmap</
artifactId
>
<
version
>2.3.4.726</
version
>
</
dependency
>
|
Spring-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- sqlMapCLient -->
<
bean
id
=
"sqlMapClient"
class
=
"org.springframework.orm.ibatis.SqlMapClientFactoryBean"
>
<
property
name
=
"configLocation"
value
=
"classpath:sqlmap-config.xml"
/>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
<!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
<
bean
id
=
"dataSource"
class
=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<!-- BoneCP type -->
<
property
name
=
"driverClassName"
value
=
"${jdbc.driverClass}"
/>
<
property
name
=
"url"
value
=
"${jdbc.jdbcUrl}"
/>
<
property
name
=
"username"
value
=
"${jdbc.username}"
/>
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
</
bean
>
|
BaseDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package
com.tq365.samples.ibatis.dao;
import
javax.annotation.PostConstruct;
import
javax.annotation.Resource;
import
org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import
com.ibatis.sqlmap.client.SqlMapClient;
/**
* BaseDao,Dao需继承此Dao
* @author archie2010
* since 2011-3-3 下午02:52:36
*/
public
class
BaseDao
extends
SqlMapClientDaoSupport{
@Resource
(name =
"sqlMapClient"
)
private
SqlMapClient sqlMapClient;
@PostConstruct
public
void
initSqlMapClient(){
super
.setSqlMapClient(sqlMapClient);
}
}
|
@PostConstruct注解
在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时
UserDAOimpl.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import
org.springframework.stereotype.Component;
import
com.tq365.samples.ibatis.model.User;
import
com.tq365.sqmples.util.Pages;
/**
* UserDAO接口实现类
* @author archie2010
* since 2011-3-3 下午02:54:57
*/
@Component
(
"userDAO"
)
public
class
UserDAOimpl
extends
BaseDao
implements
UserDAO{
public
User findUserById(
int
id){
return
(User) getSqlMapClientTemplate().queryForObject(
"findUserById"
, id);
}
/**
* 添加User
*/
public
void
addUser(User user) {
getSqlMapClientTemplate().insert(
"addUser"
, user);
}
/**
* 删除User
*/
public
void
deleteUserById(
int
id) {
getSqlMapClientTemplate().delete(
"deleteUserById"
, id);
}
/**
* 查找User
*/
public
User findUser(User user) {
return
(User) getSqlMapClientTemplate().queryForObject(
"findUser"
, user);
}
public
int
getTotalCountByUsername(String username){
return
(Integer) getSqlMapClientTemplate().queryForObject(
"getTotalCountByUsername"
, username);
}
/**
* User分页
*/
@SuppressWarnings
(
"unchecked"
)
public
Pages<User> findUsersByUsername(String username,
int
pageNo) {
Pages<User> pages=
new
Pages<User>(getTotalCountByUsername(username));
pages.setPageSize(
5
);
List<User> userList=
new
ArrayList<User>();
Map<String, Object> map=
new
HashMap<String, Object>();
map.put(
"username"
, username);
map.put(
"start"
, pages.getStart(pageNo));
userList=(List<User>) getSqlMapClientTemplate().queryForObject(
"findUsersByUsername"
, map);
pages.setPageList(userList);
return
pages;
}
/**
* 查询所有记录
* @return
*/
public
int
getTotalCount(){
return
(Integer) getSqlMapClientTemplate().queryForObject(
"getTotalCount"
);
}
@SuppressWarnings
(
"unchecked"
)
public
Pages<User> findAllPageUser(
int
pageNo){
Pages<User> pages=
new
Pages<User>(getTotalCount());
pages.setPageSize(
5
);
List<User> userList=
new
ArrayList<User>();
userList=(List<User>) getSqlMapClientTemplate().queryForList(
"findAllPageUser"
, pages.getStart(pageNo));
pages.setPageList(userList);
return
pages;
}
}
|
本文转自 LinkedKeeper 51CTO博客,原文链接:http://blog.51cto.com/sauron/1271834,如需转载请自行联系原作者