IDEA下SpringBoot + Mybatis

环境版本

Mysql-8.0
springboot-2.1.6
jdk-1.8

1.新建项目

新建项目

2.采用mybatis-generator自动生成mapper,dao,entity

连接:https://www.jianshu.com/p/b519e9ef605f

3.配置application.properties文件

server.port=8080
# ==============================
# MySQL connection config
# ==============================
spring.datasource.url=jdbc:mysql://localhost:3306/wg_insert?useUnicode=true&characeterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
# ==============================
# Hikari 数据源专用配置
# ==============================
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
# ==============================
# Thymeleaf configurations
# ==============================
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=UTF-8
# ==============================
# Mybatis configurations
#添加对xml中对实体对象引用的配置
#添加xml在resouces目录下的位置
# ==============================
mybatis.type-aliases-package=com.wglvzyx.mybatisredis.entity
mybatis.mapper-locations=classpath:mapper/*.xml

4.实现查询所有的学生函数功能

  • StudentMapper添加函数List selectAllStudent();


    注意在IDEA中需要给这个StudentMapper加上注解@Repository;
    否则后面注入时会报错,原因嘛?

  • StudentMapper.xml中实现这个函数

  
  • 新建一个StudentService的业务类并实现获取所有学生信息的函数


@Service
public class StudentService{
    @Autowired
    StudentMapper studentMapper;

    public List GetAllStu(){
        return studentMapper.selectAllStudent();
    }

}

5.在前端显示所有学生名字

  • 新建一个HomeController控制类
@Controller
public class HomeController {
    @Autowired
    StudentService studentService;

    @RequestMapping(value = "/")
    public String gethome(Model model){
        List allstu = new ArrayList<>();
        allstu = studentService.GetAllStu();
        model.addAttribute("wgstu",allstu);
        return "index";
    }
}
  • 新建一个index.html文件





    
    mybatis测试


我的标题

6.在springboot主入口添加注解,里面值为Mapper对应的Java接口类


@MapperScan("com.wglvzyx.mybatisredis.dao")

7.完成启动项目

启动完成记住localhost:8080

效果图

运行结果

你可能感兴趣的:(IDEA下SpringBoot + Mybatis)