今天学习了SpringBoot框架个人感觉这个框架可以节省大量的开发时间所以利用闲暇时间写一篇微博,也算是做一下记录吧,有兴趣的朋友可以看下。因为我也只是入个门所以有些地方可能有些小问题。
springBoot 介绍:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
SpringBoot 是一个可完全基于注解开发的框架,并且其内置Tomcat容器。所以SpringBoot程序不需要发布到Tomcat容器中,所以我们并不需要建立web项目可以直接使用JAVA项目直接开发,但是SpringBoot必须基于Maven或者Gradle 开发。
第一步:使用maven建立一个JAVA项目
目录结构如下图:
因为我已经提前建立了一个项目所以下面使用这个项目演示。
第二步:在Maven中配置下载需要的Jar包
<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>cn.et</groupId>
<artifactId>ssmb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 表示继承SpringBoot官方的项目,可以加载该项目中使用的jar包,这个是使用SpringBoot必须要添加的-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- 表示可以发布web程序 自动启动一个tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis集成 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<!-- 操作数据源 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 添加转译jsp的jar -->
<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>
<!-- 自动监听配置文件和jsp (开发模式) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>oracle</groupId>
<artifactId>oracle</artifactId>
<version>3.2.8</version>
<scope>system</scope>
<systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>
</dependency>
<!-- dbcp数据源 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
在SpringBoot集成Mybatis中必不可少的当然是连接数据库了,所以需要一份JDBC的配置文件 application.properties(我这里是使用的Oracle)配置文件内容如下:
spring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
spring.datasource.username=scott
spring.datasource.password=tiger
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
#日志输出级别 level级别
logging.level.root=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=DEBUG
接下来就是开发的步骤了。
创建一个程序的入口,(我这里使用的是main方法作为入口,而SpringBoot只能扫描入口包下面的子包,所以其他层次必须在mian方法类的包下面)
main方法代码如下:
package cn.et.boot.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* 自动添加 spring.xml文件 并且配置 自动扫描
* 自动添加web.xml 同时 在web.xml 配置过滤器 拦截器
* @EnableTransactionManagement 表示启动全局事务支持
*/
@EnableTransactionManagement
//表示这是SpringBoot应用的入口
@SpringBootApplication
public class ExecuteProcedure {
/**
* @param args
*/
//使用main方法作为程序的入口
public static void main(String[] args) {
//SpringBoot运行项目
SpringApplication.run(ExecuteProcedure.class, args);
}
}
Control层代码如下:
package cn.et.boot.mybatis.control;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.et.boot.mybatis.entity.Emp;
import cn.et.boot.mybatis.entity.PageEntity;
import cn.et.boot.mybatis.services.EmpService;
/**
* 这里也不需要再使用@ResponseBody将返回的数据转换为Json类型了
* RestController 自动将数据转换为Json了
* @author Administrator
*
*/
@RestController
public class SpringMvcAction {
@Autowired
private EmpService sms;
/**
* 因为这里查询做了分页,所以我返回的是一个分页对象
* @param ename
* @param curPage
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping(value="/query")
public PageEntity query(String ename,int curPage) throws UnsupportedEncodingException{
ename=new String(ename.getBytes("iso-8859-1"),"utf-8");
ename="%"+ename.trim().toUpperCase()+"%";
return sms.query(ename,curPage);
}
/**
* 修改方法
* @param emp
* @param os
* @throws IOException
*/
@RequestMapping(value="/update")
public void update(Emp emp,OutputStream os) throws IOException{
try {
sms.update(emp);
os.write("1".getBytes());
} catch (Exception e) {
os.write("2".getBytes());
}
}
/**
* 删除方法
* @param empno
* @param os
* @throws IOException
*/
@RequestMapping(value="/del")
public void delete(String empno,OutputStream os) throws IOException{
try {
sms.delete(empno);
os.write("1".getBytes());
} catch (Exception e) {
os.write("2".getBytes());
}
}
@RequestMapping(value="/add")
public void add(Emp emp,OutputStream os) throws IOException{
try {
sms.add(emp);
os.write("1".getBytes());
} catch (Exception e) {
os.write("2".getBytes());
}
}
@RequestMapping("/deptno")
public List queryDeptno(OutputStream os){
return sms.queryDeptno();
}
}
服务层的代码如下(接口这里我就不写出来了,直接写实现类):
package cn.et.boot.mybatis.services.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.et.boot.mybatis.dao.EmpDao;
import cn.et.boot.mybatis.entity.Emp;
import cn.et.boot.mybatis.entity.PageEntity;
import cn.et.boot.mybatis.services.EmpService;
import cn.et.boot.mybatis.utils.PageUtils;
//启动事务
@Transactional
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDao ed;
//增加方法
public void add(Emp emp) {
ed.add(emp);
}
//删除方法
public void delete(String empno) {
ed.delete(empno);
}
//查询方法
//设置为只读,如果这里不设置为只读效率会很低
@Transactional(readOnly=true)
public PageEntity query(String ename,int curPage) {
Map totalCount=ed.queryTotal(ename);
PageEntity pe=PageUtils.call(totalCount, curPage, 6);
pe.setList(ed.query(ename, pe.getStartIndex(), pe.getEndIndex()));
return pe;
}
//修改方法
public void update(Emp emp) {
ed.update(emp);
}
//查询部门ID
public List queryDeptno() {
return ed.queryDeptno();
}
}
模型层的代码如下:
package cn.et.boot.mybatis.dao.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.et.boot.mybatis.dao.EmpDao;
import cn.et.boot.mybatis.entity.Emp;
import cn.et.boot.mybatis.mapper.EmpMapper;
@Repository
public class EmpDaoImpl implements EmpDao {
//mybatis的映射接口对象
@Autowired
private EmpMapper em;
/**
* 此方法用于增加员工信息
*/
public void add(Emp emp) {
em.addEmp(emp);
}
/**
* 此方法用于删除员工信息
*/
public void delete(String empno) {
em.deleteEmp(empno);
}
/**
* 此方法用于查询员工信息
*/
public List query(String ename,int startIndex,int endIndex) {
if(ename==null){
ename="";
}
return em.queryList(ename,startIndex,endIndex);
}
/**
* 此方法用于修改员工信息
*/
public void update(Emp emp) {
em.updateEmp(emp);
//int i=5/0;
}
/**
* 此方法用于查询部门编号
*/
public List queryDeptno() {
return em.queryDeptno();
}
/**
* 查询数据总行数
*/
public Map queryTotal(String ename) {
if(ename==null){
ename="";
}
return em.queryTotal(ename);
}
}
MyBatis的映射接口代码如下:
package cn.et.boot.mybatis.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.et.boot.mybatis.entity.Emp;
/**
* mybatis映射接口
* @author Administrator
*
*/
@Mapper
public interface EmpMapper {
/**
* 查询方法
* @param ename
* @return
* where ename like '%#{ename}%'
*/
@Select("select * from (select e.*,rownum rn from emp e where ename like #{ename}) where rn>=#{1} and rn<=#{2} ")
public List<Emp> queryList(@Param("ename")String ename,int startIndex,int endIndex);
/**
* 删除方法
* @param empno
*/
@Delete("delete emp where empno=#{empno}")
public void deleteEmp(@Param("empno")String empno);
/**
* 增加方法
* @param emp
*/
@Insert("insert into emp values(#{emp.empno},#{emp.ename},#{emp.job},#{emp.mgr},sysdate,#{emp.sal},#{emp.comm},#{emp.deptno})")
public void addEmp(@Param("emp")Emp emp);
/**
* 修改方法
*/
@Update("update emp set ename=#{emp.ename},job=#{emp.job},mgr=#{emp.mgr},hiredate=to_date(#{emp.hiredate},'yyyy/MM/dd'),sal=#{emp.sal},comm=#{emp.comm},deptno=#{emp.deptno} where empno=#{emp.empno}")
public void updateEmp(@Param("emp")Emp emp);
@Select("select deptno as dno from emp group by deptno")
public List<Map<String, String>> queryDeptno();
@Select("select count(rowid) as ct from emp where ename like #{ename}")
public Map queryTotal(@Param("ename")String ename);
}