重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

【SpringBoot整合SpringDataJPA简单使用】-创新互联

SpringBoot 整合 Spring Data JPA 简单使用
  • 快速入门
    • 引入依赖(pom.xml文件)
    • 配置数据源信息(application.yml)
    • 编写实体类user
    • 编写dao
    • 定义service接口
    • 继承server接口实现方法
    • 编写controller

创新互联致力于互联网品牌建设与网络营销,包括网站设计、成都做网站、SEO优化、网络推广、整站优化营销策划推广、电子商务、移动互联网营销等。创新互联为不同类型的客户提供良好的互联网应用定制及解决方案,创新互联核心团队10多年专注互联网开发,积累了丰富的网站经验,为广大企业客户提供一站式企业网站建设服务,在网站建设行业内树立了良好口碑。快速入门 引入依赖(pom.xml文件)
org.springframework.bootspring-boot-starter-data-jpa2.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;
编写dao
public 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://cqcxhl.cn/article/ieeos.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP