SpringBoot集成Mybatis
1.pom.xml引入依赖
2.数据库建表(id设为自动递增)
插入一条记录
3.数据库配置
4.建包和类
5.Hello.java
package com.example.demo.Controller;
import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello{
@Autowired
private UserService userService;
@RequestMapping("/getUserInfo")
public Object getUserById(Integer id){
return userService.getUserById(id).toString();
}
@RequestMapping("/insertUserInfo")
public Object insertUser(String username,String sex){
userService.insertUser(username,sex);
return "success";
}
}
6.User.java
package com.example.demo.Entity;
public class User {
private int id;
private String username;
private String sex;
public void setId(int id){
this.id = id;
}
public int getId() {
return id;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex(){
return sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
7.UserMapper.java
package com.example.demo.Mapper;
import com.example.demo.Entity.User;
import org.apache.ibatis.annotations.*;
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
User getUserById(@Param("id") Integer id);
@Insert("insert into user values(NULL,#{username},#{sex})")
void insertUser(@Param("username") String username, @Param("sex") String sex);
}
8.UserService.java
package com.example.demo.Service;
import com.example.demo.Entity.User;
public interface UserService {
User getUserById(Integer id);
void insertUser(String username, String sex);
}
9.UserServiceImpl.java
package com.example.demo.Service;
import com.example.demo.Entity.User;
import com.example.demo.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id){
return userMapper.getUserById(id);
}
@Override
public void insertUser(String username, String sex) {
userMapper.insertUser(username,sex);
}
}
10.在浏览器中进行测试
(1)查询ID为1的用户信息
(2)插入用户Linda
(3)查询ID为2的用户信息
11.SpringBoot ResultFul风格 JSON格式 加特定请求,如Get、Post
Get用来查数据,Post用来插数据
Hello.java改为:
package com.example.demo.Controller;
import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello{
@Autowired
private UserService userService;
@GetMapping("/getUserInfo")
public Object getUserById(Integer id){
return userService.getUserById(id).toString();
}
@PostMapping("/insertUserInfo")
public Object insertUser(String username,String sex){
userService.insertUser(username,sex);
return "success";
}
}
注意:PostMapping需要测试工具(如PostMan)来测试,不能在浏览器地址栏中测试: