搭建一个简单的SpringBoot项目

一、创建项目

1.File->new->project;

2.选择“Spring Initializr”,点击next;

.完善项目信息,组名可不做修改,项目名可做修改;最终建的项目名为:test,src->main->java下包名会是:com->example->test;点击next;

4.Web下勾选Spring Web Start ;Template Englines勾选Thymeleaf;SQL勾选:MySQL Driver,JDBC API 和 MyBatis Framework三项;点击next;

5.选择项目路径,点击finish;打开新的窗口;

6.项目目录

7.在templates文件下新建index.html页面,作为启动的初始页面;


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
你好!初学者,我是SpringBoot的简单启动页面!
</body>

</html>

8.在com.example.test下新建controller文件夹,在controller文件夹下建一个简单的helloController类;(Controller类要添加@Controller注解,项目启动时,SpringBoot会自动扫描加载Controller)

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @RequestMapping("/index")
    public String sayHello(){
        return "index";
    }
}

9.这里我创建了一个application.yml 当然你也可以用本身的


spring:
  datasource:
    name: ststistical #数据库名
    url: jdbc:mysql://127.0.0.1:3306/ststistical #url
    username: root  #用户名
    password: 123456  #密码
    driver-class-name: com.mysql.cj.jdbc.Driver


mybatis:
  mapper-locations: classpath:mappers/*.xml   #一会xml的位置
  type-aliases-package: com.example.demo.pojo  #实体类位置

10.运行项目启动类TestApplication.java

11.然后编写简单地增删改查 先看一下整体目录

12.实体类-pojo

package com.example.demo.pojo;

/**
 * @Author:王涛
 * @Description:
 * @Date:Created in 11:23  2020/8/18
 * @Modified BY:
 */
public class TeacherPojo {
    private int id;
    private String teacher_name;
    private String teacher_post;
    private String teacher_class;
    private String spare1;
    private String spare2;

    public int getId() {
        return id;
    }

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

    public String getTeacher_name() {
        return teacher_name;
    }

    public void setTeacher_name(String teacher_name) {
        this.teacher_name = teacher_name;
    }

    public String getTeacher_post() {
        return teacher_post;
    }

    public void setTeacher_post(String teacher_post) {
        this.teacher_post = teacher_post;
    }

    public String getTeacher_class() {
        return teacher_class;
    }

    public void setTeacher_class(String teacher_class) {
        this.teacher_class = teacher_class;
    }

    public String getSpare1() {
        return spare1;
    }

    public void setSpare1(String spare1) {
        this.spare1 = spare1;
    }

    public String getSpare2() {
        return spare2;
    }

    public void setSpare2(String spare2) {
        this.spare2 = spare2;
    }
}

13.dao层

package com.example.demo.mapper;

import com.example.demo.pojo.TeacherPojo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @Author:王涛
 * @Description:
 * @Date:Created in 11:23  2020/8/18
 * @Modified BY:
 */
@Mapper
public interface TeacherDao {
    //增加
    int addInfo(TeacherPojo teacherPojo);
    //删除
    int  delectInfo(@Param("id") int id);
    //修改
    int updateInfo(TeacherPojo teacherPojo);
    //查看
    List<TeacherPojo> selectInfo(TeacherPojo teacherPojo);
}

14.servvice层 以及实现类

package com.example.demo.service;


import com.example.demo.pojo.TeacherPojo;

import java.util.List;

/**
 * @Author:王涛
 * @Description:
 * @Date:Created in 11:23  2020/8/18
 * @Modified BY:
 */
public interface TeacherService {
    //增加
    int addInfo(TeacherPojo teacherPojo);
    //删除
    int  delectInfo(int id);
    //修改
    int updateInfo(TeacherPojo teacherPojo);
    //查看
    List<TeacherPojo> selectInfo(TeacherPojo teacherPojo);

}

 

package com.example.demo.service.serviceImpl;

import com.example.demo.mapper.TeacherDao;
import com.example.demo.pojo.TeacherPojo;
import com.example.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:王涛
 * @Description:
 * @Date:Created in 15:07  2020/8/18
 * @Modified BY:
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Override
    public int addInfo(TeacherPojo teacherPojo) {
        return  teacherDao.addInfo(teacherPojo);
    }

    @Override
    public int delectInfo(int id) {
        return teacherDao.delectInfo(id);
    }

    @Override
    public int updateInfo(TeacherPojo teacherPojo) {
        return teacherDao.updateInfo(teacherPojo);
    }

    @Override
    public List<TeacherPojo> selectInfo(TeacherPojo teacherPojo) {

        return teacherDao.selectInfo(teacherPojo);
    }


}

