The bottom layer of the springboot project integrates the jpa specification and mybatis

    As you and I all know, the jpa specification is based on hibernate, which is very convenient for single-table operations, but a little powerless for multi-table operations, so in my spare time, I did a small test by myself to integrate mybatis. Of course, the integrated jpa specification has been explained in detail in the previous blog. If you want to know more, please see my previous blog.

    The premise of this blog is based on the previous blog, so I will not emphasize the configuration of those databases.

    The first step is to introduce the relevant jar of mybatis

    

<!--======================mybatis basic configuration ======================= =======================-->
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>6.0.6</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.1.2</version>
		</dependency>

The next step is to simply write the mapper configuration.

However, there is a small point to pay attention to is to add mapper scanning on the startup class. The @MapperScan annotation is followed by the specific location of the mapper file.

Next is to write the Mapper interface.

public interface DemoMapper extends CrudRepository<Demo,Long>{
    @Select("select * from Demo where name =#{name}")
    public List<Demo> findByName(String name);

    @Select("select * from Demo where id=#{id}")
    public Demo getById(long id);

    @Select("select name from Demo where id=#{id}")
    public String getNameById(long id);
}

This interface inherits the CrudRepository interface in the Jpa specification, followed by some additional mybatis query interfaces written using the @Select annotation. It is relatively simple to write, just to be an example.

There is a small pit here, that is, try to use the common method of find when naming the interface. Secondly, the attribute name of the entity is followed by By, neither more nor less, otherwise there will be easter eggs.

The last thing is to write a very simple Controller to verify it.

@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoMapper demoMapper;

    @RequestMapping("findByName")
    public List<Demo> findByName(String name){
        return demoMapper.findByName(name);
    }

    @RequestMapping("add")
    public Integer add(){
        Demo demo = new Demo();
        demo.setName("demo");
        demoMapper.save(demo);
        return demo.getId();
    }
}
In this way, there are two kinds of bottom layers, each with its own strength.


    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325152629&siteId=291194637