SpringBoot使用JdbcTemplate
Posted 听风忆雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot使用JdbcTemplate相关的知识,希望对你有一定的参考价值。
1.导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
2.创建实体类
public class Grade { private Integer gradeId; private String gradeName; public Grade() { } public Grade(String gradeName) { this.gradeName = gradeName; } public Grade(Integer gradeId, String gradeName) { this.gradeId = gradeId; this.gradeName = gradeName; } public Integer getGradeId() { return gradeId; } public void setGradeId(Integer gradeId) { this.gradeId = gradeId; } public String getGradeName() { return gradeName; } public void setGradeName(String gradeName) { this.gradeName = gradeName; } }
3.配置文件
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///myschool username: root password: 123 ##更改Tomcat端口 server: port: 8081 ##指定当前工程项目访问地址 context-path: /jdbc
4.dao层接口
public interface IGradeDao { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> findAll(); }
5.dao层实现类
@Repository public class IGradeDaoImpl implements IGradeDao { //导入JDBCTemplate模板 @Resource private JdbcTemplate jdbcTemplate; @Override public int insertGrade(Grade grade) { return jdbcTemplate.update("insert into Grade(GradeName) values(?)",grade.getGradeName()); } @Override public int updateGrade(Grade grade) { return jdbcTemplate.update("update Grade set GradeName=? where GradeId=?",grade.getGradeName(),grade.getGradeId()); } @Override public int deleteGrade(Integer id) { return jdbcTemplate.update("delete from Grade where GradeId=?",id); } @Override public List<Grade> findAll() { //封装行数据映射 RowMapper<Grade> rowMapper=new RowMapper<Grade>() { @Override public Grade mapRow(ResultSet rs, int rowNum) throws SQLException { Grade grade=new Grade(rs.getInt("GradeId"),rs.getString("GradeName")); return grade; } }; return jdbcTemplate.query("select * from Grade", rowMapper); } }
6.service层接口
import java.util.List; public interface IGradeService { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> findAll(); }
7.servicece层实现类
@Service("iGradeService") public class IGradeServiceImpl implements IGradeService { @Resource private IGradeDao iGradeDao; @Override public int insertGrade(Grade grade) { return iGradeDao.insertGrade(grade); } @Override public int updateGrade(Grade grade) { return iGradeDao.updateGrade(grade); } @Override public int deleteGrade(Integer id) { return iGradeDao.deleteGrade(id); } @Override public List<Grade> findAll() { return iGradeDao.findAll(); } }
8.controller层
@RestController public class JDBCTemplateController { @Resource private IGradeService iGradeService; @RequestMapping("/insertGrade") public int insertGrade(){ return iGradeService.insertGrade(new Grade("S1")); } @RequestMapping("/updateGrade") public int updateGrade(){ return iGradeService.updateGrade(new Grade(6,"S2")); } @RequestMapping("/deleteGrade") public int deleteGrade(){ return iGradeService.deleteGrade(6); } @RequestMapping("/findAll") public List<Grade> findAll(){ return iGradeService.findAll(); } }
9.测试类
@SpringBootApplication public class StartSpringBoot { public static void main(String[] args) { SpringApplication.run(StartSpringBoot.class,args); } }
添加
修改
删除
查询
以上是关于SpringBoot使用JdbcTemplate的主要内容,如果未能解决你的问题,请参考以下文章