前言:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,应用于快速应用开发领域
所需支持:Maven构建提供支持或Gradle ,不懂Maven或者Gradle 的建议还是先去学习学习再研究SpringBoot; JDK1.7
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.et</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!--启用 actuator框架功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<!--表示可以发布web程序 并启动一个tomcat-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加转译jsp的jar 否则无法转译jsp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--操作数据源 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>oracle</groupId>
<artifactId>oracle</artifactId>
<version>3.2.8</version>
<scope>system</scope>
<systemPath>D:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Controller层常用注解
package cn.et.lesson01.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* RestController 目标就是配置
* 作用:自动配置 消息转换器 jackson
* 并在action上添加@ResponseBody
*
* EnableAutoConfiguration 旧版中使用 无法扫描其他类中的注解
* 自动添加Spring.xml 并自动扫描
* 自动添加web.xml 并自动生成web.xml中的配置
*
* SpringBootApplication 新版中用的注解 功能比EnableAutoConfiguration更强大
* 可以扫描到其他类的注解,甚至是架包中的注解
* @author Ma-PC
*/
@RestController
@SpringBootApplication
public class DaoController {
@Autowired
private EmpDaoImpl dao;
@RequestMapping("/hello")
public List hello(){
return dao.selectEmp();
}
}
Entity实体类:
package cn.et.entity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import org.hibernate.annotations.Type;
/**
* Entity注解 表示该类是用于映射的实体类 会自动进行结果集映射
* @author Ma-PC
*/
@Entity
public class Emp implements java.io.Serializable {
@Id //oid
/* @GeneratedValue指定了标识符的生成策略。JPA提供了4种标准用法。
(1)AUTO:根据不同的数据库选择不同的策略。
(2)TABLE:使用表保存id值。
(3)INDENITY:使用数据库自动生成主键(主要是自动增长类型,如MySql、SQL Server)。
(4)SEQUENCE:使用序列创建主键(如Oracle)。*/
@GeneratedValue(strategy=GenerationType.AUTO)
@SequenceGenerator(name="sequence",sequenceName="HIBERNATE_SEQUENCE") //指定使用数据库中的哪个序列
private int empno;
private String ename;
private String job;
private Short mgr;
@Type(type="date")
private Date hiredate;
private Double sal;
private Double comm;
private Short deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return this.ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return this.job;
}
public void setJob(String job) {
this.job = job;
}
public Short getMgr() {
return this.mgr;
}
public void setMgr(Short mgr) {
this.mgr = mgr;
}
public Double getSal() {
return this.sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return this.comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Short getDeptno() {
return deptno;
}
public void setDeptno(Short deptno) {
this.deptno = deptno;
}
public Date getHiredate() throws ParseException {
return hiredate;
}
public void setHiredate(String hiredate) throws ParseException {
Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(hiredate);
this.hiredate = parse;
}
}
Service层
/**
* Transactional 表示该类中的所有方法都使用事务
* @author Ma-PC
*/
@Transactional
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDao dao;
}
启动类-main方法
@EnableTransactionManagement //表示启动全局事务支持
@SpringBootApplication
public class Start {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Start.class, args);
}
}
Configuration 其他配置
package cn.et.lesson01.ioc;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Configuration 标识该类为一个bean的容器
*
*
* @author Administrator
*/
@Configuration
public class BeanContainer {
/**
* @Bean
* 调用该方法产生一个bean
* 方法名 就是 id;
*
* @ConfigurationProperties(prefix="dbcpsource")
* 自动扫描资源文件 并且将资源文件中前缀包含"dbcpsource"的四要素
* 自动填充到BasicDataSource中;
*
* @return
*/
@Bean
@ConfigurationProperties(prefix="dbcpsource")
public DataSource dataSource(){
BasicDataSource dataSource = new BasicDataSource();
return dataSource;
}
}