【SpringBoot整合SpringDataJPA简单使用】-创新互联
SpringBoot 整合 Spring Data JPA 简单使用
当前名称:【SpringBoot整合SpringDataJPA简单使用】-创新互联
当前链接:http://azwzsj.com/article/ieeos.html
- 快速入门
- 引入依赖(pom.xml文件)
- 配置数据源信息(application.yml)
- 编写实体类user
- 编写dao
- 定义service接口
- 继承server接口实现方法
- 编写controller
org.springframework.boot spring-boot-starter-data-jpa 2.6.5
配置数据源信息(application.yml)spring:
datasource:
username: userName
password: password
name: databaseName
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.253.129:3306/databaseName?serverTimezone=UTC&characterEncoding=UTF-8
编写实体类user@Entity
@Data
@Table(name = "user")
public class SysUser {@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String username;
@Column
private String password;
编写daopublic interface SysUserDao extends JpaRepository, JpaSpecificationExecutor{SysUser findByUsername(String username);
}
定义service接口public interface SysUserService {Listsearch();
SysUser searchById(Integer id);
SysUser searchByUsername(String username);
boolean add(SysUser sysUser);
boolean update(SysUser sysUser);
boolean delete(Integer id);
}
继承server接口实现方法@Service
public class SysUserServiceImpl implements SysUserService {@Autowired
SysUserDao sysUserDao;
@Override
public Listsearch() {return sysUserDao.findAll();
}
@Override
public SysUser searchById(Integer id) {return sysUserDao.findById(id).get();
}
@Override
public SysUser searchByUsername(String username) {return sysUserDao.findByUsername(username);
}
@Override
public boolean add(SysUser sysUser) {return sysUserDao.save(sysUser) != null;
}
@Override
public boolean update(SysUser sysUser) {return sysUserDao.save(sysUser) != null;
}
@Override
public boolean delete(Integer id) {try {sysUserDao.deleteById(id);
} catch (Exception e) {return false;
}
return true;
}
}
编写controller@Controller
@RequestMapping("sysUser")
public class SysUserController {@Autowired
SysUserService sysUserService;
@Autowired
SysRoleService sysRoleService;
@RequestMapping("search")
public ModelAndView search() {ModelAndView mv = new ModelAndView("sysUser/show");
Listlist = sysUserService.search();
mv.addObject("list", list);
return mv;
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前名称:【SpringBoot整合SpringDataJPA简单使用】-创新互联
当前链接:http://azwzsj.com/article/ieeos.html