目录

SpringBoot整合JDBC模板-LMLPHP

Grade实体类

public class Grade {
private Integer gradeId;
private String gradeName; public Grade(){ } public Grade(Integer gradeId, String gradeName) {
this.gradeId = gradeId;
this.gradeName = gradeName;
} public Grade(String gradeName) {
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;
}
}

IGradeDao

import com.boot.entity.Grade;
import java.util.List;
public interface IGradeDao {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer id);
public List<Grade> findAll();
}

IGradeDaoImpl

import com.boot.dao.IGradeDao;
import com.boot.entity.Grade;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@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);
}
}

IGradeService

public interface IGradeService {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer id);
public List<Grade> findAll();
}

IGradeServiceImpl

@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();
}
}

JDBCTemplateController

@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(,"S2"));
}
@RequestMapping("/deleteGrade")
public int deleteGrade(){
return iGradeService.deleteGrade();
}
@RequestMapping("/findAll")
public List<Grade> findAll(){
return iGradeService.findAll();
}
}

application.yml

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springbootjpa
username: root
password: ##更改Tomcat端口
server:
port:
##指定当前工程项目访问地址
context-path: /jdbc
05-16 07:02