一、创建项目:选择需要的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
#mybatis
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.wisely.stuMybatis.bean
三、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 + "]";
}
}
四、Mapper映射文件
<?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">
<!-- namespace指用户自定义的命名空间。 -->
<mapper namespace="com.wisely.stuMybatis.dao.UserInfoDao">
<resultMap type="UserInfo" id="usermap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="gender" property="gender"/>
</resultMap>
<select id="findById" parameterType="Integer" resultMap="usermap">
select * from user_info where id=#{id}
</select>
</mapper>
五、Controller:
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 com.wisely.stuMybatis.bean.UserInfo;
@Mapper
public interface UserInfoDao {
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>