spring-boot 入门 ssmb小例子

来源:互联网 发布:美浓烧底标 知乎 编辑:程序博客网 时间:2024/06/06 03:42

pom.xml 依赖包

    <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>



application.properties 配置数据源四要素

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orclspring.datasource.username=scottspring.datasource.password=tigerspring.datasource.driver-class-name=oracle.jdbc.OracleDriver

mapper 层对数据库的curd

package cn.et.mapper;import java.util.List;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import cn.et.entity.Emp;//实例化这个类@Mapperpublic interface EmpMapper {       @Delete({        "delete from SCOTT.EMP",        "where EMPNO = #{empno}"    })    public  int deleteByPrimaryKey(Short empno);        @Insert({    "insert into emp(empno,sal,ename,job) values((select max(empno)+1 from emp),#{sal},#{ename},#{job})"    })    public int insert(Emp record);          @Select({        "select EMPNO, ENAME, JOB, SAL from (select t.*,rownum as ro from SCOTT.EMP t where ENAME LIKE #{0}) where ro BETWEEN #{1} AND #{2} "    })    public List<Emp> selectByPrimaryKey(String name,int startIndex,int endIndex);        @Select({        "select count(rowid) from SCOTT.EMP where ENAME LIKE #{name}"    })    public int getCount(String name);      @Update({            "update SCOTT.EMP set ENAME = #{ename},JOB = #{job},SAL = #{sal} where EMPNO = #{empno}"    })    public int updateByPrimaryKey(Emp record);}

service层

package cn.et.service.imp;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import cn.et.entity.Emp;import cn.et.entity.PageEntity;import cn.et.mapper.EmpMapper;import cn.et.service.EmpService;import cn.et.utils.PageUtils;//调用事物@Transactional//实例化类@Servicepublic class EmpServiceImp implements EmpService {@Autowiredprivate EmpMapper em;//此方法不启用事物@Transactional(readOnly=true)public PageEntity query(Emp ui, int curPage) {int count = em.getCount("%"+ui.getEname()+"%");PageEntity pe = PageUtils.getPageEntity(count, 5, curPage);List<Emp> list = em.selectByPrimaryKey("%"+ui.getEname()+"%",pe.getstartIndex() , pe.getEndIndex());pe.setData(list);return pe;}public void add(Emp ui) {em.insert(ui);}public void delete(Emp ui) {em.deleteByPrimaryKey(ui.getEmpno());}public void update(Emp ui) {em.updateByPrimaryKey(ui);}}

control 层

package cn.et.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.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import cn.et.entity.Emp;import cn.et.entity.PageEntity;import cn.et.service.EmpService;//实例化这个类@RestControllerpublic class EmpAction {@Autowiredprivate EmpService es;@RequestMapping("queryEmp")public PageEntity query(String ename,int curPage){Emp ui=new Emp();ui.setEname(ename);PageEntity pe = es.query(ui, curPage);return pe;}@RequestMapping("addEmp")public void add(Emp ui,OutputStream os) throws UnsupportedEncodingException, IOException{try {es.add(ui);os.write("1".getBytes("utf-8"));} catch (Exception e) {os.write("0".getBytes("utf-8"));e.printStackTrace();}}@RequestMapping("updateEmp")public void update(Emp ui,OutputStream os) throws UnsupportedEncodingException, IOException{try {es.update(ui);os.write("1".getBytes("utf-8"));} catch (Exception e) {os.write("0".getBytes("utf-8"));e.printStackTrace();}}@RequestMapping("deleteEmp")public void delete(Emp ui,OutputStream os) throws UnsupportedEncodingException, IOException{try {es.delete(ui);os.write("1".getBytes("utf-8"));} catch (Exception e) {os.write("0".getBytes("utf-8"));e.printStackTrace();}}}


Utils包 service层的调用类

package cn.et.utils;import cn.et.entity.PageEntity;/*//开始位置private int startIndex;//结束位置private int endIndex;//总条数private int count;//当前页数privateint curPage;//下一页private int nextPage;//上一页private int prePage;//总页数private int pageCount;*  * 16 5 * 1,2,3,4,5   1  5  开始位置=(页数-1)*每页行数+1; * 6,7,8,9,10   2结束=页数*行数 * 11,12,13,14,15   * 16,  4**/public class PageUtils {//通过数据的总条数,每页的数据条数,当前第几页;计算所需要的数据public static PageEntity getPageEntity(int count,int curCount,int curPage){PageEntity pe=new PageEntity();pe.setCount(count);pe.setCurPage(curPage);//计算总页数int pageCount=count%curCount==0? count/curCount:count/curCount+1;pe.setPageCount(pageCount);//计算上一页int prePage=curPage==1? curPage:curPage-1;pe.setPrePage(prePage);//计算下一页int nextPage=curPage==pageCount? curPage:curPage+1;pe.setNextPage(nextPage);//计算每页开始位置int startIndex=(curPage-1)*curCount+1;pe.setstartIndex(startIndex);//计算每页结束位置int endIndex=curPage==pageCount? count:curCount*curPage;pe.setEndIndex(endIndex);return pe;}}

entity层 Emp实体类


package cn.et.entity;import java.math.BigDecimal;import java.util.Date;public class Emp {        private Short empno;        private String ename;        private String job;       private BigDecimal sal;    public Short getEmpno() {        return empno;    }        public void setEmpno(Short empno) {        this.empno = empno;    }        public String getEname() {        return ename;    }       public void setEname(String ename) {        this.ename = ename;    }        public String getJob() {        return job;    }        public void setJob(String job) {        this.job = job;    }        public BigDecimal getSal() {        return sal;    }        public void setSal(BigDecimal sal) {        this.sal = sal;    }    @Overridepublic String toString() {return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job+ ", sal=" + sal+  "]";}    }

entity层 工具实体类

package cn.et.entity;import java.util.List;public class PageEntity {//开始位置private int startIndex;//结束位置private int endIndex;//总条数private int count;//当前页数privateint curPage;//下一页private int nextPage;//上一页private int prePage;//总页数private int pageCount;/** * 用来保存当前页的数据 * */private List data;public int getstartIndex() {return startIndex;}public void setstartIndex(int startIndex) {this.startIndex = startIndex;}public int getEndIndex() {return endIndex;}public void setEndIndex(int endIndex) {this.endIndex = endIndex;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getCurPage() {return curPage;}public void setCurPage(int curPage) {this.curPage = curPage;}public int getNextPage() {return nextPage;}public void setNextPage(int nextPage) {this.nextPage = nextPage;}public int getPrePage() {return prePage;}public void setPrePage(int prePage) {this.prePage = prePage;}public List getData() {return data;}public void setData(List data) {this.data = data;}public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {this.pageCount = pageCount;}}

mian方法程序启动入口

package cn.et;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.transaction.annotation.EnableTransactionManagement;//让service类中的事物生效,启动全局事物@EnableTransactionManagement//加载全局配置@SpringBootApplicationpublic class StartSSMB {public static void main(String[] args) {SpringApplication.run(StartSSMB.class, args);}}