使用Spring Boot+MyBatis+Thymeleaf框架作查詢操做

在你創建的工程下建立 Module 選擇Spring initializr建立


在Type處選擇: Maven Project(項目的構建工具)


建立依賴時勾上web,mybatis,mysql(這個看你我的須要吧,能夠自主選擇)



創建好的項目結構以下:



注意:application.properties和application.yml是同一個東西,均爲項目的核心配置文件
內容以下:

#鏈接數據庫
spring.datasource.url=jdbc:mysql://localhost:3306/smbms
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#引入mybatis的配置文件mybatis:
    mybatis.mapper-locations=classpath:mapper/*.xml
    mybatis.type-aliases-package=com.example.sprboot.pojo複製代碼

相應的pom.xml文件

<?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>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
 <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--添加web依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入spring boot包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId> 
           <scope>test</scope>
        </dependency>
        <!-- Spring-Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--使用json對象-->
        <dependency
>            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <!--使用thymeleaf標籤-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>複製代碼

相應的接口UserMapper以下:

@Repository
public interface UserMapper {
    List<User> getList();
}複製代碼

service以下:

public interface UserService {
    List<User> getList();
}複製代碼

impl以下:

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> getList() {
        return userMapper.getList();
    }
}複製代碼

在resources中建一個文件夾mapper裏面放mapper.xml文件,代碼以下:

<select id="getList" resultType="User">
    select * from smbms_user
</select>複製代碼

在templates文件夾中建html文件,使用Thymeleaf進行數據遍歷
核心代碼以下:

<table>
    <th>工號</th>
    <th>用戶名</th>
    <th>姓名</th>
    <th>性別</th>
    <th>生日</th>
    <th>電話</th>
    <th>地址</th>
    <th>建立時間</th>
    <tr th:each="user : ${users}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.userCode}"></td>
        <td th:text="${user.userName}"></td>
        <td th:text="${user.gender}"></td>
        <td th:text="${user.birthday}"></td>
        <td th:text="${user.phone}"></td>
        <td th:text="${user.address}"></td>
        <td th:text="${user.createdBY}"></td>
    </tr>
</table>複製代碼

此處有一個th標籤,須要引入一個

<html xmlns:th="http://www.thymeleaf.org">複製代碼

並在pom.xml中引入對應的jar包(html中不能使用jstl表達式)
下一篇文章介紹thymeleaf的知識


控制器代碼以下:

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/")
    public String getStuinforList(HttpServletRequest request, Model model){
        List<User> list=userService.getList();
        model.addAttribute("users",list);
        System.out.println(list);
        return "/index.html";
    }
}複製代碼
相關文章
相關標籤/搜索