一、整合步驟
1、添加啟動依賴
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2、配置Mybatis
#配置mybatis
mybatis:
#實體類包名路徑
type-aliases-package: com.bh.pojo
#映射文件路徑
# mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3、給啟動類配置MapperScan掃描注解
package com.bh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
/**
* 啟動類
*/
@SpringBootApplication
//整合通用Mapper,修改掃描注解tk.mybatis.spring.annotation.MapperScan
@MapperScan("com.bh.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4、編寫各層代碼
pojo實體類
package com.bh.pojo;
import javax.persistence.Id;
import javax.persistence.Table;
import tk.mybatis.mapper.annotation.KeySql;
@Table(name = "user")
public class User {
@Id
@KeySql(useGeneratedKeys = true)//主鍵回填
private int id;
private String username;
private String password;
private String email;
……(geter、setter)
}
mapper層
package com.bh.mapper;
import com.bh.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
service層
package com.bh.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bh.mapper.StudentMapper;
import com.bh.mapper.UserMapper;
import com.bh.pojo.User;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
//根據id查詢
public User findUserById(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
public List<User> find(){
return userMapper.selectAll();
}
}
controller層
package com.bh.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.bh.pojo.User;
import com.bh.service.UserService;
@RestController
public class HelloController {
@Autowired
private UserService userService;
/**
* 根據id查詢
* @param id
* @return
*/
@GetMapping("/user/{id}")
public User getUser(@PathVariable Integer id) {
return userService.findUserById(id);
}
/**
* 查詢所有用戶
* @return
*/
@GetMapping("/user")
public List<User> get(){
return userService.find();
}
}
二、出現的問題
1、問題
代碼的大致結構基本寫完,然后看似也沒什么問題,把程序運行起來,去訪問,就會發現
但是呢我數據庫里是有數據的,接下來再訪問一下查詢全部用戶的接口
現在問題就是我這的id全是0,這肯定是不對的,看控制台打印的sql語句會發現沒有id那個字段
2、原因及解決方法
經過排查,發現問題出現的原因是我實體類中定義的id類型是基本數據類型int
解決方法把int類型改為它的包裝類Integer類型,問題就解決了
修改User類中的類型
運行結果
三、總結
【注意】在開發過程中,實體類的屬性類型盡量不要使用基本類型,用相應的包裝類來代替,避免出現一些奇怪的問題