SpringBoot(二):集成Mybatis

1、先导入mybatis依赖(如果之前钩记了就不用导了)

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

2、在application.yml加入mybatis配置

mybatis:
  mapper-locations: classpath:mapping文件包名/*.xml
  type-aliases-package: 实体类包的全路径名

3、数据库建表

CREATE TABLE tb_user(
USER_ID INT PRIMARY KEY auto_increment, # 用户id
USER_NAME VARCHAR(20)  #用户姓名
);

4、创建实体类(User)

package club.djkplay.springboot2.pojo;

public class User {
    private Integer user_Id;

    private String user_Name;

    public User() {
    }

    public User(Integer user_Id) {
        this.user_Id = user_Id;
    }

    public User(Integer user_Id, String user_Name) {
        this.user_Id = user_Id;
        this.user_Name = user_Name;
    }

    public Integer getUser_Id() {
        return user_Id;
    }

    public void setUser_Id(Integer user_Id) {
        this.user_Id = user_Id;
    }

    public String getUser_Name() {
        return user_Name;
    }

    public void setUser_Name(String user_Name) {
        this.user_Name = user_Name;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_Id=" + user_Id +
                ", user_Name='" + user_Name + '\'' +
                '}';
    }
}

5、创建mapping文件(UserMapper.xml)

<?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="club.djkplay.springboot2.dao.UserMapper">
  <resultMap id="BaseResultMap" type="club.djkplay.springboot2.pojo.User">
    <id column="USER_ID" jdbcType="INTEGER" property="userId" />
    <result column="USER_NAME" jdbcType="VARCHAR" property="userName" />
  </resultMap>
  <sql id="Base_Column_List">
    USER_ID, USER_NAME
  </sql>

  <select id="selectAllUser" resultType="user">
    select * from tb_user
  </select>
  
</mapper>

6、创建接口(UserMapper)

package club.djkplay.springboot2.dao;

import club.djkplay.springboot2.pojo.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userMapper")
public interface UserMapper {

    List<User> selectAllUser();
}

7、创建controller控制器测试(TestController)

package club.djkplay.springboot2.controller;

import club.djkplay.springboot2.dao.UserMapper;
import club.djkplay.springboot2.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class TestController {

    @Resource
    private UserMapper userMapper;

    @RequestMapping("select")
    public ModelAndView adduser(){
        ModelAndView mv = new ModelAndView();
        List<User> list = userMapper.selectAllUser();
           mv.addObject("list",list);
            mv.setViewName("success");
            return mv;
    }
}

8、创建ftl静态测试页面(Test.ftl)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>

<form action="add" method="post">
    <input type="submit" value="提交">
</form><br/>

</body>
</html>

9、测试成功跳转的页面(Success.ftl)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>

<#list list as i>
    ${i}
</#list>

</body>
</html>

10、测试成功