spring boot web

来源:互联网 发布:php redis 消息队列 编辑:程序博客网 时间:2024/05/17 09:05

这居然是小编大笑第一篇博客,也是第一次学习使用springBoot,主要是刚摸索springboot学着搭建了springboot web+mybatis。本文主要介绍的是搭建spring boot web项目,以及该过程中遇到的一些坑。好了废话不多说,直接上干货。


1、首先上pom文件

<?xml version="1.0" encoding="UTF-8"?><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>com.springboot</groupId><artifactId>spring-boot-web</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>spring-boot-web</name><description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.6.RELEASE</version>    </parent>    <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.7</java.version><spring.boot.version>1.3.5.RELEASE</spring.boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><!--JSP start--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><!--JSP end--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.0.0</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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>${spring.boot.version}</version></dependency></dependencies><build><finalName>test</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
关于pom文件中趟的坑是凡是有这个属性的<scope>provided</scope> ,在使用idea的时候需要删除,不然项目启动不成功,而且不会报错。

2、关于spring boot 启动类

package com.springboot;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.springboot.mapper")public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}


3、关于SpringBootServletInitializer类

package com.springboot;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.web.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);}}

4、ok,关于jsp就不啰嗦了,这样大致的一个web项目已初步完成,下面将对应的项目结构上图


5、以上4步已经将对应的web项目搭建好了

6、接下来gei da

package com.springboot.bean;import java.util.Date;/** * <p>表sl_seller对应的SlSeller。</p> * * @author 自动生成 * @version 1.00 */public class SlSeller {    /**     *      */    private static final long serialVersionUID = 1L;    /** 区划(6)+顺序码(4) */    private String slCode;    /** 卖家账号 */    private String slAccount;    /** 区划(6)+顺序码(4) */    private String slCodeDis;    /** SL_CODE_MANUFACTURE */    private String slCodeManufacture;    /** SL_CODE_SELF */    private String slCodeSelf;    省略get、set方法}
mapper类<pre name="code" class="java">package com.springboot.mapper;import com.springboot.bean.SlSeller;/** * Created by zhu_kai1 on 2016/11/10. */public interface SlSellerMapper {    SlSeller findOne(String slCode);}

mybatis文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"><mapper namespace="com.springboot.mapper.SlSellerMapper">       <select id="findOne" resultType="com.springboot.bean.SlSeller"  parameterType="String">              SELECT               SL_ACCOUNT AS slAccount,               SL_CODE_DIS AS slCodeDis,               MEMO1 AS memo1               FROM sl_seller WHERE SL_CODE = #{slCode}       </select></mapper>

service类
package com.springboot.service;import com.springboot.bean.SlSeller;/** * Created by zhu_kai1 on 2016/11/10. */public interface SlSellerService {    SlSeller findOne(String slCode);}
service实现类
package com.springboot.service.impl;import com.springboot.service.SlSellerService;import com.springboot.bean.SlSeller;import com.springboot.mapper.SlSellerMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * Created by zhu_kai1 on 2016/11/10. */@Servicepublic class SlSellerServiceImpl implements SlSellerService {    @Autowired    private SlSellerMapper sellerMapper;    @Override    public SlSeller findOne(String slCode) {        return sellerMapper.findOne(slCode);    }}

controller类
package com.springboot.controller;import com.springboot.bean.SlSeller;import com.springboot.service.SlSellerService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;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.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;/** * Created by zhu_kai1 on 2016/11/3. */@Controller@RequestMapping("/seller")public class SellerController {    private static Logger logger = LoggerFactory.getLogger(SellerController.class);    @Autowired    private SlSellerService sellerService;    @RequestMapping(value = "/querySeller",method = RequestMethod.GET)    @ResponseBody    public String querySeller(){        logger.debug("开始查询");        String slCode = "1";        SlSeller slSeller = sellerService.findOne(slCode);        logger.info(slSeller.getSlAccount());        return  slSeller.getSlAccount();    }}

结束语:ok到这里mybatis也已经完成了,第一次写博客,写的不好的地方还请大家见谅。

1 0