一、创建项目,选择相关pom starter
二、application-dev.properties配置:
server.port=8888
#logging.file=G://mylog//log_1.log
#slogging.level.org.springframework.web=DEBUG
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=sx96411
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
三、JavaBean:
package com.wisely.stuMybatis.bean;
public class UserInfo {
private Integer id;
private String name;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", name=" + name + ", gender=" + gender + "]";
}
}
四、控制器:
package com.wisely.stuMybatis.controller;
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 org.springframework.web.bind.annotation.ResponseBody;
import com.wisely.stuMybatis.bean.UserInfo;
import com.wisely.stuMybatis.service.UserInfoService;
@Controller
@RequestMapping("/userinfo/")
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
@RequestMapping("index")
public String index() {
return "index";
}
@ResponseBody
@RequestMapping("show/{id}")
public UserInfo show(@PathVariable Integer id) {
UserInfo userInfo = userInfoService.findById(id);
//model.addAttribute("user",userInfo);
return userInfo;
}
}
五、Dao层:
package com.wisely.stuMybatis.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.wisely.stuMybatis.bean.UserInfo;
@Mapper
public interface UserInfoDao {
@Select("select * from user_info where id = #{id}")
@Results(value= {
@Result(column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="gender",property="gender")
})
public UserInfo findById(Integer id);
}
六、service层:
package com.wisely.stuMybatis.service;
import com.wisely.stuMybatis.bean.UserInfo;
public interface UserInfoService {
public UserInfo findById(Integer id);
}
七、service实现层:
package com.wisely.stuMybatis.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wisely.stuMybatis.bean.UserInfo;
import com.wisely.stuMybatis.dao.UserInfoDao;
import com.wisely.stuMybatis.service.UserInfoService;
@Service
public class UserInfoServiceImpl implements UserInfoService{
@Autowired
private UserInfoDao userInfoDao;
@Override
public UserInfo findById(Integer id) {
System.out.println("查找id = "+ id +"的数据");
UserInfo userInfo = userInfoDao.findById(id);
System.out.println(userInfo);
return userInfo;
}
}
八、前端页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>index.html</h1>
<a href="/userinfo/show/3">查看用户信息</a>
</body>
</html>