Java教程

springboot+mybatis 第二波实现

本文主要是介绍springboot+mybatis 第二波实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录:

 

数据库

 

 

User

package com.entity;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Repository;

@Data
public class User {
    private int id;
    private String message;
}

UserMapper

package com.mapper;

import com.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

public interface UserMapper {

    @Select("select * from user where message = #{id}")
    List<User> getUserById2(long id);


    //    #单条查询
    @Select("select * from user where id = #{id}")
    User getUserById(long id);

    @Insert("insert into user(Message) values(#{id},#{message})")
//    @Options(keyProperty = "id" , useGeneratedKeys = true)
    public int insterUser(User user);

    @Delete("delete from user where id = #{id}")
    void delStudentById(Integer id);


}

Mybatis720Application

package com;

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

@SpringBootApplication
@MapperScan("com.mapper")
public class Mybatis720Application {

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

}

application.yml

## 应用名称
#spring.application.name=Mybatis_7_20
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis:
  mapper-locations: classpath:mappers/*xml
#指定Mybatis的实体目录
  type-aliases-package: com.entity
# 数据库驱动:
spring:
  aop:
    proxy-target-class: true
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
# 数据源名称
    name: defaultDataSource
# 数据库连接地址
    url: jdbc:mysql://localhost:3306/datademo?serverTimezone=UTC
# 数据库用户名&密码:
    username: root
    password: 669988
# 应用服务 WEB 访问端口
server:
  port: 8180

 

UserMapperTest.java   (单元测试)

package com.mapper;

import com.entity.User;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.apache.ibatis.session.SqlSession;
import org.apache.logging.log4j.message.Message;
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 sun.font.CompositeFont;
import org.apache.ibatis.session.SqlSessionFactory;

import java.sql.SQLException;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;
//    @Autowired
//    private User user;

    @Test
    public void getUserById() throws SQLException {
//       单条 查询
//        User user = userMapper.getUserById(1);
//        System.out.println(user.getId());


////      多条 查询
//        List<User> user = userMapper.getUserById2(123);
//        System.out.println(user);



//        新增
//        User user = new User();
//        user.setId(14);
//        user.setMessage("我是1");
//        userMapper.insterUser(user);

////      删除
//        userMapper.delStudentById(12);



    }

}

 

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>
    <groupId>com.example</groupId>
    <artifactId>Mybatis_7_20</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Mybatis_7_20</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.Mybatis720Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

  

单元测试快捷创建的方法:

https://blog.csdn.net/u012430402/article/details/103788721

 

参考文档: https://blog.csdn.net/u012430402/article/details/104580393

这篇关于springboot+mybatis 第二波实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!