关于Dao层和Service层

Dao层和Service层的区别

传统访问数据库的方法非常面向过程,分为以下几步:
– 实例化connection
– 实例化statement

– 通过statement的参数sql语句访问数据库,返回数据进行处理
例子:


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
import ...
public class DBAccess {

public static void main(String[] args) throws SQLException, FileNotFoundException, IOException
{
DBAccess access = new DBAccess();
access.test();
}

private void test() throws SQLException, FileNotFoundException, IOException
{
String url = "jdbc:postgresql://localhost:5432/rylynn";

Properties p = new Properties();
p.load(new FileInputStream("reg.txt"));

Connection connection = DriverManager.getConnection(url,p); //建立connection
Statement statement = connection.createStatement(); //建立satatement
statement.execute("insert into abo values((001),'hnb')"); //执行sql语句

ResultSet resultSet = statement.executeQuery("select number from abo where number < 2");
while(resultSet.next())
{
int id = resultSet.getInt(1);
// String name = resultSet.getString(1);
System.out.println("ID:" + id);
}
statement.close();
connection.close();
}
}

传统数据库访问模式缺点显而易见:
一就是各个模块间的耦合太紧,statement要依赖connection,connection还依赖于数据库的种类。
二就是如果我改变的数据库的种类,或者要提供不同的数据库服务,那么我就要提供大量的重复代码。


Dao层

  • dao层叫数据访问层,全称为data access object,属于一种比较底层,比较基础的操作,具体到对于某个表、某个实体的增删改查。

以对于user的操作为例进行说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface UserDao {

public List<User> findAll();

public User findById(String id);

public void update(User user);

public void add(User user);

public void delete(String id);

public User findByIdAndPassword(@Param("id") String username, @Param("password") String password);

public void updatePassword(@Param("userId") String id, @Param("password") String password);

User findByUsername(String username);
}


在接口中对方法进行了定义,在UserDao.xml中给出了sql语句实现
在UserDao中,就对user这个实体的增删补查各类基本的操作进行了声明,并用mybatis框架进行实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<select id="findAll" resultMap="user_map">
SELECT * FROM user WHERE user_id != 'admin'
</select>

<select id="findById" parameterType="String" resultMap="user_map">
SELECT * FROM user WHERE user_id = #{value}
</select>

<update id="update" parameterType="User">
UPDATE user SET password = #{password} ,authority = #{authority} WHERE user_id = #{userId}
</update>

<update id="updatePassword" parameterType="map">
UPDATE user SET password = #{password} WHERE user_id = #{userId}
</update>

<insert id="add" parameterType="User">
INSERT INTO user(user_id,password,salt,role_ids,locked) VALUES(#{userId},#{password},#{salt},#{roleIdsStr},#{locked})
</insert>

<select id="findByIdAndPassword" parameterType="map" resultMap="user_map">
SELECT * FROM user WHERE user_id = #{id} AND password = #{password}
</select>

Service层

service层叫服务层,被称为服务,它是将几种操作封装起来。
service层要使用接口来定义有以下几点好处:

  1. service实现类实现了service接口,进行具体的业务操作
  2. java中接口是多继承的,如果你需要一个类实现多个service,你用接口可以实现。
  3. 要提供不同的数据库的服务时,我们只需要面对接口用不同的类实现即可,而不需要重复地定义类编程规范问题,接口化的编程为的是将实现封装起来,而调用时只需要关心接口而不用关心实现,也就是“高内聚,低耦合”的思想。
    以对于user的操作为例进行说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import ...
public interface UserBiz {
public List<UserVo> findAll() throws InvocationTargetException, IllegalAccessException;

public User findById(String id);

public void update(User user);

public void add(User user);

public void delete(String id);

public void changePassword(String userId, String newPassword);


public User findByUsername(String username);

public Set<String> findRoles(String username);

public Set<String> findPermissions(String username);
}


  • service层里面的方法相较于dao层中的方法进行了一层包装,例如通过id查找用户,通过用户名查找用户,是在基础的操作上又增加了一层包装的,实现的是相对高级的操作。最后将这些操作在serviceimpl类中实现,

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
import ...
@Service
public class UserBizImpl implements UserBiz {

@Resource
UserDao userDao;

@Resource
RoleDao roleDao;

@Resource
StaffDao staffDao;

@Resource
private PasswordHelper passwordHelper;
@Resource(name = "roleBizImpl")
private RoleBiz roleBiz;

@Override
public List<UserVo> findAll() throws InvocationTargetException, IllegalAccessException {
List<UserVo> userVoList = new ArrayList<>();
List userList = userDao.findAll();


Iterator iterator = userList.iterator();

while (iterator.hasNext()) {
StringBuilder s = new StringBuilder();
User user = (User) iterator.next();
List<Long> roleIds = user.getRoleIds();

UserVo userVo = new UserVo();
BeanUtils.copyProperties(userVo, user);

if (roleIds != null) {
int i = 0;
int size = roleIds.size();
for (; i < size - 1; i++) {
Role role = roleDao.findOne(roleIds.get(i));

s.append(role.getDescription());
s.append(",");
}
Role role = roleDao.findOne(roleIds.get(i));
s.append(role.getDescription());
userVo.setRoleIdsStr(s.toString());
}


userVoList.add(userVo);

}

return userVoList;
}


总结

这样进行分层,访问数据库和进行service之间的分工明确,如果我需要对service的需求修改,无需改变dao层的代码,只要在实现上改变即可,如果我有访问数据库的新需求,那我也只要在dao层的代码中增添即可。