Spring Boot从入门到放弃-整合Mybatis

摘要:

咱们做JavaEE开发肯定要和数据库打交道,所以打交道同时我们肯定要使用SQL语句,但我们不可能和曾经学Java一样,使用JDBC做增删改查,我们既然使用了Spring,我们肯定要做一些高大上的工具来做数据操作,所以我们选择了Mybatis。

Mybatis一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集

SpringBoot整合Mybatis有两种方式:

1. 使用XML方式:

使用XML方式也是我们最常用的方式了,在SSM中我们大部分也是用XML方式, 然后用Mybatis的逆向工程来完成基础SQL语言的书写。

既然我们SpringBoot尽量减少配置文件,我们就把xml写在resources的mapper下。、

在application中配置:

logging.level.root = WARN
logging.level.org.springframework.web = DEBUG
#配置log写入文件
#logging.file = springinfo.log
logging.path=./log
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
#启动默认字符集utf-8
spring.http.encoding.force=true

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 控制台输出sql
logging.level.org.spring.springboot.mapper=debug
## Mybatis 配置
mybatis.typeAliasesPackage=cn.org.xxxx.entity
mybatis.mapperLocations=classpath:mapper/*.xml

对bean类和xml进行扫描。

在Mapper上加入注解:

否则会报如下错。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name

配置Application.java

package cn.org.easycoding;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by Guanzhong Hu
 * Date :2020/1/18
 * Description :
 * Version :1.0
 */
@SpringBootApplication
@EnableCaching
@MapperScan(basePackages = "cn.org.xxxx.mapper")
public class EasyCodingApplication {

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

对map进行单独扫描。

2.  使用注解方式:

其他基本是一样的,我们使用PersonMapper1.java进行注解生产:

package cn.org.easycoding.mapper;

import cn.org.easycoding.entity.Person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import javax.validation.constraints.DecimalMax;

/**
 * Created by Guanzhong Hu
 * Date :2020/1/21
 * Description :注解方式进行查询
 * Version :1.0
 */
@Mapper
public interface PersonMapper1 {
    @Select("select * from person")
   Person findAllPerson();
}

只要加上@select等即可,如果需要携带参数,则写成如下:

@Mapper
public interface PersonMapper1 {
    @Select("select * from person where name = #{name}")
   Person findAllPerson(@Param("name") String name);
}

name则可带入SQL。

3. 进行单元测试:

package cn.org.easycoding.test;

import cn.org.easycoding.mapper.PersonMapper;
import cn.org.easycoding.mapper.PersonMapper1;
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;


/**
 * Created by Guanzhong Hu
 * Date :2020/1/20
 * Description :@SpringBootTest
 * Version :1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest
public class RootTest {
    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private PersonMapper1 personMapper1;
    @Test
    public void testPerson(){
        System.out.println(personMapper.selectByExample(null));
        System.out.println(personMapper1.findAllPerson("alan"));
    }
}

可以看的出一个是list一个是对象,因为一个是查询所有,一个是根据name精确查询。

后期整理好会将demo放上码云:https://gitee.com/jockhome/springboot

发布了48 篇原创文章 · 获赞 111 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_37857921/article/details/104061538