SpringBoot2.0(三) 整合MyBatis

一、创建项目

使用idea创建springboot项目,可以直接添加mybatis和mysql数据库的相关依赖,

只需要再创建项目的时候多勾几个勾就可以了

(如果使用的不是idea开发工具,可以按照你熟悉的方式创建maven项目,添加下边的pom.xml文件中的内容即可)

1.1 选择创建spring项目

SpringBoot2.0(三) 整合MyBatis_第1张图片

1.2 勾选springboot的web、mybatis、mysql支持

SpringBoot2.0(三) 整合MyBatis_第2张图片

SpringBoot2.0(三) 整合MyBatis_第3张图片

没有截图的步骤按照正常项目的创建方式即可

下边是项目创建成功之后生成的pom.xml文件,可以在依赖中发现包含mybatis和mysql的依赖。



	4.0.0

	com.peng
	springboot_mybatis
	0.0.1-SNAPSHOT
	jar

	springboot_mybatis
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		

		
		
			mysql
			mysql-connector-java
			runtime
		

	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


二、使用MyBatis完成CRUD操作

下边是项目最终的目录结构

SpringBoot2.0(三) 整合MyBatis_第4张图片

1.1 建表

CREATE TABLE student
(
  id    INT(10) AUTO_INCREMENT PRIMARY KEY,
  sname VARCHAR(50) NOT NULL,
  sage  INT(10)     NULL
);

1.2 创建实体类

public class Student {
    private int id;
    private String sname;
    private int sage;

    
    public int getId() {
        return id;
    }

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

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", sname='" + sname + '\'' +
                ", sage=" + sage +
                '}';
    }

}

1.3 创建StudentDAO接口

public interface StudentDAO {

    public List selectAll();

    public int insert(Student stu);

    public int delete(int id);

    public int update(Student stu);

}

1.4创建StudentMapper.xml文件

在resources下创建一个文件夹叫做mappers,该文件夹用来存储mybatis的mapper文件







     
     

     
           delete from student where id = #{id}
     

     
          update student set sname = #{sname} ,sage = #{sage} where id = #{id}
     

     
          insert into student(sname,sage) VALUES (#{sname},#{sage})
     

1.5 修改application.properties文件

#mysql数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456


#mybatis配置信息
#mapper文件的存放路径
mybatis.mapper-locations=classpath:mappers/*.xml

1.6在SpringBoot的启动类中,配置扫描器

@MapperScan("com.peng.springboot_mybatis.dao")的作用:可以对指定包下的mapper接口进行扫描

如果不配置,需要在每一个接口上边配置@Mapper注解

@MapperScan({"xxx","yyy"}) 支持对多个包下进行扫描,也支持*号通配符

@SpringBootApplication
@MapperScan("com.peng.springboot_mybatis.dao")
public class SpringbootMybatisApplication {

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

1.7编写测试类,对CRUD方法进行测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisApplicationTests {


	@Autowired
	private StudentDAO dao; //使用idea此处会报错,但是不影响使用

	@Test
	public void test1() {       //测试查询方法
		List list = dao.selectAll();
		for (Student student : list) {
			System.out.println(student);
		}
	}
	@Test
	public void test2(){       //测试添加方法
		Student stu = new Student();
		stu.setSname("haha");
		stu.setSage(20);
		int i = dao.insert(stu);
		System.out.println(i);
	}
	@Test
	public void test3(){       //测试修改方法
		Student stu = new Student();
		stu.setId(6);
		stu.setSname("haha");
		stu.setSage(21);
		int i = dao.update(stu);
		System.out.println(i);
	}

	@Test
	public void test4(){      //测试删除方法
		int i = dao.delete(2);
		System.out.println(i);
	}
}

-------------------------到此SpringBoot整合mybatis就结束了-----------------------------------------------------

------------------------ 下边会对mybatis的使用进行优化-----------------------------------------------------------

配置打印SQL语句、集成pagehelper分页插件、使用阿里的druid数据库连接池

三、打印SQL语句

 作用:通过打印sql语句,方便查看我们执行的SQL语句

在application.properties文件中增加一行配置 ,记得将com.peng.springboot_mybatis.dao修改为你的package路径

logging.level.com.peng.springboot_mybatis.dao=debug

 

四、集成 PageHelper

作用:PageHelper是github上的一个开源项目,可以简化mybatis的分页开发。如果你使用过mybatis就知道这是一个需要自己写sql语句的半自动化orm框架,如果需要完成分页功能,就会稍显麻烦,使用pageHelper可以简化分页开发。

1.修改pom.xml文件,增加pageHelper的依赖


   com.github.pagehelper
   pagehelper-spring-boot-starter
   1.2.3

2.编写一个查询全部的方法 (然后就没了 ), 那么下边我们就赶紧测试一下吧

 

3.测试分页的使用,在测试类中增加一个新的测试方法

      PageHelper是引入的jar中的一个类,不需要自己写,直接使用即可。

      startPage方法的两个参数分别代表pageNum(访问第几页)/pageSize(每页显示多少条数据)

       在调用查询全部的方法之前先执行startPage方法,PageHelper会对接下来执行的第一个SQL语句进行物理分页(底层使用是拦截器)

       list的实际类型是Page,可以通过输出 list.getClass()的方式查看,所以我们可以将list对象强转成Page,然后调用getTotal方法获取总条数

@Test
 public void test5(){
		PageHelper.startPage(1,5); //访问第1页,每页显示5条
		List list = dao.selectAll();
		for (Student s : list) {
			System.out.println(s);
		}

		long count = ((Page) list).getTotal(); //获取总条数
		System.out.println(count);
}

可以通过控制台打印sql语句发现,实际并没有执行查询全部的sql语句,而是输出了带有limit的分页查询语句,并且可以看到输出了查询总条数的sql语句,这样我们调用getTotal()方法获取总条数也就不奇怪了.

SpringBoot2.0(三) 整合MyBatis_第5张图片

下边是pagehelper在github上的网址,希望深入学习的可以通过下边的网址进一步学习

https://pagehelper.github.io/

 

五、整合阿里的druid

1. 修改pom.xml文件


		com.alibaba
		druid
		1.1.10

 

2.修改application.properties ,增加spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456

通过上边的配置,我们再次查询之前写好的查询方法,可以在控制台发现已经使用了druid连接池

SpringBoot2.0(三) 整合MyBatis_第6张图片

 

你可能感兴趣的:(SpringBoot)