上一篇博文已经详细介绍了SpringBoot环境搭建过程,这一节介绍一下SpringBoot整合Mybatis,在mapper中数据库的查询操作使用的是注解的方式,本文将介绍使用XML的方式进行整合。
SpringBoot整合Mybatis
方式2:mapper使用XML
项目结构
1:准备数据库数据和配置数据源以及配置Mybatis的XML配置文件
#加载Mybatis配置文件 mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-location = classpath:config/sqlMapConfig.xml
#数据源必填项 spring.datasource.driver-class-name= com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456
#选填 # Number of ms to wait before throwing an exception if no connection is available. spring.datasource.tomcat.max-wait=10000 # Maximum number of active connections that can be allocated from this pool at the same time. spring.datasource.tomcat.max-active=50 # Validate the connection before borrowing it from the pool. spring.datasource.tomcat.test-on-borrow=true |
2:编写Mapper.Xml和sqlMapConfig.xml
userMapper.xml |
<?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.liujia.springdemo.mapper.UserMapper" > <select id="getAll" resultType="com.liujia.springdemo.entity.User" > SELECT * FROM user </select> </mapper> |