15.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
		PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.TeacherDao">



	<!--<sql id="teachers">-->
		<!--a.id AS "id",-->
		<!--a.teacher_name AS "teacherName",-->
		<!--a.teacher_post AS "teacherPost",-->
		<!--a.teacher_class AS "teacherClass",-->
		<!--a.spare1 AS "spare1",-->
		<!--a.spare2 AS "spare2"-->
	<!--</sql>-->
	<!--<resultMap type="com.example.demo.pojo.TeacherPojo" id="teacherMap">-->
		<!--<id column="id" property="id" />-->
		<!--<result column="teacherName" property="teacher_name" />-->
		<!--<result column="teacherPost" property="teacher_post" />-->
		<!--<result column="teacherClass" property="teacher_class" />-->
		<!--<result column="spare1" property="spare1" />-->
		<!--<result column="spare1" property="spare2" />-->
	<!--</resultMap>-->


	<select id="selectInfo" resultType="TeacherPojo"  parameterType="TeacherPojo">
	SELECT *
	FROM teacher

	<where>
	<if test="id != null and id != ''">
	AND id = #{id}
	</if>
	<if test="teacher_name != null and teacher_name != ''">
	AND teacher_name = #{teacher_name}
	</if>
	<if test="teacher_post != null and teacher_post != ''">
	AND teacher_post = #{teacher_post}
	</if>
	<if test="teacher_class != null and teacher_class != ''">
	AND teacher_class = #{teacher_class}
	</if>
	<if test="spare1 != null and spare1 != ''">
	AND spare1 = #{spare1}
	</if>
	<if test="spare2 != null and spare2 != ''">
	AND spare2 = #{spare2}
	</if>
	</where>

	</select>


	<!--<select id="selectInfo"  resultType="TeacherPojo"  parameterType="TeacherPojo">-->
		<!--SELECT *-->
		<!--FROM teacher-->
        <!--<trim prefix="set" suffix="where id=#{id}" prefixOverrides=",">-->
            <!--<if test="teacher_name!=null and teacher_name!=''">,teacher_name =#{teacher_name}</if>-->
            <!--<if test="teacher_post!=null and  teacher_post!=''">,teacher_post =#{teacher_post}</if>-->
            <!--<if test="teacher_class!=null and teacher_class!=''">,teacher_class =#{teacher_class}</if>-->
        <!--</trim>-->

	<!--</select>-->

	<insert id="addInfo">
		insert into teacher
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="teacher_name!=null and teacher_name!=''">teacher_name ,</if>
			<if test="teacher_post!=null and  teacher_post!=''">teacher_post ,</if>
			<if test="teacher_class!=null and teacher_class!=''">teacher_class ,</if>
			<if test="spare1!=null and spare1!=''">spare1 ,</if>
			<if test="spare2!=null and spare2!=''">spare2 ,</if>
		</trim>
		values
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="teacher_name !=null and teacher_name !=''">#{teacher_name},</if>
			<if test="teacher_class !=null and teacher_class !=''">#{teacher_class},</if>
			<if test="teacher_class !=null and teacher_class !=''">#{teacher_class},</if>
			<if test="spare1!=null and spare1!=''">spare1 ,</if>
			<if test="spare2!=null and spare2!=''">spare2 ,</if>
		</trim>

	</insert>

	<update id="updateInfo"  parameterType="Integer">
		UPDATE teacher
		<trim prefix="set" suffix="where id=#{id}" prefixOverrides=",">
			<if test="teacher_name!=null and teacher_name!=''">,teacher_name =#{teacher_name}</if>
			<if test="teacher_post!=null and  teacher_post!=''">,teacher_post =#{teacher_post}</if>
			<if test="teacher_class!=null and teacher_class!=''">,teacher_class =#{teacher_class}</if>
			<if test="teacher_class!=null and teacher_class!=''">,spare1 =#{spare1}</if>
			<if test="teacher_class!=null and teacher_class!=''">,spare2 =#{spare2}</if>
		</trim>
	</update>

	<delete id="delectInfo" parameterType="Integer">
		delete from teacher
		WHERE id = #{id}
	</delete>


</mapper>

16.控制层

package com.example.demo.controller;

import com.example.demo.pojo.TeacherPojo;
import com.example.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:王涛
 * @Description:
 * @Date:Created in 11:24  2020/8/18
 * @Modified BY:
 */
@RestController
//@SpringBootApplication
@RequestMapping(value = "/user")
public class TeacherControcller {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping(value = "/delect")
    @ResponseBody
    public int index(){
        TeacherPojo teacherPojo=new TeacherPojo();
        teacherPojo.setId(5);
        int i = teacherService.delectInfo(15);
        return i;
    }
    //查看
    @RequestMapping(value = "/select")
    @ResponseBody
    public List<TeacherPojo>  select(){
        TeacherPojo teacherPojo=new TeacherPojo();
       teacherPojo.setTeacher_name("曾金穗");
        //    teacherPojo.setId(1);
        List<TeacherPojo> teacherPojos = teacherService.selectInfo(teacherPojo);
        return teacherPojos;
    }
    //修改
    @RequestMapping(value = "/update")
    @ResponseBody
    public String  update(){
        TeacherPojo teacherPojo=new TeacherPojo();
        teacherPojo.setId(14);
        teacherPojo.setTeacher_name("王涛");
        teacherPojo.setTeacher_class("wt");
        teacherPojo.setTeacher_post("123");
        teacherPojo.setSpare1("啥也不是");
        int i = teacherService.updateInfo(teacherPojo);
        return "修改成功";
    }
    //添加
    @RequestMapping(value = "/add")
    @ResponseBody
    public String  add(){
        TeacherPojo teacherPojo=new TeacherPojo();
        teacherPojo.setTeacher_name("王涛2");
        teacherPojo.setTeacher_class("王涛2");
        teacherPojo.setTeacher_post("1234");
        teacherPojo.setSpare1("啥也不是1");
        teacherPojo.setSpare2("啥也不是2");
        int i = teacherService.addInfo(teacherPojo);
        return "添加成功";
    }


}

16.最后是pom里面注意的一点:

 <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

这个会扫描xml文件。

17.最后一一实现就可以了

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值