SpringBoot+MyBatis-Plus简例

由于本人处于学习阶段,本例只展示SpringBoot+MyBatis的简单使用,不做过深的研究。

开发工具:

IDEA,HeidiSQL

1.在IDEA中创建SpringBoot项目,加入依赖如下(之前一直以为需要加入MyBatis的依赖,后来发现是不用的):


<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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>mybatisplus0917artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>mybatisplus0917name>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.5.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatisplus-spring-boot-starterartifactId>
            <version>1.0.5version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plusartifactId>
            <version>2.1.4version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

2.数据库连接,在resource资源文件下的application.properties中添加下列内容。数据库的用户名密码,以及要用到的映射文件位置**mybatis-plus.mapper-locations,别名配置**mybatis-plus.type-aliases-package,这里要区别mybatis.mapper-locations和mybatis.type-aliases-package,别问我怎么知道的。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus
spring.datasource.username=root
spring.datasource.password=1234
mybatis-plus.mapper-locations=classpath*:/mapper/**Mapper.xml
mybatis-plus.type-aliases-package=com.example.mybatisplus.entity

3.创建实体类Person.java

public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private String address;
    //setter和getter
    //toString
    }

4.写Mapper接口PersonMapper.java,可以BaseMapper中的基本方法,也可以自己定义方法,这是对MyBatis做了升级。

 @Mapper
public interface PersonMapper extends BaseMapper<Person> {
    Integer listCount();
    Person findPersonById(Integer id);
    }

5.在resource下创建mapper目录,写Mapper接口对应的xml映射文件PersonMapper.xml





<mapper namespace="com.example.mybatisplus0917.mapper.PersonMapper">
    <select id="listCount" resultType="Integer">
        SELECT COUNT(*) FROM person;
    select>
    <select id="findPersonById" parameterType="Integer" resultType="Person">
        SELECT * FROM person WHERE id=#{id}
    select>
mapper>

6.在test目录下创建测试类如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonMapperTest {
    @Autowired
    private PersonMapper personMapper;
    @Test
    public void addUser() {
        Person person = new Person();
        person.setId(10001);
        person.setName("盖伦");
        person.setAge(25);
        person.setAddress("德玛西亚");
        personMapper.insert(person);
    }
    @Test
    public void listCountTest() {
        System.out.println(personMapper.listCount());
    }

    @Test
    public void findPersonByIdTest() {
        System.out.println(personMapper.findPersonById(10001));
    }
}

测试全部通过:
这里写图片描述

数据库的表结构:
SpringBoot+MyBatis-Plus简例_第1张图片

本文只对SpringBoot+MyBatis-Plus的简单使用做了介绍,主要可以省去了MyBatis中一些基本的数据库操作,开发时可以更多将精力放在复杂操作上。了解更多的MyBatis-Plus可以参考:http://mp.baomidou.com/

你可能感兴趣的:(SpringBoot+MyBatis-Plus简例)