当前位置: 移动技术网 > IT编程>开发语言>Java > springboot+mybatis+Thymeleaf

springboot+mybatis+Thymeleaf

2019年05月12日  | 移动技术网IT编程  | 我要评论
1.项目结构 2.代码展示 1.pom.xml 2.application.properties 3.实体类test 4.mapper层(接口和映射文件) 接口 映射文件 5.业务层 接口(TestService) 实现类(TestServiceImpl) 6.表示层(controller) 7.启 ...

1.项目结构

2.代码展示

 1.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>
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.1.4.release</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.bl</groupid>
    <artifactid>test</artifactid>
    <version>0.0.1-snapshot</version>
    <packaging>war</packaging>
    <name>test</name>
    <description>demo project for spring boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-jdbc</artifactid>
        </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>2.0.1</version>
        </dependency>

        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <scope>runtime</scope>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-tomcat</artifactid>
            <scope>provided</scope>
        </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-thymeleaf</artifactid>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>

    </build>

</project>

 2.application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aaa
spring.datasource.username=root
spring.datasource.password=aaa

spring.thymeleaf.cache=false

3.实体类test 

package com.bl.test.pojo;

public class test {
    private int id;
    private int y;
    private int w;

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public int gety() {
        return y;
    }

    public void sety(int y) {
        this.y = y;
    }

    public int getw() {
        return w;
    }

    public void setw(int w) {
        this.w = w;
    }
}

 4.mapper层(接口和映射文件)

 接口

package com.bl.test.mapper;

import com.bl.test.pojo.test;
import org.apache.ibatis.annotations.mapper;

import java.util.list;
@mapper
public interface testmapper {
    public list<test> queryall();
    public test queryone(int id);
    public void update(test test);
    public void delete(int id);
    public void add(test test);

}

映射文件

<?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.bl.test.mapper.testmapper">

    <resultmap id="baseresultmap" type="com.bl.test.pojo.test">
        <id column="id" jdbctype="integer" property="id" />
        <result column="y" jdbctype="integer" property="y" />
        <result column="w" jdbctype="integer" property="w" />
    </resultmap>
    <select id="queryall" resultmap="baseresultmap">
        select * from test
    </select>
    <select id="queryone" resultmap="baseresultmap">
        select * from test where id=#{id}
    </select>
    <update id="update">
        update test set y=#{y},w=#{w} where id=#{id}
    </update>
    <insert id="add">
        insert into test values(null,#{y},#{w})
    </insert>
    <delete id="delete">
        delete from test where id=#{id}
    </delete>

</mapper>

5.业务层

接口(testservice)

package com.bl.test.service;

import com.bl.test.pojo.test;

import java.util.list;

public interface testservice {
    public list<test> selectall();
    public test queryone(int id);
    public void update(test t);
    public void delete(int id);
    public void add(test t);
}

实现类(testserviceimpl)

package com.bl.test.service.impl;

import com.bl.test.mapper.testmapper;
import com.bl.test.pojo.test;
import com.bl.test.service.testservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;

import java.util.list;

@service
public class testserviceimpl implements testservice {
    @autowired
    private testmapper testmapper;

    @override
    public list<test> selectall() {
        return testmapper.queryall();
    }

    @override
    public test queryone(int id) {
        return testmapper.queryone(id);
    }

    @override
    public void update(test t) {
        testmapper.update(t);
    }

    @override
    public void delete(int id) {
        testmapper.delete(id);
    }

    @override
    public void add(test t) {
        testmapper.add(t);
    }
}

6.表示层(controller)

package com.bl.test.controller;

import com.bl.test.pojo.test;
import com.bl.test.service.testservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

import java.util.list;

@controller
public class testcontroller {
    @autowired
    private testservice testservice;
    @requestmapping("/test/queryall")
    public string queryall(model model){
        list<test> list=testservice.selectall();
        model.addattribute("list",list);
        return "test";
    }

    @requestmapping("/test/queryone")
    public string queryone(model model,int id){
        test t=testservice.queryone(id);
        model.addattribute("test",t);
        return "testone";
    }

    @requestmapping("/test/update")
    public string update(test test){

        testservice.update(test);
        return "redirect:/test/queryall";
    }

    @requestmapping("/test/add")
    public string add(test test){
        testservice.add(test);
        return "redirect:/test/queryall";
    }

    @requestmapping("/test/delete")
    public string delete(int id){
        testservice.delete(id);
        return "redirect:/test/queryall";
    }

    @requestmapping("{test}")
    public string test(@pathvariable("test") string test){
        return test;
    }
}

7.启动类

package com.bl.test;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

@springbootapplication
public class testapplication {

    public static void main(string[] args) {
        springapplication.run(testapplication.class, args);
    }

}

8.前端

1.显示全部

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    <table border="1" cellpadding="0">
        <tr>
            <th>编号</th>
            <th>y</th>
            <th>w</th>
            <th>操作</th>
        </tr>
        <tr th:each="list : ${list}">
            <td th:text="${list.id}"></td>
            <td th:text="${list.y}"></td>
            <td th:text="${list.w}"></td>
            <td>
                <a th:href="@{/test/queryone(id=${list.id})}">修改</a>
                <a th:href="@{/test/delete(id=${list.id})}">删除</a>
            </td>
        </tr>
        <tr>
           <td colspan="4">
              <a th:href="@{/testadd}">添加</a>
           </td>
        </tr>
    </table>
</body>
    <script>
        function a(id){
            alert(id);
        }
    </script>
</html>

2.添加

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>添加</title>
</head>
<body>
    <form th:action="@{/test/add}">
        <table border="1" cellpadding="0">
            <tr>
                <th>y</th>
                <th>w</th>
                <th>操作</th>
            </tr>
            <tr >
                <td><input name="y" /></td>
                <td><input name="w"/></td>
                <td><input type="submit" value="提交" /> ></td>
            </tr>
        </table>
    </form>


</body>
    <script>
        function a(id){
            alert(id);
        }
    </script>
</html>

3.修改

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>修改</title>
</head>
<body>
    <form th:action="@{/test/update}">
        <table border="1" cellpadding="0">
            <tr>
                <th>编号</th>
                <th>y</th>
                <th>w</th>
                <th>操作</th>
            </tr>
            <tr >
                <td>
                    <input name="id" readonly th:value="${test.id}" />
                </td>
                <td><input name="y" th:value="${test.y}" /></td>
                <td><input name="w" th:value="${test.w}" /></td>
                <td><input type="submit" value="提交" /> ></td>
            </tr>
        </table>
    </form>


</body>
    <script>
        function a(id){
            alert(id);
        }
    </script>
</html>

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网