SpringBoot----[16]—SpringBoot集成MyBatis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010996565/article/details/83021706

SpringBoot----[16]—SpringBoot集成MyBatis

集成Mybatis

新建maven project
 新建一个maven project,取名为:spring-boot-mybatis

在这里插入图片描述

在pom.xml文件中引入相关依赖
(1)基本依赖,jdk版本号;
(2)mysql驱动,mybatis依赖包,mysql分页PageHelper:
<!-- mysql 数据库驱动. -->
    <dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 	
			spring-boot mybatis依赖:
			
			请不要使用1.0.0版本,因为还不支持拦截器插件,
	    	1.2.0 是博主写帖子时候的版本,大家使用最新版本即可
	     -->
	<dependency>
	    <groupId>org.mybatis.spring.boot</groupId>
	    <artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>1.2.0</version>
	</dependency>
<!-- 
    	MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
    	将其作为一个plugin装入到SqlSessionFactory中。 
		Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。 
		Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper
     -->	
    <dependency>
	    <groupId>com.github.pagehelper</groupId>
	    <artifactId>pagehelper</artifactId>
	    <version>4.1.0</version>
	</dependency>	
创建启动类App.java

在这里插入图片描述

在application.properties添加配置文件
•	########################################################
•	###datasource
•	########################################################
•	spring.datasource.url = jdbc:mysql://localhost:3306/test
•	spring.datasource.username = root
•	spring.datasource.password = ok
•	spring.datasource.driverClassName = com.mysql.jdbc.Driver
•	spring.datasource.max-active=20
•	spring.datasource.max-idle=8
•	spring.datasource.min-idle=8
•	spring.datasource.initial-size=10

编写Demo测试类
package com.cheny.spring_boot_mybatis;

public class Demo {

	private long id;
	private String name;
	// 省略get和set
}
编写DemoMapper
package com.cheny.spring_boot_mybatis.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.cheny.spring_boot_mybatis.Demo;

@Repository
public interface DemoMapper {
	
	//#{name}:参数占位符
	@Select("select * from Demo where name = #{name}")
	List<Demo> likeName(String name);
	
	@Select("select * from Demo where id = #{id}")
	Demo getById(long id);
	
	@Select("select name from Demo where id = #{id}")
	String getNameByID(long id);
	
	@Insert("insert into demo (name) values(#{name})")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void save(Demo demo);
}

编写DemoService
package com.cheny.spring_boot_mybatis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheny.spring_boot_mybatis.mapper.DemoMapper;

@Service
public class DemoService {
	
	@Autowired
	private DemoMapper demoMapper;
	
	public List<Demo> likeName(String name){
		return demoMapper.likeName(name);
	}

	@Transactional
	void save(Demo demo) {
		demoMapper.save(demo);
	}
}

编写DemoController
package com.cheny.spring_boot_mybatis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;

@RestController
public class DemoController {
	
	@Autowired
	private DemoService demoService;
	
	@RequestMapping("/likeName")
	public List<Demo> likeName(String name){
		// 第一个参数 第几页
		//第二个参数 每页获取的条数
		PageHelper.startPage(1, 2);
		return demoService.likeName(name);
	}
	
	@RequestMapping("/save")
	public Demo save() {
		Demo demo = new Demo();
		demo.setName("张三");
		demoService.save(demo);
		return demo;
	}
}

使用pageHelper分页

package com.cheny.spring_boot_mybatis.config;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;
@Configuration
public class MyBatisConfiguration {
	@Bean
    public PageHelper pageHelper() {
		System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }

}

获取自增长Id

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u010996565/article/details/83021706
今日推荐