Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis

前言

    spring boot 极大的简化了项目配置,比传统的SSM框架的开发效率更高。

开发准备

  1. jdk1.8
  2. maven 3.5.0
  3. IDEA

开发步骤:

1. 新建Spring Boot项目

    1.1 Intellij idea菜单栏File->new->project。

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第1张图片

    1.2选择左侧栏中spring initializr,右侧选择jdk版本,以及默认的Service URL,点击next。

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第2张图片

    1.3然后填写项目的Group、Artifact等信息,点击next。

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第3张图片

    1.4左侧点击Web,中间一侧选择Web,然后左侧选择SQL,中间一侧选择JDBC、Mybatis、MYSQL(数据库用的是mysql,大家可以选择其他DB),点击next。

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第4张图片

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第5张图片

    1.5 至此,一个maven web项目就创建好了,目录结构如下。
Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第6张图片

2. 创建文件夹形成web目录结构

    2.1 创建以下典型的web目录结构

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第7张图片

3. 修改属性配置文件

    3.1 将 application.properties文件修改为application.yml文件
        原因:如果使用的是默认的applica.properties文件,会重复写许多配置式的前缀,并且不能够体现出结构化特点。

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第8张图片

    3.2 在pom.xml中添加依赖,防止默认寻找application.properties文件出错

<dependency>
    <groupId> org.springframework.boot groupId>
    <artifactId>spring-boot-configuration-processorartifactId>
    <optional>trueoptional>
dependency>

    3.3 在application.yml文件中写入配置

server:
  port: 8081  //访问端口号
spring:
  datasource:  //数据库配置
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/angulardemo
    username: root
    password: wmj
