多语言展示
当前在线:1752今日阅读:162今日分享:23

SpringBoot整合MyBatis开发框架

SpringBoot整合MyBatis开发框架
工具/原料
1

intellij idea

2

springboot

方法/步骤
1

如果要进行mybatis的配置一定要导入spring-boot所支持的mybatis开发包。            org.mybatis.spring.boot            mybatis-spring-boot-starter            1.3.1       

2

随后要去修改application.yml配置文件,追加mybatis的相关配置项:server:  port: 8080spring:  messages:    basename: i18n/Message,i18n/Pages #资源文件的名称  datasource:    #配置当前要使用的数据源的操作类型    type: com.alibaba.druid.pool.DruidDataSource    driver-class-name: org.gjt.mm.mysql.Driver    url: jdbc:mysql://localhost:3306/mldn_1    username: root    password: root    dbcp2:      min-idle: 5      initial-size: 5      max-total: 5      max-wait-millis: 200mybatis:  config-location: classpath:mybatis/mybatis.cfg.xml  type-aliases-package: com.gwolf.vo  mapper-locations:   - classpath:mybatis/mapper/**/*.xml

3

建立一个Dept的vo类:package com.gwolf.vo;import java.io.Serializable;public class Dept implements Serializable {    private Long deptno;    private String dname;    public Long getDeptno() {        return deptno;    }    public void setDeptno(Long deptno) {        this.deptno = deptno;    }    public String getDname() {        return dname;    }    public void setDname(String dname) {        this.dname = dname;    }   @Override   public String toString() {       return 'Dept{' +            'deptno=' + deptno +            ', dname='' + dname + '\'' +            '}';   }}

4

在src/main/resources目录下建立有一个mybatis/mybatis.cfg.xml配置文件:                                     

5

建立IDeptDAO接口,注意接口所在的包:package com.gwolf.dao;import com.gwolf.vo.Dept;@Mapperpublic interface IDeptDAO {    public List findAll();}在定义DAO接口的时候由于需要自动生成实现子类,所以在接口声明处一定要编写一个“@Mapper”的注解,否则你的DAO的接口和*.xml的Mapper文件无法整合在一起。

6

在src/main/resources/mybatis目录下建立有一个mapper子目录,而后在里面定义有com/gwolf/Dept.xml配置文件。       

7

建立一个IDeptService接口,作为服务使用。package com.gwolf.service.impl;import com.gwolf.dao.IDeptDAO;import com.gwolf.service.IDeptService;import com.gwolf.vo.Dept;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class DeptServiceImpl implements IDeptService{    @Autowired    private IDeptDAO deptDAO;    @Override    public List getAll() {        return this.deptDAO.findAll();    }}

8

进行代码测试类的编写:package com.gwolf.test;import com.gwolf.StartSpringBootMain;import com.gwolf.service.IDeptService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import javax.annotation.Resource;import javax.sql.DataSource;@SpringBootTest(classes = StartSpringBootMain.class)@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfigurationpublic class TestDeptService {    @Resource    private IDeptService deptService;        @Test    public void testList() throws Exception{        System.out.println(this.deptService.getAll());    }    }

9

此时测试通过,则springboot与mybatis已经可以成功的整合在一起进行项目开发,此时的配置要比之前使用spring+mybatis直接配置简单N多倍。

推荐信息