SpringBoot拾级而上·mybatis 无配置文件注解版

一、添加依赖

    compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.0.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.0.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.4.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12'
    compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.2'
    compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.4.RELEASE'

二、application.properties 添加相关配置

mybatis.type-aliases-package=com.springboot.springboot_web.entity

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root

如果使用了多环境配置的机制,则不需要在application.properties里配置数据链接,只需在对应的文件中配置
application-dev.properties application-test.properties application-prod.propertie

springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中

三、在启动类中添加对mapper包扫描@MapperScan

@SpringBootApplication
@MapperScan("com.springboot.springboot_web.mapper")
public class SpringbootWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootWebApplication.class, args);
    }
}

或者直接在Mapper类上面添加注解@Mapper,建议使用上面那种,不然每个mapper加个注解也挺麻烦的

四、开发Mapper
调用的sql语句在这里定义

package com.springboot.springboot_web.mapper;

import com.springboot.springboot_web.entity.AccountsEntity;
import com.springboot.springboot_web.enums.InviteCodeTypeEnum;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AccountsMapper {

    @Select("SELECT * FROM Accounts")
    @Results({
            //@Result(property = "id",  column = "id", javaType = InviteCodeTypeEnum.class),
            @Result(property = "id", column = "id"),
            @Result(property = "mobile", column = "mobile")
    })
    List getAll();

    @Select("SELECT * FROM Accounts WHERE id = #{id}")
    AccountsEntity getById(Long id);

    @Insert("INSERT INTO Accounts(accountName,mobile,isDeleted,inviteCodeType) VALUES(#{accountName}, #{mobile}, #{isDeleted}, #{inviteCodeType})")
    void insert(AccountsEntity accounts);

    @Update("UPDATE Accounts SET accountName=#{accountName},mobile=#{mobile},isDeleted=#{isDeleted},inviteCodeType=#{inviteCodeType} WHERE id =#{id}")
    void update(AccountsEntity accounts);

    @Delete("DELETE FROM Accounts WHERE id =#{id}")
    void delete(Long id);

}

@Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
column 对应数据库里的字段名,property对应实体类中的属性名
inviteCodeType 这里使用里枚举

package com.springboot.springboot_web.enums;

public enum InviteCodeTypeEnum {
    user,other
}
package com.springboot.springboot_web.entity;

import com.springboot.springboot_web.enums.InviteCodeTypeEnum;

import java.io.Serializable;

public class AccountsEntity implements Serializable {

    private Integer id;
    private String accountName;
    private String mobile;
    private String isDeleted;
    private InviteCodeTypeEnum inviteCodeType;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(String isDeleted) {
        this.isDeleted = isDeleted;
    }

    public InviteCodeTypeEnum getInviteCodeType() {
        return inviteCodeType;
    }

    public void setInviteCodeType(InviteCodeTypeEnum inviteCodeType) {
        this.inviteCodeType = inviteCodeType;
    }
}

五、使用
这里我们编写测试类AccountsMapperTest
注意加标注 @MapperScan("com.springboot.springboot_web.mapper")
如果有如下报错,可无视


image.png
package com.springboot.springboot_web.mapperTest;

import com.springboot.springboot_web.entity.AccountsEntity;
import com.springboot.springboot_web.enums.InviteCodeTypeEnum;
import com.springboot.springboot_web.mapper.AccountsMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
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
@MapperScan("com.springboot.springboot_web.mapper")
public class AccountsMapperTest {

    @Autowired
    private AccountsMapper accountsMapper;

    @Test
    public void testQueryAll() {
        List accountsMapperAll = accountsMapper.getAll();
        for(int i=0;i

你可能感兴趣的:(SpringBoot拾级而上·mybatis 无配置文件注解版)