整合JPA
同样的整合JPA我们只需要启动我们SpringBoot已经集成好的模块即可。
添加依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--启动JPA组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
启动JPA组件后直接配置数据库连接信息就可以使用JPA功能。
Application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
实体类:Employee.java
@Table(name="tal_employee")
@Entity
public class Employee implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name="last_Name")
private String lastName;
private String email;
private String gender;
//get set 省略
}
EmployeeDao接口:
public interface EmployeeDao extends JpaRepository<Employee, Integer>{
}
EmployeeController.java:
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@ResponseBody
@RequestMapping("/emps")
public List<Employee> getEmployees(){
List<Employee> employees = employeeDao.findAll();
System.out.println(employees);
return employees;
}
}
整合MyBatis
引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入对JDBC的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--引入对logging的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- SpringBoot MyBatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##############datasource classpath 数据连接池地址##############
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#指定我们的mapper.xml位置
mybatis.mapper-locations=classpath:com/simple/springboot/mybatis/dao/mapper/*.xml
#entity.class 指定我们实体类所在包位置
mybatis.type-aliases-package=com.simple.springboot.mybatis.entity
当然这里还有很多属性如果想要使用可以参考官方文档。到了这里其他就不写了,把他当作SSM使用就ok。
注意事项:在我们的Dao层接口中一定要在类上加上注解@Mapper否则无法扫描到。
AOP功能使用
在我们SpringBoot中使用AOP非常简单。
package com.simple.springboot.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SpringBootAspect {
/**
* 定义一个切入点
* @author:SimpleWu
* @Date:2018年10月12日
*/
@Pointcut(value="execution(* com.simple.springboot.util.*.*(..))")
public void aop(){}
/**
* 定义一个前置通知
* @author:SimpleWu
* @Date:2018年10月12日
*/
@Before("aop()")
public void aopBefore(){
System.out.println("前置通知 SpringBootAspect....aopBefore");
}
/**
* 定义一个后置通知
* @author:SimpleWu
* @Date:2018年10月12日
*/
@After("aop()")
public void aopAfter(){
System.out.println("后置通知 SpringBootAspect....aopAfter");
}
/**
* 处理未处理的JAVA异常
* @author:SimpleWu
* @Date:2018年10月12日
*/
@AfterThrowing(pointcut="aop()",throwing="e")
public void exception(Exception e){
System.out.println("异常通知 SpringBootAspect...exception .." + e);
}
/**
* 环绕通知
* @author:SimpleWu
* @throws Throwable
* @Date:2018年10月12日
*/
@Around("aop()")
public void around(ProceedingJoinPoint invocation) throws Throwable{
System.out.println("SpringBootAspect..环绕通知 Before");
invocation.proceed();
System.out.println("SpringBootAspect..环绕通知 After");
}
}
任务调度
SpringBoot已经集成好一个调度功能。
@Component
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
/**
* 任务调度,每隔5秒执行一次
* @author:SimpleWu
* @Date:2018年10月12日
*/
@Scheduled(fixedRate = 1000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}
然后启动的时候我们必须要在主函数类上加上注解:@EnableScheduling(翻译过来就是开启调度)
/**
* SpringBoot使用任务调度
* @EnableScheduling标注程序开启任务调度
* @author :SimpleWu
* @Date:2018年10月12日
*/
@SpringBootApplication
@EnableScheduling
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
针对于上面的知识点我总结出了有1到5年开发经验的程序员在面试中涉及到的绝大部分架构面试题及答案做成了文档和架构视频资料免费分享给大家(包括Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术资料),希望能帮助到您面试前的复习且找到一个好的工作,也节省大家在网上搜索资料的时间来学习,也可以关注我一下以后会有更多干货分享。