首页 > 编程语言 > 详细

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

时间:2019-11-05 21:26:48      阅读:71      评论:0      收藏:0      [点我收藏+]

添加依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</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 boot整合MyBatis+freemarker的简单案例

原文:https://www.cnblogs.com/marrycode/p/11801700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!