写点什么

Spring Boot+Mybatis+thymeleaf 整合

作者:Java高工P7
  • 2021 年 11 月 11 日
  • 本文字数:2933 字

    阅读完需:约 10 分钟

CREATE TABLE user_tb (


userid int(11) NOT NULL AUTO_INCREMENT,


username varchar(255) DEFAULT NULL,


department varchar(255) DEFAULT NULL,


classroom int(11) DEFAULT NULL,


PRIMARY KEY (userid)


) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;


INSERT INTO user_tb VALUES ('1', '周杰伦', '音乐系', '5');


INSERT INTO user_tb VALUES ('2', '张无忌', '武当派', '1');


INSERT INTO user_tb VALUES ('3', '赵敏', '蒙古族', '3');

二、创建项目

<table border="1" cellpadding="1" cellspacing="1"><tbody><tr><td colspan="2">1、创建项目</td></tr><tr><td><img alt="" height="839" src="https://img-blog.csdnimg.cn/20200812160921673.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2gyNTAzNjUyNjQ2,size_16,color_FFFFFF,t_70" width="1200"></td><td><img alt="" height="839" src="https://img-blog.csdnimg.cn/20200812161002682.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2gyNTAzNjUyNjQ2,size_16,color_FFFFFF,t_70" width="1200"></td></tr><tr><td colspan="2">2、最终项目结构</td></tr><tr><td colspan="2"><p style="text-align:center;"><img alt="" height="546" src="https://img-blog.csdnimg.cn/20200812165046254.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2gyNTAzNjUyNjQ2,size_16,color_FFFFFF,t_70" width="263"></p></td></tr></tbody></table>

三、添加 pom.xml

里面引入了本次整合所需要的所有依赖包,包括 Mybatis、thymeleaf、lombok。


<?xml version="1.0" encoding="UTF-8"?>


<project xmlns="http://maven.apache.org/POM/4.0.0"


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>


<groupId>org.example</groupId>


<artifactId>ShiroDemo</artifactId>


<version>1.0-SNAPSHOT</version>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.0.6.RELEASE</version>


</parent>


<dependencies>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>org.mybatis.spring.boot</groupId>


<artifactId>mybatis-spring-boot-starter</artifactId>


<version>1.1.1</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-devtools</artifactId>


<optional>true</optional>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-thymeleaf</artifactId>


</dependency>


<dependency>


<groupId>org.projectlombok</groupId>


<artifactId>lombok</artifactId>


<version>1.18.4</version>


</dependency>


</dependencies>


</project>

四、配置 application.yml

也就是 application.properties,只是格式不同而已,个人觉得 yml 格式更直观清晰。


spring:

mysql 数据源

datasource:


url: jdbc:mysql://localhost/shiro_demo?useUnicode=true&characterEncoding=UTF-8


username: root


password: root


driverClassName: com.mysql.jdbc.Driver


mybatis:


mapper-locations: classpath:mapper/*.xml


type-aliases-package: com.zyy.model

五、编写代码

user.java


因为使用了**@Data**注解,所以可以省略 get、set、toString···


package com.zyy.model;


import lombok.Data;


import java.io.Serializable;


import java.util.List;


/**


  • @ClassName User

  • @Author DoNg

  • @Date 2020/8/12 0012 10:58

  • @Description User


*/


@Data


public class User{


private Integer userid;


private String username;


private String department;


private Integer classroom;


}


UserMapper.xml? (在 resourses 目录下新建 mapper 文件夹,用于存放 xxxMapper.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="com.zyy.mapper.UserMapper">


<select id="selectAllUser" resultType="com.zyy.model.User">


select * from user_tb


</select>


</mapper>


UserMapper.java


package com.zyy.mapper;


import com.zyy.model.User;


import org.apache.ibatis.annotations.Mapper;


import org.springframework.stereotype.Repository;


import java.util.List;


/**


  • @ClassName UserMapper

  • @Author DoNg

  • @Date 2020/8/12 0012 11:19

  • @Description UserMapper


*/


@Mapper


@Repository


public interface UserMapper {


List<User> selectAllUser();


}


UserService.java


package com.zyy.service;


import com.zyy.model.User;


import org.springframework.stereotype.Service;


import java.util.List;


/**


  • @ClassName UserService

  • @Author DoNg

  • @Date 2020/8/12 0012 11:51

  • @Description UserService


*/


public interface UserService {


List<


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


User> getAllUser();


}


UserServiceImpl.java


package com.zyy.service.impl;


import com.zyy.mapper.UserMapper;


import com.zyy.model.User;


import com.zyy.service.UserService;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import java.util.List;


/**


  • @ClassName UserServiceImpl

  • @Author DoNg

  • @Date 2020/8/12 0012 11:52

  • @Description UserServiceImpl


*/


@Service


public class UserServiceImpl implements UserService {


@Autowired


private UserMapper userMapper;


@Override


public List<User> getAllUser(){


return this.userMapper.selectAllUser();


}


}


UserController.java


package com.zyy.controller;


import com.zyy.model.User;


import com.zyy.service.UserService;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;


import org.springframework.ui.ModelMap;


import org.springframework.web.bind.annotation.GetMapping;


import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;


/**


  • @ClassName UserController

  • @Author DoNg

  • @Date 2020/8/12 0012 11:33

  • @Description UserController


*/

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
Spring Boot+Mybatis+thymeleaf整合