一:引入jar包
Maven引入jar包(mybatis+connector)
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
二:配置mybatis的主配置文件
mabatis的主要配置文件命名为mybatis-config.xml 如下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置全局属性 --> <settings> <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 --> <setting name="useGeneratedKeys" value="true" /> <!-- 使用列别名替换列名 默认:true --> <setting name="useColumnLabel" value="true" /> <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true" /> <!-- 打印查询语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
内容如上,加了注释就不解释了
二:配置mybatis的mapper文件(xml使用mybatis)
1:mapper文件主要是存放sql语句的配置文件,如下的AreaMapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.project.myo2o.mapper.AreaMapper"> <select id="queryArea" resultType="com.project.myo2o.entity.Area"> SELECT * FROM tb_area ORDER BY priority DESC </select> </mapper>
<mapper namespace="com.project.myo2o.mapper.AreaMapper">这个标签填写mapper接口类的位置
<select id="queryArea" resultType="com.project.myo2o.entity.Area">
id为mapper接口类里面的一个方法名,resultType为结果集的映射类型,选择实体类(结果集的映射可以自己定义,标签为resultMap)
2:接口类的mapper文件 如下AreaMapper
import com.project.myo2o.entity.Area; import java.util.List; public interface AreaMapper { List<Area> queryArea(); }
三:在启动类读取mapper文件
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan @MapperScan("com/project/myo2o/mapper") public class Myo2oApplication { public static void main(String[] args) { SpringApplication.run(Myo2oApplication.class, args); } }
@MapperScan("com/project/myo2o/mapper") 这个读取必须写,不然springboot读取不到,当然也可以在mapper文件上加@Mapper注解,不过不推荐,这个更方便
四:配置yml文件(properties文件)
spring: datasource: url: jdbc:mysql://localhost:3307/myo2o?useUnique=true&characterEncoding=utf8&useSSL=false username: root password: 123456 driverClassName: com.mysql.jdbc.Driver
添加如上的数据库连接文件,之后mybatis便可以自动读取配置并且加载
五:使用
添加一个serviceImpl
@Service public class AreaServiceImpl implements AreaService { @Autowired private AreaMapper areaMapper; @Override public List<Area> find() { List<Area> areaList = areaMapper.queryArea(); return areaList; } }
直接注入mapper接口,便可以调用里面的方法了
六:总结
第一次写博客,逻辑不是很清楚,总结下以上内容,有不对的请轻喷~~~~~~~
首先导入jar包,之后配置好mybatis的主配置文件和mapper配置文件(其实yml里面配置的数据库信息可以直接配置在mybatis的主配置文件中,不过为了后面的连接池使用就没有这么做),配置好yml中的数据库信息,以及启动类中扫描mapper的路径之后,就可以直接通过mapper接口操纵数据库了。
mybatis具体的工作原理和流程可以看看源码和其他的教程,后续更新添加druid连接池的使用方法。
https://blog.csdn.net/qq_36784299/article/details/80250691 springboot+mybatis+druid连接池
https://download.csdn.net/download/qq_36784299/10402440 源码