我认为比较简单的Springboot映射写法

1.我们先写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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
        <!--添加amq的依赖-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-amqp</artifactId>
      </dependency>
        <!--添加jpa的依赖-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
        <!--添加redis的依赖-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
        <!--添加freemarker-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>
        <!--添加jdbc的依赖-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-mail</artifactId>
      </dependency>
        <!--添加mail的支持-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
        <!--添加web的依赖-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
        <!--添加service的依赖-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web-services</artifactId>
      </dependency>
        <!--添加starter-->
      <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>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
        <!--添加thymeleaf的模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>
   </dependencies>

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


</project>

2.我们写一个application.properties

#数据库的配置
#配置数据源

spring.datasource.url=jdbc:mysql://localhost:3306/liri?useUnicode=true&characterEncoding=utf8&useSSL=false
#数据库的用户名

spring.datasource.username=root
#数据库的密码

spring.datasource.password=1234
#项目启动的端口号

server.port=8091
web.upload-path=G:/static

#rabbitmq的配置
spring.application.name=Spring-boot-rabbitmq
spring.rabbitmq.host=localhost
spring.redis.port=6379
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

#redis
spring.redis.host=localhost
spring.redis.password=1234

3.我们开始写一个实体类

package com.example.demo;
//User的实体类对应user的实体表
public class UserEntity {
    private int id;  //用户id
    private String name;//用户姓名
    private int age;//用户年龄
    private String sex;//用户性别
    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

4.我们再写一个Mapper的映射类,我们直接把sql写在了mapper的dao上,使看起来更简洁一点。我这边就列举了增删改查

package com.example.demo;

import org.apache.ibatis.annotations.*;

import java.util.List;
//UserMapper的映射类
@Mapper
public interface UserMapper {
    //1.查询所有的User数据
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "age",column = "age"),
            @Result(property = "sex",column = "sex")
    })
    List<UserEntity> getAll();
    //根据id查询一条数据
    @Select("select * from user where id=#{id}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "age",column = "age"),
            @Result(property = "sex",column = "sex")
    })
    UserEntity getOne(int id);
    //插入数据到user
    @Insert("insert into user values(#{id},#{name},#{age},#{sex})")
    void insert(UserEntity userEntity);
    //更新数据到user
    @Update("update user set name=#{name},age=#{age},sex=#{sex} where id=#{id}")
    void update(UserEntity userEntity);
    //根据id删除user的一条数据
    @Delete("delete from user where id=#{id}")
    void delete (int id);

}

5.写一个启动类

package com.example.demo;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Scanner;
//User的启动类
@SpringBootApplication
@RestController
@RequestMapping("/democs")
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @RequestMapping("/cs")
    public String cs(){
       return "cs is a games";
    }

}

5.可以运行启动

我这边没问题

6.我们来直接测试刚才的接口

package com.example.demo;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
   //这里用做测试使用
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    //注入userMapper
   @Autowired
    private UserMapper userMapper;
   //测试插入数据到user
    //执行正确
    @Test
    public void testInsert() throws Exception {
        UserEntity u1=new UserEntity();
        u1.setId(21);
        u1.setName("zh");
        u1.setAge(22);
        u1.setSex("男");
        UserEntity u2=new UserEntity();
        u2.setId(22);
        u2.setName("she");
        u2.setAge(22);
        u2.setSex("男");
        UserEntity u3=new UserEntity();
        u3.setId(23);
        u3.setName("sh");
        u3.setAge(22);
        u3.setSex("男");
        userMapper.insert(u1);
        userMapper.insert(u2);
        userMapper.insert(u3);
    }
    //查询user的全部数据
   //执行正确
    @Test
    public void testQuery() throws Exception{
       List<UserEntity> userEntityList=userMapper.getAll();
       UserEntity userEntity=userEntityList.get(0);
       UserEntity userEntity1=userEntityList.get(1);
       UserEntity userEntity2=userEntityList.get(2);
       System.out.println("====================分割线=================");
       System.out.println(userEntity.getId()+userEntity.getAge()+userEntity.getName()+userEntity.getSex());
       System.out.println(userEntity1.getId()+userEntity1.getAge()+userEntity1.getName()+userEntity1.getSex());
        System.out.println(userEntity2.getId()+userEntity2.getAge()+userEntity2.getName()+userEntity2.getSex());
    }
    //根据user的id更新一条数据
    //执行正确
   @Test
    public void testUpdate() throws Exception{
       UserEntity userEntity=userMapper.getOne(5);
       System.out.println(userEntity.toString());
       userEntity.setName("xzx");
       userEntity.setAge(23);
       userEntity.setSex("女");
       userMapper.update(userEntity);
       Assert.assertTrue(("xz".equals(userMapper.getOne(5).getName())));
       System.out.println("=============分割线==============");
       System.out.println(userMapper.getOne(5).getName()+userMapper.getOne(5).getId()
        +userMapper.getOne(5).getAge()+userMapper.getOne(5).getSex());
    }
    //根据id删除一条数据
    //执行正确
    @Test
    public void delete() throws Exception{
        userMapper.delete(5);
    }

}

7.假如是自己学习或者是小型项目,建议用这类方法,大型项目不建议采用

上面的依赖有集成redis,shiro,mq。可以自行添加

猜你喜欢

转载自blog.csdn.net/LRXmrlirixing/article/details/83176359