springbootdemo

新建一个maven project,目录结构如下:

pom.xml

<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.example</groupId>
  <artifactId>aaa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>aaa</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    
        <!-- 确定springcloud 版本 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  <dependencies>
          <!-- 启动主类 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency> 
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
       
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!-- -->
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

application.prperties

server.port=8888
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=root

mybatis.type-aliases-package=com.example.entity

service下的2个类StudentService和StudentServiceImpl

package com.example.service;

import java.util.List;

import com.example.entity.Student;

public interface StudentService {

    int insert(Student student);
    int delete(Integer id);
    int update(Integer id);
    Student selectById(Integer id);
    List<Student> selectAll();
}
package com.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.entity.Student;
import com.example.mapper.StudentMapper;
@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    StudentMapper studentMapper;
    @Override
    public int insert(Student student) {
        // TODO Auto-generated method stub
        return studentMapper.insert(student);
    }

    @Override
    public int delete(Integer id) {
        // TODO Auto-generated method stub
        return studentMapper.delete(id);
    }

    @Override
    public int update(Integer id) {
        // TODO Auto-generated method stub
        return studentMapper.update(id);
    }

    @Override
    public Student selectById(Integer id) {
        // TODO Auto-generated method stub
        return studentMapper.selectById(id);
    }

    @Override
    public List<Student> selectAll() {
        // TODO Auto-generated method stub
        return studentMapper.selectAll();
    }

}

mapper下的类StudentMapper.java

package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.entity.Student;

@Mapper
public interface StudentMapper {
    //增加
    @Insert("insert into student()values(#{id},#{name},#{age})")
    int insert(Student student);
    //删除
    @Delete("delete student where id=#{id})")
    int delete(int id);
    //修改
    @Update("update student set name=#{name} where id = #{id})")
    int update(Integer id);
    //查询
    @Select("select id,name,age from student where id = #{id})")
    Student selectById(Integer    id);
    //查询
    @Select("select id,name,age from student")
    List<Student> selectAll();

}

entity下的类Student.java

package com.example.entity;

public class Student {

    private int id;
    private String name;
    private int age;
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

controller下的类StudentController.java

package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.Student;
import com.example.service.StudentService;


@RestController
public class  StudentController{
    @Autowired
    StudentService service;
    @RequestMapping("/add")
    public String add() {
        System.out.println("111111111111111");
        Student stu = new Student();
        stu.setId(1);
        stu.setName("ling");
        stu.setAge(35);
        int result = service.insert(stu);
        System.out.println("插入结果为:"+result);
        return String.valueOf(result);
    }
    
    @RequestMapping("/delete")
    public String delete(int id) {
        System.out.println("2222222222222");
        int result = service.delete(id);
        return String.valueOf(result);
    }
    
    @RequestMapping("/update")
    public String update(int id) {
        System.out.println("3333333333333");
        int result = service.update(id);
        return String.valueOf(result);
    }
    
    @RequestMapping("/findById")
    public String findById(int id) {
        System.out.println("444444444444444444");
        Student result = service.selectById(id);
        return String.valueOf(result.getName());
    }
    
    @RequestMapping("/findAll")
    public String findAll() {
        System.out.println("55555555555555555");
        List<Student> result = service.selectAll();
        return result.toString();
    }

}

App.java

package com.example;

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

import com.example.App;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@MapperScan("com.example.mapper")
public class App {
    public static void main( String[] args ){
        SpringApplication.run(App.class, args);
        System.out.println( "Hello World!" );
    }
}

 

posted @ 2020-04-14 10:37  云霄123  阅读(119)  评论(1)    收藏  举报