四层架构实现具体流程

依据上篇进行

1.UserMapper.xml编写sql语句

<?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.example.pagedemo.dao.UserMapper">
    <!--    建立关联-->
    <resultMap id="userResult" type="user">
        <result column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="passwd" property="passwd"/>
        <result column="create_date" property="createDate"/>
    </resultMap>

    <sql id="sql_u">
        select id, user_name, passwd, create_date
        from t_user
    </sql>
    <select id="findAll" resultMap="userResult">
        <include refid="sql_u"/>
    </select>
    //分页
    <select id="findPages" resultMap="userResult">
        <include refid="sql_u"/> limit #{pageIndex},#{pageSize}
    </select>
</mapper>

2.UserMapper.java引用上1

public interface UserMapper {
    List<User> findAll();
    List<User> findPages(int pageIndex,int pageSize);
}

3.IUserService定义抽象方法

public interface IUserService {
    List<User> queryUserList();
    List<User> queryUserPages(int pageIndex,int pageSize);
}

4.UserServiceImpl实现类

@Service
public class UserServiceImpl implements IUserService{
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public List<User> queryUserList() {
        List<User> all = userMapper.findAll();
        return all;
    }

    @Override
    public List<User> queryUserPages(int pageIndex,int pageSize) {
        List<User> pages = userMapper.findPages((pageIndex-1)*pageSize,pageSize);
        return pages;
    }

}

5.UserController

@RestController
@RequestMapping("/user")//二级界面
public class UserController {

    @Autowired(required = false)
    private IUserService iUserService;

    @RequestMapping("/all")
    public List<User> findAll(){
        return iUserService.queryUserList();
    }
    @RequestMapping("/pages")
    public List<User> Pages(int pageIndex,int pageSize){
        return iUserService.queryUserPages(pageIndex,pageSize);
    }
}

你可能感兴趣的:(架构,java,开发语言)