练手用的,目前只有单表,后续更新1V多、多V1和多V多

搭建springboot基础环境

这个不在这里细说了,网上多的是。

项目结构

主要分为如下几部分:

  • pom.xml
  • mybatis.xml
  • application.properties
  • bean
  • dao
  • service
  • controller

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.miracle</groupId>
    <artifactId>testplatform</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testplatform</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <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-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

#日志配置
logging.file.path=./log/platform.log
logging.file.max-size=10MB
logging.level.com.miracle=debug

#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.config-location=classpath:mybatis/mybatis-config.xml

Test.java

package com.miracle.bean;

/**
 * 功能描述:
 *
 * @Author: Miracle
 * @Date: 2020/4/16 17:05
 */
public class Test {
    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;
    }

    @Override
    public String toString() {
        return "Test{" +
                "id=" + this.getId() +
                ", name='" + this.getName() + '\'' +
                ", age=" + this.getAge() +
                '}';
    }
}

TestDao.java

package com.miracle.dao;

import com.miracle.bean.Test;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能描述:
 *
 * @Author: Miracle
 * @Date: 2020/4/16 18:35
 */

@Repository
@Mapper
public interface  TestDao {
    Test getTestById(int id);
    List<Test> getAllTest();
    void insertTest(Test test);
    void updateTestById(Test test);
}

TestService.java

package com.miracle.service;

import com.miracle.bean.Test;
import com.miracle.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 *
 * @Author: Miracle
 * @Date: 2020/4/16 19:33
 */

@Service
public class TestService {
    @Autowired
    private TestDao testDao;

    public Test getTestById(int id){
        return testDao.getTestById(id);
    }

    public List<Test> getAllTest() {
        return testDao.getAllTest();
    }

    public void insertTest(Test test) {
        testDao.insertTest(test);
    }

    public void updateTestById(Test test) {
        testDao.updateTestById(test);
    }
}

TestController.java

package com.miracle.controller;

import com.miracle.bean.Test;
import com.miracle.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述:
 *
 * @Author: Miracle
 * @Date: 2020/4/16 17:09
 */
@RestController
@RequestMapping("/test")
public class TestController {

    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    TestService testService;

    @RequestMapping(value = "/getTest",method = RequestMethod.GET)
    public Test getTest(){
        Test test = new Test();
        test.setName("Miracle");
        test.setId(1);
        test.setAge(22);
        return test;
    }

    @RequestMapping(value = "/getAllTest",method = RequestMethod.GET)
    public List<Test> getAllTest(){
        return testService.getAllTest();
    }

    @RequestMapping(value = "/getTestById",method = RequestMethod.GET)
    public Test getTestById(int id){
        return testService.getTestById(id);
    }

    @RequestMapping(value = "/insertTest",method = RequestMethod.POST)
    public void insertTest(@RequestBody Test test){
        testService.insertTest(test);
    }

    @RequestMapping(value = "/updateTest",method = RequestMethod.POST)
    public void updateTestById(@RequestBody Test test){
        testService.updateTestById(test);
    }

    @RequestMapping(value = "/addTest",method = RequestMethod.POST)
    public void addTest(@RequestBody Test test){
        logger.info(test.getName()+"-"+test.toString());
    }
}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.miracle.bean"/>
    </typeAliases>
    <mappers>
        <mapper resource="mybatis/mapper/TestMapper.xml"/>
    </mappers>
</configuration>

TestMapper.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.miracle.dao.TestDao">
    <select id="getTestById" parameterType="int" resultType="Test">
        select id,name,age from test where id = #{id}
    </select>
    <select id="getAllTest" resultType="Test">
        select id,name,age from test
    </select>
    <insert id="insertTest" parameterType="Test" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into test (name,age) values(#{name},#{age})
    </insert>
    <update id="updateTestById" parameterType="Test" >
        update test set name=#{name},age=#{age} where id=#{id}
    </update>
</mapper>

sql

DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'm1', '11');
INSERT INTO `test` VALUES ('2', 'm2', '12');
INSERT INTO `test` VALUES ('3', 'm2', '12');
INSERT INTO `test` VALUES ('4', 'm4', '14');
INSERT INTO `test` VALUES ('5', 'm5', '15');

发表评论

电子邮件地址不会被公开。 必填项已用*标注