首先确定你的IDEA是不是UItimate 如果选择的Community不会出现Spring Initializr
选择File 新建Project
点击下一步选择左边的web勾选web
点击完下一步之后 在出现的界面右下角 点击import Changes
接下写一个测试的Controller 必须在创建完项目之后生成的目录下写 负责可能会扫不到包
写完之后启动项目,一般情况下,IDEA自己会直接帮你生成一个Configuration,如果没有生成,自己手动生成一个。
点击+号选择spring boot
选择你的主加载类(spring boot 会自动生成一个,在你创建的目录下),点击ok
启动项目在浏览器查看是否成功
我这边端口号是8080
成功
如果需要连接数据库需要引入下面这些依赖(本篇用的是JdbcTemplate操作数据库)
<!--启用jdbc操作数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--连接mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在applicationContext文件中配置数据源
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/user?characterEncoding=utf8&useSSL=true
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
然后开始写service
package com.example.bpm.Service;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface TestService {
public List findAll();
}
然后开始写impl
package com.example.bpm.Service.impl;
import com.example.bpm.Service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class TestServiceImpl implements TestService{
@Autowired
private JdbcTemplate jdbcTemplate;
public List findAll(){
String sql="SELECT * from user ";
List list =jdbcTemplate.queryForList(sql);
return list;
}
}
在controller中调用
package com.example.bpm.Controller;
import com.example.bpm.Service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/")
public String test(){
List list = testService.findAll();
return list.toString();
}
}
重启项目运行
没有任何问题
在这边我生成model的时候要加载Hibernate的配置文件才能生成 感觉有点绕路子了 如有大神看到,请赐教。
生成model
选择Project Structure
在弹出的界面选择Modules点击中间的+号选择hibernate 选中hibernate点击右边的+号自动添加配置文件
添加完成之后点击界面右边框上的database
点击+号选择datasource---MySql
点击之后一次填写地址---数据库名---用户名----密码
连接成功之后就会出现
然后点击界面左边框上的Persistence
package com.example.bpm.model;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "user", schema = "user", catalog = "")
public class UserEntity {
private int id;
private String username;
private String password;
private String usermessage;
private String email;
private String role;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Basic
@Column(name = "usermessage")
public String getUsermessage() {
return usermessage;
}
public void setUsermessage(String usermessage) {
this.usermessage = usermessage;
}
@Basic
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Basic
@Column(name = "role")
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
UserEntity that = (UserEntity) object;
return id == that.id &&
Objects.equals(username, that.username) &&
Objects.equals(password, that.password) &&
Objects.equals(usermessage, that.usermessage) &&
Objects.equals(email, that.email) &&
Objects.equals(role, that.role);
}
@Override
public int hashCode() {
return Objects.hash(id, username, password, usermessage, email, role);
}
}
有什么不对的地方多交流 多交流 多交流