springboot整合mybatis与thymeleaf

GechangLiu 2020-01-26

1.创建springboot项目

(1)选择Spring Initializr

  springboot整合mybatis与thymeleaf

   (2)填写自己的Group 与 Artifact

  springboot整合mybatis与thymeleaf

   (3)选择依赖框架

  springboot整合mybatis与thymeleaf

   等待maven下载好依赖和插件即可

2.主配置文件(这里使用的是更为简洁的ylm文件)

 application.yml

主要配置了数据源和mapper路径与实体类别名

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: 330069879
mybatis:
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: com.datatest.signup.entity

3.实体类与表的映射关系

 Person.java

package com.datatest.signup.entity;

public class Person {
    private String id;
    private String name;
    private int age;
    private int sex;

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=‘" + id + ‘\‘‘ +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", sex=" + sex +
                ‘}‘;
    }
}

数据库表person

springboot整合mybatis与thymeleaf

4.mapper.xml  与    PersonMapper

<?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.datatest.signup.mapper.PersonMapper">
    <select id="queryPersonById" resultType="Person" parameterType="String">
        select * from person where id=#{id}
    </select>
</mapper>

 PersonMapper.java

package com.datatest.signup.mapper;

import com.datatest.signup.entity.Person;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonMapper {
    Person queryPersonById(String id);
}

5.Service层

PersonService.java
package com.datatest.signup.service;

import com.datatest.signup.entity.Person;

public interface PersonService {
    public Person queryPersonById(String id);
}
package com.datatest.signup.service.impl;

import com.datatest.signup.entity.Person;
import com.datatest.signup.mapper.PersonMapper;
import com.datatest.signup.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;

    @Override
    public Person queryPersonById(String id) {
        return personMapper.queryPersonById(id);
    }
}

6.controller层

package com.datatest.signup.controller;

import com.datatest.signup.entity.Person;
import com.datatest.signup.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class StudentController {

    @Autowired
    private PersonService personService;

    @RequestMapping("query/{id}")
    public String queryPersonById(@PathVariable String id, Map<String,Object> map){
        Person person = personService.queryPersonById(id);
        System.out.println(person);
        map.put("person",person);
        return "result";
    }
}

7.前段页面

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>sprint boot</title>
</head>
<body>
    <a href="query/P01">查询一个人</a>
</body>
</html>

result.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>result</title>
</head>
<body>
    <p th:text="${person}">没有查询到该人</p>
</body>
</html>

8.主程序文件

package com.datatest.signup;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.datatest.signup.mapper")
@SpringBootApplication
public class SignupApplication {

    public static void main(String[] args) {
        SpringApplication.run(SignupApplication.class, args);
    }

}

9.测试效果

springboot整合mybatis与thymeleaf

 springboot整合mybatis与thymeleaf

相关推荐