spring boot整合MyBatis+freemarker的简单案例

添加依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> 

配置数据源信息application.yml:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: toor
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: true

编写pojo对象,mapper接口,mapper映射文件:

pojo:

import lombok.Data;

import javax.persistence.*;

@Entity
@Table(name = "user")
@Data
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)// 主键自增长
    private Integer id;
    private String username;
    private String password;
    private String name;
}

mapper接口:

import com.hui.entity.User;

import java.util.List;

public interface UserMapper {
    List<User> getUserList();
}

mapper映射文件:

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hui.mapper.UserMapper">
    <select id="getUserList" resultType="com.hui.entity.User">
        select * from user
    </select>
</mapper>

配置MyBatis包扫描信息:

在主启动类上添加@MapperScan

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan({"com.hui.mapper"})
public class MainClass {

    public static void main(String[] args) {

        SpringApplication.run(MainClass.class,args);
    }
}

编写Controller以测试代码:

import com.hui.dao.UserDao;
import com.hui.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/page")
public class PageController {

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String getUserList(Model model) {
        List<User> userList = userDao.findAll();
        model.addAttribute("userList",userList);
        return "index";
    }
}

index.ftl模板信息:

<html>
<head>
    <title>UserList</title>
</head>
<body>
    <table border="1px" cellspacing="0" cellpadding="0" width="100%">
        <thead>
            <tr>
                <th>id</th>
                <th>用户名</th>
                <th>密码</th>
                <th>姓名</th>
            </tr>
        </thead>
        <tbody>
            <#list userList as user>
                <tr style="text-align: center">
                    <td>${user.id}</td>
                    <td>${user.username}</td>
                    <td>${user.password}</td>
                    <td>${user.name}</td>
                </tr>
            </#list>
        </tbody>
    </table>
</body>
</html>

请求后台接口,查看效果如下:

 

 

 

posted @ 2019-11-05 21:18  LadyGodiva  阅读(610)  评论(0编辑  收藏  举报