SpringBoot学习笔记之JDBC使用

SpringBoot使用jdbc步骤
1)、pom.xml 配置maven依赖
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
2)、属性配置文件(application.properties)
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3)、代码使用方法
@Service
public class CommonServiceImpl implements CommonService {
@Autowired
private JdbcTemplate jdbcTemplate; //直接注入JdbcTemplate就可以使用
@Autowired
private IConsultContract iConsultContract;
public List queryContent(Map map) {
String sql = "select * from consult_content a where a.state = 0 and a.type = 1 order by a.itemindex";
return jdbcTemplate.query(sql, new RowMapper() {
public ConsultContent mapRow(ResultSet rs, int rowNum)
throws SQLException {
ConsultContent cc = new ConsultContent();
cc.setContent(rs.getString("content"));
cc.setId(rs.getInt("id"));
cc.setItemIndex(rs.getInt("itemIndex"));
cc.setState(rs.getInt("state"));
cc.setType(rs.getString("type"));
return cc;
}
});
}
public List queryConsultContract() {
return iConsultContract.findAll();
}
}

在controller中增加接口调用
@Autowired
private ICommonService commonService;

@RequestMapping("/queryContent")
public @ResponseBody List queryContent(){
logger.info("开始查询");
List content = commonService.queryContent(new HashMap());
logger.info("结束查询");
return content;
}

测试方法:
http://localhost:8080/web/queryContent

源码下载地址:http://download.csdn.net/download/liyuejin/9986140


你可能感兴趣的:(JDBC)