一.在上一篇博客我简单记录了SpringBoot整合Mybatis(注解方式)
https://blog.csdn.net/u012430402/article/details/80185366
以下记录依赖于上一篇博客的工程
二.工程目录如下:
三.在pom.xml中引入Druid依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.5</version> </dependency>
四.在application.yml中设置使用Druid数据源和Mapper映射文件的路径
spring: datasource: url: jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver username: root password: root type: com.alibaba.druid.pool.DruidDataSource mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.xiao.pojo
五.书写映射文件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.xiao.mapper.UserMapper"> <!-- 根据id查询用户 --> <select id="getUser" parameterType="Integer" resultType="User"> select * from tb_user where id = #{id} </select> </mapper>
六.启动项目,访问地址http://localhost:8080/getUser?id=1
成功!
源码下载地址:https://github.com/devilyang123/All_Demo/tree/master/SpringBoot_02
扫描二维码关注公众号,回复:
901526 查看本文章