zoukankan      html  css  js  c++  java
  • 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>

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

  • 相关阅读:
    spring整合websocket通信
    Log4j学习
    Elasticsearch学习之ES节点类型以及各种节点的分工
    Elasticsearch 学习之提升性能小贴士
    Python奇技淫巧
    汉语拼音转换工具(Python 版)
    根据数据库的表结构的类型返回其对应的简写类型
    python的动态加载机制??
    计算二进制中的字符串的长度
    一个erlang游戏服务端
  • 原文地址:https://www.cnblogs.com/marrycode/p/11801700.html
Copyright © 2011-2022 走看看