使用IDEA+springboot建立ssm+gradle專案(一)
阿新 • • 發佈:2019-12-31
建立專案
新建專案,選擇SpringIntializr
,建立方式選擇spring官方連結即可。(點開此連結也可以線上建立專案)
gradle
下一步,勾選Spring Boot DevTools
,Spring web
,Mybatis Framework
,MySQL Driver
下一步,點選finish
,專案即建立完成。
連線資料庫跑通測試
建立實體
public class UserEntity {
private long id;
private String name;
private String code;
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;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
複製程式碼
建立dao
@Repository
public interface UserDao {
@Select("select * from t_user")
@Results({
@Result(property = "name",column = "name"),@Result(property = "code",column="code")
})
List<UserEntity> getAll ();
}
複製程式碼
建立controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/getAll")
public List<UserEntity> getAll() {
return userDao.getAll();
}
}
複製程式碼
在Demo9Application
檔案中中新增@MapperScan("com.example.demo9.dao")
用於掃描dao,可以避免每個dao都去新增mapper註解了
@MapperScan("com.example.demo9.dao")
@SpringBootApplication
public class Demo9Application {
public static void main(String[] args) {
SpringApplication.run(Demo9Application.class,args);
}
}
複製程式碼
配置application.properties
#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=testtest
複製程式碼
現在專案就配置完了,具體目錄結構如下:
現在可以執行程式了,執行後使用地址:http://localhost:8080/user/getAll
如果返回表中資料就說明呼叫成功了。