前面我们提到了使用mybatis官方提供的Spring Boot整合包可以实现mybatis的整合,
地址:https://github.com/mybatis/spring-boot-starter
对pom依赖配置如下
<!-- 整合mybatis需要 mybatis相关的jar会自动下载 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
完整的pom文件配置如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wx</groupId>
<artifactId>bootmybatis02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jdbcTemplate 以及事务支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 整合mybatis mybatis相关的jar会自动下载 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 阿里巴巴druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
接下来看整合mybatis的配置,在application.properties文件中配置
#整合mybatis关键配置
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
mybatis.config-location=classpath:mybatis-config.xml
项目目录如下所示
接下来看关键代码
dao层=====>
package com.wx.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.wx.entitys.UserEntity;
@Component("userDao")
public class UserDaoImpl implements IUserDao {
@Autowired
private SqlSessionTemplate sqlTemplate;
public List<UserEntity> queryUserList(){
List<UserEntity> userList = sqlTemplate.selectList("user.queryAll");
return userList;
}
}
SqlSessionTemplate会自动注入,那这就奇怪了,怎么注入的呢,我们来看源代码
我们找到mybatis-spring-boot-autoconfigure-1.2.1.jar
观察画横线处
其实就是对传统的整合方式进行了自动配置
控制层代码如下
package com.wx.controlers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wx.biz.UserBiz;
import com.wx.entitys.UserEntity;
@Controller
public class UserControler {
@Autowired
private UserBiz userBiz;
@RequestMapping("/Users.php")
@ResponseBody
public List<UserEntity> queryUserList(){
return userBiz.queryUserList();
}
}
启动类代码如下:
package com.wx.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
/* 如果没有配置ComponentScan,默认扫描当前包及其子包,如果配置了,就要把当前包也纳入扫描*/
@ComponentScan(basePackages="com.wx.boot,com.wx.dao,com.wx.biz,com.wx.controlers")
public class BootMybatisApp {
public static void main(String[] args) {
SpringApplication.run(BootMybatisApp.class, args);
}
}