Controller:
package com.beijing.yidianyun.controller; import com.beijing.yidianyun.mapper.UserMapper; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/user") public class UserController { @Resource private UserMapper userMapper; @RequestMapping("/") public String index(Model model, HttpServletResponse response) { model.addAttribute("name", "simonsfan"); return "/index"; } @PostMapping("/query") @ResponseBody public Map<String,Object> findUser(String id){ HashMap<String,Object> map=new HashMap<>(); map.put("name","张三"); map.put("age","13"); return map; } }
mapper:
package com.beijing.yidianyun.mapper; import com.beijing.yidianyun.pojo.User; import java.util.List; public interface UserMapper { User findById(Long id); List<User> findAll(); }
pojo:
package com.beijing.yidianyun.pojo; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String name; private String password; public User() { } public User(Long id, String name, String password) { this.id = id; this.name = name; this.password = password; } public static long getSerialVersionUID() { return serialVersionUID; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
启动文件:》
@MapperScan("com.beijing.yidianyun.mapper")
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.beijing.yidianyun.pojo"/> </typeAliases> <!--<mappers>--> <!--</mappers>--> </configuration>
UserMapper.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.beijing.yidianyun.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.beijing.yidianyun.pojo.User" > <result column="id" property="id" /> <result column="name" property="name" /> <result column="password" property="password" /> </resultMap> <select id="findById" resultMap="BaseResultMap"> select * from user where id = #{id} </select> <select id="findAll" resultMap="BaseResultMap"> select * from user </select> </mapper>
index.html
<!DOCTYPE html> <html lang="en"> <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> function houtai(){ $.ajax({ url: "/user/query", data: {"id":12}, type: "POST", contentType: "application/json;charset=UTF-8",//指定消息请求类型 dataType: 'json', success: function(data) { var str = JSON.stringify(data); alert(str ); } }); } </script> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <button type="button" οnclick="houtai()">Click Me!</button> <thead> <tr> <th>hotelSeq</th> <th>酒店名称</th> <th>订单号</th> <th>联系人手机号</th> <th>预定时间</th> <th>userId</th> <th>cellid</th> <th>gps定位城市</th> <th>wifi定位城市</th> <th>定位距离</th> </tr> </thead> </body> </html>
appp
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT spring.datasource.username=root spring.datasource.password=111111 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver mybatis.config-location=classpath:config/mybatis-config.xml mybatis.mapper-locations= classpath:mapper/*.xml #模板引擎配置 spring: freemarker: template-loader-path: classpath:/templates/ prefer-file-system-access: false settings: template_update_delay: 0 classic_compatible: true number_format: 0.###### template_exception_handler: ignore request-context-attribute: rc charset: UTF-8