一 启动类
package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.imooc.mapper")
//扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages= {"com.imooc", "org.n3r.idworker"})
public class ImoocApplication {
public static void main(String[] args) {
SpringApplication.run(ImoocApplication.class, args);
}
}
二 控制器
package com.imooc.controller;
import java.util.Date;
import java.util.List;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.imooc.pojo.IMoocJSONResult;
import com.imooc.pojo.SysUser;
import com.imooc.service.UserService;
@RestController
@RequestMapping("mybatis")
public class MyBatisCRUDController {
@Autowired
private UserService userService;
@Autowired
private Sid sid;
@RequestMapping("/saveUser")
public IMoocJSONResult saveUser() throws Exception {
String userId = sid.nextShort();
SysUser user = new SysUser();
user.setId(userId);
user.setUsername("imooc" + new Date());
user.setNickname("imooc" + new Date());
user.setPassword("abc123");
user.setIsDelete(0);
user.setRegistTime(new Date());
userService.saveUser(user);
return IMoocJSONResult.ok("保存成功");
}
@RequestMapping("/updateUser")
public IMoocJSONResult updateUser() {
SysUser user = new SysUser();
user.setId("10011001");
user.setUsername("10011001-updated" + new Date());
user.setNickname("10011001-updated" + new Date());
user.setPassword("10011001-updated");
user.setIsDele