mybatis:  //Mybatis相关配置
  typeAliasesPackage: com.wmj.bootMybatis.Entity  //指明实体位置
  mapperLocations: classpath:Mapper/*.xml  //配置Mapper.xml文件地址

4. 在Spring Boot启动类中声明Dao层的位置

package com.wmj.bootMybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.wmj.bootMybatis.Dao")//非常重要
public class BootMybatisApplication {

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

5. 像传统的是SSM开发模式进行开发

    5.1 开发目录结构如下
Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第9张图片

    5.2 Entity层

package com.wmj.springbootMybatis.Entity;

public class Hero {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

    5.2 Dao层

package com.wmj.springbootMybatis.Dao;

import com.wmj.springbootMybatis.Entity.Hero;

import java.util.List;

public interface HeroDao {
    //获取所有英雄信息
    List getAllHeroInfo();
    //获取所有英雄信息
    List getHeroInfoById(Integer id);
    //获取查询英雄信息
    List getHeroInfoByName(String name);

    //修改英雄信息
    int updateHeroInfo(Hero heroInfo);
    //获取现有英雄的最大ID
    int getMaxHeroId();
    //添加英雄信息
    int addHeroInfo(Hero hero);

    //根据学号删除英雄信息
    int deleteHeroInfo(Integer id);
}

    5.3 Dao层对应Mapper.xml文件

version="1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"com.wmj.springbootMybatis.Dao.HeroDao" >
    id="heroResultMap" type="com.wmj.springbootMybatis.Entity.Hero" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
    

    
    
    

    id="updateHeroInfo" parameterType="com.wmj.springbootMybatis.Entity.Hero">
        update herotable
        <set >
            <if test="name != null" >
                name = #{name},
            if>
        set>
        where id = #{id}
    

    

    id="addHeroInfo" parameterType="com.wmj.springbootMybatis.Entity.Hero" >
        insert into herotable values (#{id}, #{name})
    

    id="deleteHeroInfo" parameterType="java.lang.Integer" >
        delete from herotable
        where id = #{id}
    

    5.4 Service层接口

package com.wmj.springbootMybatis.Service;

import com.wmj.springbootMybatis.Entity.Hero;

import java.util.List;

public interface HeroService {
    //获取所有hero
    public List getAllHeroInfo();
    //根据id获取hero,返回List
    public List getStudentInfoById(int id);
    //根据name获取hero,返回List
    public List getStudentInfoByName(String name);
    //修改Hero信息
    int updateHeroInfo(Hero heroInfo);
    //获取hero的最大Id
    int getMaxHeroId();
    //添加Hero信息
    int addHeroInfo(Hero heroInfo);
    //根据id删除Hero信息
    int deleteHeroInfo(Integer id);
}

    5.5 Service层实现层

package com.wmj.springbootMybatis.Service.Impl;

import com.wmj.springbootMybatis.Dao.HeroDao;
import com.wmj.springbootMybatis.Entity.Hero;
import com.wmj.springbootMybatis.Service.HeroService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("heroService")
public class HeroServiceImpl implements HeroService {
    @Resource
    private HeroDao heroDao;

    @Override
    public List getAllHeroInfo() {
        List stuInfo =this.heroDao.getAllHeroInfo();
        return stuInfo;
    }

    @Override
    public List getStudentInfoById(int id) {
        List stuInfo =this.heroDao.getHeroInfoById(id);
        return stuInfo;
    }

    @Override
    public List getStudentInfoByName(String name) {
        List stuInfo =this.heroDao.getHeroInfoByName(name);
        return stuInfo;
    }

    @Override
    public int updateHeroInfo(Hero heroInfo) {
        return this.heroDao.updateHeroInfo(heroInfo);
    }


    @Override
    public int getMaxHeroId() {
        return this.heroDao.getMaxHeroId();
    }

    @Override
    public int addHeroInfo(Hero heroInfo) {
        return this.heroDao.addHeroInfo(heroInfo);
    }

    @Override
    public int deleteHeroInfo(Integer id) {
        return this.heroDao.deleteHeroInfo(id);
    }
}

    5.6 Service层实现层

package com.wmj.springbootMybatis.Controller;

import com.wmj.springbootMybatis.Entity.Hero;
import com.wmj.springbootMybatis.Service.HeroService;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/hero")
public class HeroInfoController {

    @Resource
    private HeroService heroSerive;

    @RequestMapping(value="/selectAllHero")
    public List selectAllHero(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域问题
        List allHeroInfo=heroSerive.getAllHeroInfo();
        return allHeroInfo;
    }

    @RequestMapping(value="/selectHeroById")
    public List selectHeroById(HttpServletRequest request, HttpServletResponse response,String heroId){
        response.setHeader("Access-Control-Allow-Origin", "*");
        List HeroInfo=heroSerive.getStudentInfoById(Integer.parseInt(heroId));
        return HeroInfo;
    }

    @RequestMapping(value="/selectHeroByName")
    public List selectHeroByName(HttpServletRequest request, HttpServletResponse response,String heroName){
        response.setHeader("Access-Control-Allow-Origin", "*");
        List HeroInfo = new ArrayList();
        if(heroName.equals("")){

        }
        else{
            HeroInfo=heroSerive.getStudentInfoByName(heroName);
        }
        return HeroInfo;
    }

    @RequestMapping(value="updateHeroById")
    public JSONObject  updateStudentInfo(HttpServletRequest request, HttpServletResponse response, String hero) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        JSONObject jsonobject = JSONObject.fromObject(hero);
        Hero rule = (Hero) JSONObject.toBean(jsonobject, Hero.class);
        int result = heroSerive.updateHeroInfo(rule);
        JSONObject state = new JSONObject();
        if(result ==1){
            state.put("state", "修改成功");
        }
        else{
            state.put("state", "修改失败");
        }
        return state;
    }
    @RequestMapping(value="/addHeroInfo")
    public List addStudentInfo(HttpServletRequest request, HttpServletResponse response, String name){
        response.setHeader("Access-Control-Allow-Origin", "*");
//        JSONObject jsonobject = JSONObject.fromObject(json);
        //获取英雄的最大ID
        int heroMaxId = heroSerive.getMaxHeroId();
        int heroId = heroMaxId+1;

        JSONObject heroInfo = new JSONObject();
        heroInfo.put("id", heroId);
        heroInfo.put("name", name);

        Hero rule = (Hero) JSONObject.toBean(heroInfo, Hero.class);
        heroSerive.addHeroInfo(rule);
        List HeroInfo=heroSerive.getStudentInfoById(heroId);
        return HeroInfo;


    }
    @RequestMapping(value="deleteHeroById")
    public JSONObject deleteHeroById(HttpServletRequest request, HttpServletResponse response, String  id) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        int result = heroSerive.deleteHeroInfo(Integer.parseInt(id) );

        JSONObject state = new JSONObject();
        if(result ==1){
            state.put("state", "删除成功");
        }
        else{
            state.put("state", "删除失败");
        }
        return state;
    }
}

6. 运行结果

Spring Boot(一)使用Spring Initializr创建项目入门+整合Mybatis_第10张图片

你可能感兴趣的:(Spring,Boot)