java流行框架之Springboot项目篇
•发布于   •作者 王家明  •706 次浏览  •最后一次编辑是   •来自 博客

springboot+springdata+maven实现REST风格

1.创建maven项目

2.pom.xml配置

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency> 
    </dependencies>
   

    <build>
	    
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    
    </build>

3.springboot配置文件

application.yml

#用于设置这个开发环境
spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/test
    data-username: root
    data-password: 
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

application-dev.yml

server:
  port: 8080
  context-path: /springboot

4.启动springboot的GirlApplication.java类

@SpringBootApplication
public class BoyApplication {
	public static void main(String[] args) {
		SpringApplication.run(BoyApplication.class, args);
	}
}

5.实体类

@Entity
public class Boy {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private  int age;

    public Boy() {
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

6.控制器BoyController.java

@RestController
@RequestMapping("/boy")
public class BoyController {
    @Autowired
    private BoyDao boyDao;
    @Autowired
    private BoyService boyService;
}

7.业务层接口BoyService.java

public interface BoyService {
    
}

8.业务层接口实现类BoyServiceImpl.java

@Service
@Transactional  //加入事务
public class BoyServiceImpl implements BoyService {
    @Autowired
    private BoyDao boyDao;
}

9.数据访问层BoyDao.java

    只需要继承JpaRepository,无需任何实现

public interface BoyDao extends JpaRepository { }

10.页面访问IndexController.java

@Controller
public class IndexController {
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
}

访问主页:http://localhost:8080/springboot/index

11.实现功能

  • 查询全部

index.html添加:

 

<a href="/springboot/boy/boy">查询所有</a>

BoyController.java添加:

    //@RequestMapping(value="/boy",method = RequestMethod.GET)
    @GetMapping("/boy")
    public List findAll() {
        return boyDao.findAll();
    }
浏览器访问:
http://localhost:8080/springboot/boy/boy


  • 新增

index.html添加:


<form action="/springboot/boy/add" method="post">

    姓名<input type="text" name="name" value=""/>

    年龄<input type="text" name="age" value=""/>

    <input type="submit" value="新增"/>

</form>

BoyController.java添加:

    //@RequestMapping(value="/add",method = RequestMethod.POST)
    @PostMapping("/add")
    public Boy add(Boy boy) {
        return boyService.add(boy);//返回对象
    }

BoyServiceImpl.java添加:

    public Boy add(Boy boy){
        return boyDao.save(boy);
    }
  • 根据ID查询

index.html添加:

 

<a href="/springboot/boy/findOne/1">根据ID查询</a>

BoyController.java添加:

    //@RequestMapping(value="/findOne/{id}",method = RequestMethod.GET)
    @GetMapping("/findOne/{id}")
    public Boy findOne(@PathVariable(name = "id")Long id){
        return boyDao.findOne(id);
    }
  • 根据ID更新

index.html添加:

<form action="/springboot/boy/update/3" method="post" enctype="application/x-www-form-urlencoded">

    <input type="hidden" name="_method" value="PUT"/>

姓名<input type="text" name="name" value=""/>

年龄<input type="text" name="age" value=""/>

    <input type="submit" value="根据ID更新"/>

</form>

BoyController.java添加:

    //@RequestMapping(value="/update/{id}",method = RequestMethod.PUT)
    @PutMapping("/update/{id}")
    public Boy update(@PathVariable(name = "id") Long id, Boy boy) {
    	System.out.println(id);//3
    	System.out.println(boy);//Boy [id=3, name=heheheh, age=222]
        return boyDao.save(boy);
    }
  • 根据ID删除

index.html添加:

 

<form action="/springboot/boy/delete/5" method="post">

    <input type="hidden" name="_method" value="DELETE"/>

    <input type="submit" value="根据id删除"/>

</form>

BoyController.java添加:

    //@RequestMapping(value="/delete/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable(name = "id")Long id){
        boyDao.delete(id);
    }
  • 根据age查询

index.html添加:

 

<a href="/springboot/boy/findByAge/22">根据age查询</a>

BoyController.java添加:

    //@RequestMapping(value="/findByAge/{age}",method = RequestMethod.GET)
    @GetMapping("/findByAge/{age}")
    public List findByAge(@PathVariable(name = "age")int age){
        return boyDao.findByAge(age);
    }

BoyDao.java添加:

public List findByAge(int age);


0 回复
回到顶部

©2017 Powered by 三十三行伪代码
皖ICP备17005175号-3