构建一个SpringBoot整合了MyBatis和JSP

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

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




    4.0.0

    edu.zju.jzs.test
    SpringBootJSP
    war
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        


        
        
            mysql
            mysql-connector-java
            5.1.38
        


        
        
            org.springframework.data
            spring-data-commons
            1.13.1.RELEASE
        


        

        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
            javax.servlet
            jstl
        


    



文件写好了下一步就是要进行配置了,有很多中配置方式,这里用最懒的但是相对比较麻烦的配置方式,当然并没有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 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 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 showAllStudent(HttpServletRequest request, HttpServletResponse response) {
        ArrayList allstudent = service.getAllStudent();
        return allstudent;
    }
@RequestMapping(value = "/showstring", method = RequestMethod.GET)
//这个是返回对应的页面的 /page/showstudent.jsp 这个page是从properties文件中得到的
public String showStduent(HttpServletRequest request, HttpServletResponse response, Map 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等还有相应的测试文件什么的,就不展示了,构建好以后是这样的




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







你可能感兴趣的:(java,SpringBoot,MyBatis,java,mybatis,框架,springboot)