Spring Boot學習筆記之使用Spring Boots實現資料庫操作(IntelliJ IDEA+navicat for Sql Server)
這裡使用Spring Boot實現了一個簡單的專案,能夠實現簡單的資料庫操作,工具使用的是IntelliJ IDEA2017.3,資料庫工具使用的是navicat for Sql Server,語言使用的Java。
1.新建一個空的Maven專案
2.匯入需要的jar包
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>MavenWebProject02</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- tomcat的支援 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 引入jdbc的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 引入SQL Server的jar包 --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> </dependencies> <!-- 以下為阿里雲的倉庫連結 --> <repositories> <repository> <id>aliyun-repos</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
注:此處遇到了Failuer to find com.microsoft.sqlserver:sqljdbc4:jar:4.0的問題,maven專案引入sqljdbc4 找不到包,添加了【以下為阿里雲的倉庫連結】下面的配置後就不報錯了,此處原因不明。
3.新建資料庫並配置資料庫資訊
我們使用navicat for sql server新建一個測試庫test01,然後新建一個表t_test,此處需要注意,標識這裡需要勾選,navicat for sql server在只能在新建表的時候設定自動遞增,就是勾選此處的標識:
我們的資料相關配置資訊application.properties:資料庫名為test01,使用者名稱為sa,密碼為sa123456:
server.port=8081 #資料庫使用者名稱密碼設定 spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.url=jdbc\:sqlserver\://127.0.0.1\:1433;databaseName\= test01 spring.datasource.username=sa spring.datasource.password=sa123456 #資料庫連線池配置 spring.datasource.tomcat.max-active=20 spring.datasource.tomcat.max-idle=8 spring.datasource.tomcat.min-idle=8 spring.datasource.tomcat.initial-size=10
4.相關程式碼
User.java:
public class User {
private Long id;
private String name;
public User(Long id, String name) {
this.id = id;
this.name = name;
}
public User(String name) {
this.name = name;
}
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserDao.java:
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(User user){
jdbcTemplate.update("insert into t_user(name) values(?)",user.getName());
}
public User get(Long id){
RowMapper<User> rowMapper = new BeanPropertyRowMapper <User>(User.class);
User user = jdbcTemplate.queryForObject("select * from t_user where id = ?",rowMapper,id);
return user;
}
}
UserController.java:
@RestController
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/user/hello")
public String hello(){
return "hello";
}
@RequestMapping("/user/save")
public String save(String name){
User user = new User(name);
userService.save(user);
return "saved successfully";
}
@RequestMapping("/user/get")
public User get(Long id){
User user = userService.get(id);
return user;
}
}
IUserService介面:
public interface IUserService {
void save(User user);
User get(Long id);
}
UserServiceImpl.java:
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private UserDao dao;
@Override
public void save(User user) {
dao.save(user);
}
@Override
public User get(Long id) {
return dao.get(id);
}
}
主方法App.java:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
工程結構如下:
5.程式執行效果展示
App.java方法啟動成功後,我們在瀏覽器中輸入http://localhost:8081/user/save?name=mytest,可以看到如下執行結果:
我們檢視資料庫(測試sql語句的時候插入了兩條資料,所以此處id為3):
我們在瀏覽器中輸入http://localhost:8081/user/get?id=3,可以看到如下結果: