spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 集成通用maper,与分页插件:

spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 集成通用maper,与分页插件:


  • spring boot 142RELEASEThymeleafmybatis 集成通用maper与分页插件
    • pomxml
    • UserPojo
    • UserService
    • UserController
    • 前台模板页

pom.xml

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.2.RELEASEversion>
        <relativePath /> 
    parent>

    <properties>
        <java.version>1.8java.version>
        
        <mybatis.version>3.4.0mybatis.version>
        <mybatis.spring.version>1.3.0mybatis.spring.version>
        <mapper.version>3.3.9mapper.version>
        <pagehelper.version>4.1.6pagehelper.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatypegroupId>
            <artifactId>jackson-datatype-jodaartifactId>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.modulegroupId>
            <artifactId>jackson-module-parameter-namesartifactId>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.11version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>${mybatis.spring.version}version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.2version>
            <scope>compilescope>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>${pagehelper.version}version>
        dependency>
        
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapperartifactId>
            <version>${mapper.version}version>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>springloadedartifactId>
                        <version>1.2.5.RELEASEversion>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>
    
    
    
    
  • 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
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116

UserPojo

@Table(name="user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


    private String username;

    private Date birthday;

    private String email;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }




}

    
    
    
    
  • 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

UserService


@Service
@Transactional
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(readOnly = true)
    public PageInfo findAll(String username, Date birthday, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        Example example = new Example(User.class);
        if (!StringUtils.isEmpty(username)) {
            example.createCriteria().andEqualTo("username", username);
        }
        if (birthday != null) {
            example.createCriteria().andEqualTo("birthday", birthday);
        }
        List users = userMapper.selectByExample(example);
        return new PageInfo<>(users);

    }

}

    
    
    
    
  • 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

UserController

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    public String index(Model model, @RequestParam(required = false) String username,@RequestParam(required = false) Date birthday, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
        PageInfo pageInfo = userService.findAll(username, birthday, pageNum, pageSize);
        //获得当前页
        model.addAttribute("pageNum", pageInfo.getPageNum());
        //获得一页显示的条数
        model.addAttribute("pageSize", pageInfo.getPageSize());
        //是否是第一页
        model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
        //获得总页数
        model.addAttribute("totalPages", pageInfo.getPages());
        //是否是最后一页
        model.addAttribute("isLastPage", pageInfo.isIsLastPage());
        model.addAttribute("users", pageInfo.getList());

        return "index";
    }
}
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

前台模板页

<table class="table table-hover" style="border-collapse: 0"
            width="50%">
            <thead>
                <tr>
                    <th>idth>
                    <th>usernameth>
                    <th>birthdayth>
                    <th>emailth>
                tr>
            thead>
            <tr th:each="user : ${users}">
                <th th:text="${user.id}">th>
                <th th:text="${user.username}">th>
                <th th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">th>
                <th th:text="${user.email}">th>
            tr>
        table>
        <nav>
            <ul class="pagination">
                <li><a href="">«a>li>

                <li>
                    <a th:if="${not isFirstPage}" th:href="@{${'/user'}(pageNum=${pageNum-1},pageSize=${pageSize})}">Previousa>
                    <a th:if="${isFirstPagee}" href="javascript:void(0);">Previousa>
                li>

                <li th:each="pageNo : ${#numbers.sequence(1, totalPages)}">
                    <a th:if="${pageNum eq pageNo}" href="javascript:void(0);">
                         <span th:text="${pageNo}">span>
                    a>

                    <a th:if="${not (pageNum eq pageNo)}" th:href="@{${'/user'}(pageNum=${pageNo},size=${pageSize})}">
                        <span th:text="${pageNo}">span>
                    a>
                li>
                <li>
                        <a th:if="${not isLastPage}" th:href="@{${'/user'}(pageNum=${pageNum+1},size=${pageSize})}">Nexta>
                        <a th:if="${isLastPage}" href="javascript:void(0);">Nexta>
                    li>

                <li><a href="#">»a>li>
            ul>
        nav>

你可能感兴趣的:(Java,spring-boot)