springboot的入门请参考:https://blog.csdn.net/hanjun0612/article/details/81538449
这里就简单看下搭建:
一,看一下项目结构:
创建一个 Maven项目
App.class的代码如下:
package com.App;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
二,创建application.properties
#修改tomcat默认端口号
server.port=8090
#修改context path
server.context-path=/test
#配置数据源信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=root
#配置jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
三,model
package com.App.model;
import javax.persistence.*;
@Entity
@Table(name="user")
public class User {
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
private String userName;
private String password;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
四,dao
package com.App.dao;
import com.App.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface UserDao extends JpaRepository<User, Integer> {
@Query("from User where id =:id ")
User getUser(@Param("id") Integer id);
}
五,
① service
service文件夹下,还有一个imp文件夹,用来实现
package com.App.service;
import com.App.model.User;
/**
* Created by Tyler on 2018/8/9
*/
public interface UserService {
void save(User user);
User getabc(Integer id);
}
② Impl
package com.App.service.Impl;
import com.App.dao.UserDao;
import com.App.model.User;
import com.App.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
public void save(User user){
userDao.save(user);
}
@Override
public User getabc(Integer id) {
return userDao.getUser(id);
}
}
六 controller
package com.App.controller;
import com.App.model.User;
import com.App.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("hello")
public class HelloController {
@Autowired
UserService userService;
@RequestMapping(value = "say",method = RequestMethod.GET)
public String sayHello(User user) {
// user.setUserName("he");
// user.setPassword("he1");
// user.setAge(20);
// userService.save(user);
User u=userService.getabc(1);
return "hello ,spring boot";
}
}