spring boot入门
来源:互联网 发布:西安汇知中学四大恶霸 编辑:程序博客网 时间:2024/06/10 16:52
(1)需要使用maven来构建项目,加载起来很简单,直接在POM.xml文件引入依赖即可!运行起来也像普通的Java程序一样,一个应用占用Tomcat容器的一个端口,像普通程序一样停止Java程序就把相应的端口释放掉了。
因为像普通程序,所以只有一个main方法,而且必须要有一个main,应该是作为程序的入口,加载一些上下文,容器之类的吧
因为不同项目不同的tomcat端口,所以url不用加项目名称
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ywsg</groupId> <artifactId>springbootdemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springbootdemo Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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> </dependencies> <build> <finalName>springbootdemo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
(2)Spring boot加载简单,用起来也简单,如结和JPA实现CRUD和事务管理
package hello;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class StudentController {@Autowiredprivate StuRepository stuRepository;@RequestMapping("/stus")public List<Student> stuList() {return stuRepository.findAll();}@RequestMapping("/add")public Student add(@RequestParam("age") String age) {Student stu = new Student();stu.setAge(age);return stuRepository.save(stu);}@RequestMapping("/getById")public Student get(@RequestParam("id") Integer id) {return stuRepository.findOne(id);}@RequestMapping("/update")public Student update(@RequestParam("id") Integer id, @RequestParam("age") String age) {Student stu = new Student();stu.setAge(age);stu.setId(id);System.out.println("update...");// 跟新也用savereturn stuRepository.save(stu);}@RequestMapping("/remove")public void remove(@RequestParam("id") Integer id) {stuRepository.delete(id);}@RequestMapping("/getByAge")public List<Student> remove(@RequestParam("age") String age) {return stuRepository.findByAge(age);}// 事务管理@Autowiredprivate StudentService studentService;@RequestMapping("/addTwo")public void addTwo() {studentService.addTwo();}}
package hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class StudentService {@Autowiredprivate StuRepository stuRepository;@Transactionalpublic void addTwo() {Student stu1 = new Student();stu1.setAge("1");stuRepository.save(stu1);Student stu2 = new Student();stu2.setAge("5678");stuRepository.save(stu2);}}
package hello;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;public interface StuRepository extends JpaRepository<Student, Integer>{// 通过年龄来查询// 只要写个方法就好了,都不要实现了,但是方法名有规范public List<Student> findByAge(String age);}
注意:StuRepository extends JpaRepository<Student, Integer>的使用
(3)在application.properties文件配置必要的信息:
server.port=8000spring.datasource.url=jdbc:mysql://localhost/testspring.datasource.username=rootspring.datasource.password=1spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql:true
总体来说,用springboot确实简化了不少
1 0
- 【Spring Boot】Spring Boot 入门
- Spring Boot 快速入门
- spring boot 入门
- Spring Boot 入门
- Spring Boot 入门
- spring boot简易入门
- Spring boot入门
- Spring Boot 快速入门
- Spring boot快速入门
- Spring boot 入门 实例
- Spring Boot 快速入门
- spring boot入门(一)
- spring boot入门例
- Spring Boot入门例子
- spring boot 入门
- Spring Boot入门
- Spring Boot 快速入门
- Spring Boot 入门笔记
- java几种常见的数据库连接
- ubuntu下 python链接mysql
- CentOS7.2下安装pdf2htmlEX
- Linux文档格式乱码与转换
- leetcode 476. Number Complement
- spring boot入门
- 串口
- 【Leetcode】111. Minimum Depth of Binary Tree
- css 中的浮动
- Android OpenGL ES 2.0学习研究 (一)
- LDD3源码分析之访问控制
- HTML name、id、class 的区别
- caffe增加自己的layer实战(上)--caffe学习(10)
- Webview File域同源策略绕过漏洞