构建一个SpringBoot整合了MyBatis和JSP

最近因为要写数据可视化的内容,根据要求,又得使用SSM框架,虽然已经配置过NSSM框架了,但是总是在配置的时候产生各种各样的BUG,所以这次就瞄准了Spring-Boot做一个小型的微服务框架,主要的就是通过Spirng-boot整合mybatis然后整合JSPJSP不是spring-boot官方整合推荐的,只是我个人要使用,所以就在这里一并整合。

首先先说maven整合框架的内容,创建好mavenProject以后就要进行配置pom.xml文件了,文件内容大致如下:


<?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>edu.zju.jzs.test</groupId>
    <artifactId>SpringBootJSP</artifactId>
    <packaging>war</packaging><!-- 将项目打包成war包来完成JSP的页面解析,否则直接跑的话是要下载页面的-->
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--下面这三个可以说是基本配置吧-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <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.1.1</version>
        </dependency>


        <!--数据库连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.13.1.RELEASE</version>
        </dependency>


        <!-- JSP相关的配置-->

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>


    </dependencies>


</project>

文件写好了下一步就是要进行配置了,有很多中配置方式,这里用最懒的但是相对比较麻烦的配置方式,当然并没有SSM那么麻烦了。


server.port=8080
server.context-path=/thepage
server.tomcat.uri-encoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stu?useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=
spring.mvc.view.prefix: /pages
spring.mvc.view.suffix: .jsp

可以看到其中配置了端口号数据库驱动信息,编码格式等我们常常用到的内容,同时也包括了用户名、密码的内容,以及返回JSP解析的方式和默认路径/thepage

然后构建启动类Application.java


package edu.zju.jzs.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class Application {

   public static void main(String[] args){
      SpringApplication.run(Application.class,args);
      }
}

然后构建一个基本的POJO

package edu.zju.jzs.test.entity;
public class Student {
    private String name;
    private String studentnum;
    private String sex;
    private int age;
	//省略Setter和Getter类
}

然后构建一个DAO


package edu.zju.jzs.test.dao;
import java.util.ArrayList;
import edu.zju.jzs.test.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface StudentDAO {
   @Select("select * from student")
    public ArrayList<Student> getAllStudent();
   @Select("select * from student where studentnum=#{id}")
    public Student getAStudentById(@Param("id") String id);   
}

这个接口类看上去比较奇怪,因为没有使用MybtaisGenerator功能,所以就直接使用注解这个功能完成相关的操作

然后构建一个Service

package edu.zju.jzs.test.service;
import edu.zju.jzs.test.dao.StudentDAO;
import edu.zju.jzs.test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class StudentService {
    @Autowired
    private StudentDAO dao;
    public ArrayList<Student> getAllStudent()
    {
        return dao.getAllStudent();
    }
}

最后构建一个Controller


package edu.zju.jzs.test.controller;
import edu.zju.jzs.test.entity.Student;
import edu.zju.jzs.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;
@RestController
public class StudentController {

    @Autowired
    private StudentService service;
@RequestMapping(value = "/allstuinfo")
    @ResponseBody
//这个是返回JSON数据的
    public ArrayList<Student> showAllStudent(HttpServletRequest request, HttpServletResponse response) {
        ArrayList<Student> allstudent = service.getAllStudent();
        return allstudent;
    }
@RequestMapping(value = "/showstring", method = RequestMethod.GET)
//这个是返回对应的页面的 /page/showstudent.jsp 这个page是从properties文件中得到的
public String showStduent(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
    System.out.println("this is show string");
    Student student = this.sayhello();
    request.setAttribute("student", student);
    model.put("stu", student);
    return "/showstudent";
}
}

    嗯嗯这样就差不多了

构建完成后大概文件夹内容是这样的


这个里面SampleController是测试用的内容大概是如下的:


package edu.zju.jzs.test.controller;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 11544 on 2017/2/27.
 */
@Controller
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    @RequestMapping("/showaddsys")
    String showAddsysjsp()
    {
        System.out.println("entered the show add sys");
        return "/addsys";
    }
}


然后在main下构建一个webapp文件夹,下面有WEB-INF文件,同时添加一些文件比如web.xml等还有相应的测试文件什么的,就不展示了,构建好以后是这样的




这样就差不多把一个架子就搭建起来了







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值