springboot之热部署及Spring JPA简单应用
来源:互联网 发布:mac和windows双系统 编辑:程序博客网 时间:2024/06/06 02:01
关于spring-boot热部署问题,就是修改代码,不需要重启工程,节约不必要的时间成本。
首先,实行简单的springloaded 热部署,只需要在pom.xml配置:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build>
(1)在启动时,选择Maven build ,然后spring-boot:run
(2)或者设置VM参数:
-javaagent:./lib/springloaded-1.2.4.RELEASE.jar -noverify
以上两种启动方式,只能达到修改代码及返回值时,生效。如果我在Controller里新增加一个方法,则不好用了。
这个为了解决问题,我们使用devtools热部署spring-boot。
devtools热部署在pom.xml中:
<!-- spring boot devtools 依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency>##########################################################<!--在build中添加插件--><!-- 这是spring boot devtool plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart --> <fork>true</fork> </configuration> </plugin>
配置好。再启动spring-boot工程,无论是添加方法,还是新的Controller,系统都会重新加载。
下面看下Spring Data JPA,更方便的操作数据库
首先要:在pom.xml中引入mysql驱动包和spring-data jpa的依赖
<!-- 添加MySQL数据库驱动依赖包. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 添加Spring-data-jpa依赖. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
其次,在src/main/resources下创建application.properties文件,内容
###########################################################datasource --数据库连接信息########################################################spring.datasource.url = jdbc:mysql://localhost:3306/learnspring.datasource.username = rootspring.datasource.password = rootspring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10########################################################### Java Persistence Api -- Spring jpa配置######################################################### Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sql queryspring.jpa.show-sql = true# Hibernate ddl auto (create, create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategy#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy# stripped before adding them to the entity manager)spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
我们需要建一个实体类Student,只需要在Student里添加相应的注解,数据库就会自动生成表结构。如
@Entitypublic class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; private int age; public int getId() { return id; } public void setId(int 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; }}
注解在类上用@Entity(name=”表名”),
主键用@Id,自增的生成策略@GeneratedValue(strategy=GenerationType.AUTO)
其次我们只需要,编写一个StudentRepository接口,让它继承extends CrudRepository,然后在service里注入就可以,调用继承的方法crud,最后编写controller和service代码,就可以测试。
StudentRepository接口
public interface StudentRepository extends CrudRepository<Student, Integer> {}
在service(为了方便直接用的具体类,没有使用接口)中可以操作:
@Servicepublic class StudentService { @Resource private StudentRepository studentRepository; @Transactional public void save(Student student){ studentRepository.save(student); } @Transactional public void delete(int id){ studentRepository.delete(id); } public Iterable<Student> getAll(){ return studentRepository.findAll(); }}
CrudRepository继承Repository,我们也可以自己继承Repository,像查询等功能。
自定义StudentRepository2 如下:
public interface StudentRepository2 extends Repository<Student, Integer> { public Student findStudentByName(String name); @Query(value="from Student where name = :cname") public Student findMyStudentByName(@Param("cname") String name);}
除了自定义Repository,还有很多,如PagingAndSortingRepository,JpaRepository等。
如何引入JdbcTemplate?
首先在pom.xml中引入jdbc依赖
<!-- 如果引入 pring-data-jpa依赖,则不需要引入--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency>
其次在Dao里注入JdbcTemplate即可。
@Repositorypublic class StudentDao { @Resource private JdbcTemplate jdbcTemplate; public Student getStudentByCondition(String name){ String sql = " select * from student where name = ?"; RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class); return jdbcTemplate.queryForObject(sql, new Object[]{name}, rowMapper); }}
如何定义一个全局异常处理?
1、新建一个Class,这里取名为GlobalDefaultExceptionHandler
2、在class上添加注解,@ControllerAdvice;
3、在class中添加一个方法
4、在方法上添加@ExcetionHandler拦截相应的异常信息;
5、如果返回的是View – 方法的返回值是ModelAndView;
6、如果返回的是String或者是Json数据,那么需要在方法上添加@ResponseBody注解.
@ControllerAdvicepublic class GlobalDefaultExceptionHandler { @ExceptionHandler @ResponseBody public String defaultExceptionHandler(HttpServletRequest request,Exception e){ //是返回的String. //ModelAndView -- 介绍 模板引擎...? //ModelAndView mv = new ModelAndView(); //mv.setViewName(viewName); return "服务器错误"; }}
spring-boot 默认端口是8080,如何修改这些配置信息?
只需要在application.properties中配置:
#server.port=8080#server.address= # bind to a specific NIC#server.session-timeout= # session timeout in seconds#the context path, defaults to '/'#server.context-path=/spring-boot#server.servlet-path= # the servlet path, defaults to '/'#server.tomcat.access-log-pattern= # log pattern of the access log#server.tomcat.access-log-enabled=false # is access logging enabled#server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers#server.tomcat.remote-ip-header=x-forwarded-for#server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)#server.tomcat.background-processor-delay=30; # in seconds#server.tomcat.max-threads = 0 # number of threads in protocol handler#server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding
那么如何整和Mybatis呢?现在就举一个最简单例子:
1、首先在pom.xml中引入Mybatis依赖
<!-- spring-boot mybatis依赖: --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
2、在启动类上添加扫描的注解
@SpringBootApplication@MapperScan("com.itcast.*")//扫描:该包下相应的class,主要是MyBatis的持久化类.public class App{}
3、创建一个Mapper接口
public interface StudentMapper { @Select("select * from student where name =#{name}") public List<Student> selectStudentByName(String name); @Select("select *from student where id = #{id}") public Student getById(long id); @Insert("insert into student(name,age) values(#{name},#{age})") @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id") public long save(Student student);}
最后编写测试代码即可。
- springboot之热部署及Spring JPA简单应用
- Spring Boot学习之旅:(三)springboot热部署
- 【系统学习SpringBoot】再遇Spring Data JPA之JPA应用详解(自定义查询及复杂查询)
- ecplise之SpringBoot热部署
- Spring Boot热部署—SpringBoot + Devtools
- 极其简单的 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)
- 《Springboot极简教程》SpringBoot热部署应用Developer tools
- SpringBoot初始教程之热部署(五)
- springboot开启热部署之Idea&Gradle
- (6)学习SpringBoot之 热部署
- SpringBoot系列之二intelliJ idea 配置Spring boot 热部署
- spring boot之热部署
- spring boot 之热部署
- springboot-热部署
- SpringBoot热部署
- springboot实现热部署
- springboot热部署
- SpringBoot热部署配置
- 分冶算法--循环赛
- Hibernate框架学习1:
- 数字类型
- 锋利的jQuery学习笔记(三)————DOM对象和jQuery对象
- 浏览器渲染流水线解析(三)
- springboot之热部署及Spring JPA简单应用
- Android 第三方微博登录 sso package or sign error 解决办法
- Git版本管理使用
- JQ的几种选择器
- 表、栈、队列联系
- iostope用法示例之过滤篇
- 创建ROS消息和ROS服务
- 关于python的学习笔记
- java 获取字符串或文件的md5值