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
4.mapper.xml 与 PersonMapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
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 map){ Person person = personService.queryPersonById(id); System.out.println(person); map.put("person",person); return "result"; } }
7.前段页面
index.html
查询一个人
result.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); } }