intellij idea
springboot
如果要进行mybatis的配置一定要导入spring-boot所支持的mybatis开发包。
随后要去修改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
建立一个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 + '\'' + '}'; }}
在src/main/resources目录下建立有一个mybatis/mybatis.cfg.xml配置文件:
建立IDeptDAO接口,注意接口所在的包:package com.gwolf.dao;import com.gwolf.vo.Dept;@Mapperpublic interface IDeptDAO { public List
在src/main/resources/mybatis目录下建立有一个mapper子目录,而后在里面定义有com/gwolf/Dept.xml配置文件。
建立一个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
进行代码测试类的编写: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()); } }
此时测试通过,则springboot与mybatis已经可以成功的整合在一起进行项目开发,此时的配置要比之前使用spring+mybatis直接配置简单N多倍。