Spring boot jdbc 自定義SQL查詢
閒來沒事,中午學了spring boot 感覺還不錯,避免了繁瑣的配置檔案,提高開發效率還不錯。個人認為spring boot也就簡化配置而已。在學習中也遇到一些問題,@Autowired無法自動裝入bean,是由於對spring boot 不夠了解,先上程式碼吧
專案結構
專案說名,spring boot jdbcTemplate自定義sql 查詢mysql 資料庫資料
maven結構
基本的controller service serviceimpl 就不用說了
Data 是自己的工具類,
建好專案後先搞pom.xml,用到什麼加什麼依賴,倉庫沒有的話,下載慢的話可以配置阿里的映象maven倉庫,速度不錯,比國外的塊多了,方法:maven 的setting.xml 只需加入
pom.xml
我的pom.xml 是用到什麼引入什麼。大家不一定和我的一樣,有些是必須的,
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.21</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
屬性配置檔案applocations.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zanghan
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
Application為程式入口
package com.zanghan.myProject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main (String [] args){
SpringApplication.run(Application.class, args);
}
}
UserService 介面
package com.zanghan.myProject.service;
import org.springframework.stereotype.Component;
import com.zanghan.myProject.Data.IDataset;
@Component //這個不加的話,@Autowired將找不到bean
public interface UserService {
public IDataset queryUserList();
}
UserserviceImpl 實現類
package com.zanghan.myProject.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zanghan.myProject.Data.BaseDAO;
import com.zanghan.myProject.Data.IDataset;
import com.zanghan.myProject.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private BaseDAO baseDAO;
@Override
public IDataset queryUserList() {
String sql = "select * from user";
return baseDAO.query(sql,null);
}
}
UserController控制器
package com.zanghan.myProject.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zanghan.myProject.Data.IDataset;
import com.zanghan.myProject.service.UserService;
@RestController
@RequestMapping(“/user”)
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUsers")
public IDataset getUserList(){
IDataset userList=userService.queryUserList();
return userList;
}
}
執行直接訪問
localhost:8080/user/getUsers/
可根據你傳的sql得到資料庫的資料,
不使用我的工具類,同樣實現方式*
工具類為讀寫分離實現,BaseDao封裝jdbcTemplate處理,IDataset 是對json資料的處理接收,當然也可以不用工具類,sql ,jdbcTemplate處理.也就是修改接收查到的資料格式而已
service 層
@Component //這個不加的話,@Autowired將找不到bean
public interface UserService {
public List<Map<String, Object>> queryUserList();
}
serviceImpl實現層
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Map<String, Object>> queryUserList(){
String sql = "select * from appuser";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
}
}
控制層便不寫了,同樣呼叫service ,資料接收不同而已