[IDEA]::springboot框架搭建,整合mysql資料庫
阿新 • • 發佈:2018-12-25
1.完整工程目錄圖如下
2.開啟idea,左上角 file–>new–>project–>選擇spring Initializar,選擇 jdk ,然後next
3,出現下圖,繼續next,這裡可以修改你想要的檔名
4.只選中web,next
5.可以工程名,和工程存放的資料夾,點選next之後,等待
6.開啟pom.xml 新增如下依賴
<!--連線資料庫驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--支援通過jdbc連線資料庫--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
7.在application.properties檔案裡進行連線mysql資料庫的配置
spring.datasource.url=jdbc:mysql://localhost:3306/mytime?characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
8.在mytime(資料庫名),新建一個t_user表,並新增兩條資料
-- 建表語句:
CREATE TABLE `t_user` (
`login_name` varchar(255) DEFAULT NULL COMMENT '登入名',
`password` varchar(255) DEFAULT NULL COMMENT '密碼',
`phone` varchar(255) DEFAULT NULL COMMENT '手機號碼',
`email` varchar(255) DEFAULT NULL COMMENT '郵箱',
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- 新增資料
INSERT INTO `t_user` VALUES ('ning', '202CB962AC59075B964B07152D234B70', '17730012680', ' [email protected]', '1');
INSERT INTO `t_user` VALUES ('郭靖', '202CB962AC59075B964B07152D234B70', '18656004522', '[email protected]', '2');
9.在com.example.demo下新建包 entity,建立t_user的實體類
package com.example.demo.entity;
public class PubUser {
private String loginName;
private String password;
private String phone;
private String email;
private String id;
public PubUser() {
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
10.在test 檔案下的DemoApplicationTests類下寫測試方法
package com.example.demo;
import com.example.demo.entity.PubUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Resource
private JdbcTemplate jdbcTemplate;
@Test
public void contextLoads() {
}
@Test
public void testMysql(){
String sql = " select login_name,email from t_user" ;
List<PubUser> userlist = jdbcTemplate.query(sql, new RowMapper<PubUser>() {
@Override
public PubUser mapRow(ResultSet resultSet, int i) throws SQLException {
PubUser user = new PubUser();
user.setLoginName(resultSet.getString("login_name"));
user.setEmail(resultSet.getString("email"));
return user;
}
});
System.out.println("查詢成功");
for (PubUser user:userlist) {
System.out.println("使用者名稱為"+user.getLoginName()+"的郵箱是"+user.getEmail());
}
}
}
11.執行步驟
選中要測試的方法,右擊,選則 run testMysql,效果如下,即大功告成