SpringBoot整合Mybatis

        ORM全称是:Object Relational Mapping (对象关系映射),其主要作用是在编程中,把面向对象的概念跟数据库中表的概念对应起来。

        ORM指的是java对象和数据库表的一个映射关系,用Java对象来表示表中的数据,通过ORM你就可以简化对数据的操作,我们用Mybatis框架来操作Mysql中的数据,这个Mybatis框架就是ORM中的一种。现在我们通过springboot框架来集成Mybatis:

准备工作:
1、创建数据库、表、往表中添加数据:

2、创建Springboot项目,把mybatis框架和mysql驱动选上: 

    spring-boot-starter-test:凡事springboot框架自己提供的依赖,都是以spring-boot-starter开头;

    第三方提供的依赖项,前面是谁提供的就是谁的名字,后面就是spring-boot-starter:mybatis-spring-boot-starter,这是mybatis自己提供的,目的是让你在springboot项目中来使用mybatis框架,他会把mybatis所需要的jar包,对象之类的给创建好:

3、创建实体类Student:
public class Student {
    private Integer id;
    private String name;
    private Integer age;
SpringBoot整合Mybatis:
第一种方式:(不推荐)@Mapper

@Mapper:告诉mybatis这是持久层Dao接口,创建此接口的代理对象

第二种方式:(推荐)@MapperScan        

        当Dao接口太多时,逐个在Dao接口上加入@Mapper不方便,推荐使用@MapperScan,Mapper文件和java代码分开管理。

        @MapperScan:放在主启动类上面,Mybatis会根据@MapperScan注解所指定的包,到这个包中找到所有的Dao接口及对应的Mapper文件,创建这些Dao接口的代理对象:

@SpringBootApplication
@MapperScan(basePackages = "com.lifang.demo.dao")
//@MapperScan(basePackages = {"com.lifang.demo.dao","com.lifang.demo.mapper"})
public class Boot3DemoApplication{
    public static void main(String[] args){
        SpringApplication.run(Boot3DemoApplication.class,args);
    }
}

        Mapper文件和Dao接口(java代码)分开管理: 把Mapper文件放在resources目录中

步骤如下: 
1)在resources目录中创建子目录(自定义的),例如mapper目录

2)把mapper文件放到 mapper目录中

3)在application.properties文件中,指定mapper文件的目录位置

4)在主启动类上加入@MapperScan注解,指定持久层Dao接口所在的包名

mybatis:
  #指定Mapper文件的位置:
  mapper-locations: classpath:mapper/*.xml
  #指定mybatis的日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4)在pom.xml中指定把resources目录中的文件,编译到目标目录中:

 <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include> <!--包含resources目录下的任意文件-->
                </includes>
            </resource>

这样写也可以:

  <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>true</filtering>
            </resource>

        </resources>
举例:

                目录结构:        

0、主启动类:
@SpringBootApplication
@MapperScan(basePackages = "com.lifang.demo.dao") //copy reference
//@MapperScan(basePackages = {"com.lifang.demo.dao","com.lifang.demo.mapper"})
public class Boot3DemoApplication{
    public static void main(String[] args){
        SpringApplication.run(Boot3DemoApplication.class,args);
    }
}
 1、Controller层:
@Controller
public class StudentController {
    @Resource
    private StudentService studentService;

    @RequestMapping("/student/query")
    @ResponseBody
    public Student queryStudent(Integer id){
        Student s = studentService.queryStudent(id);
        return s;
    }
}
2、Service层:
public interface StudentService {
    Student queryStudent(Integer id);
}
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;
    @Override
    public Student queryStudent(Integer id) {
        return studentDao.selectById(id);
    }
}
3、Dao层:
public interface StudentDao {
    Student selectById(@Param("stuId") Integer id);
}
4、Sql映射文件:StudentDao.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">
<!--
 namespace:必须有值,自定义的唯一字符串
 推荐使用:dao 接口的全限定名称
--> <mapper namespace="com.lifang.demo.dao.StudentDao">
    <!--
    <select>: 查询数据, 标签中必须是 select 语句
    id: sql 语句的自定义名称,推荐使用 dao 接口中方法名称,
    使用名称表示要执行的 sql 语句
    resultType: 查询语句的返回结果数据类型,使用全限定类名
    -->
    <select id="selectById" resultType="com.lifang.demo.entity.Student">
        <!--要执行的 sql 语句-->
        select id,name,age from student where id = #{stuId}
    </select>

</mapper>
5、application.yml:


  在application.yml文件中指定数据库的连接信息、Mapper文件的位置、开启Mybatis的日志功能

  •    只有当Mapper文件的位置和Dao接口不在同一个目录下,即Mapper文件和Java代码分开管理时才需要指定;
  •    这些选项其实是和你mybatis的主配置文件中的选项都是一样的,以前是写在mybatis的主配置文件中而已:
server:
  port: 9001
  servlet:
    context-path: /orm

  #连接数据库:
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
    username: root
    password: 3333

mybatis:
  #指定Mapper文件的位置:
  mapper-locations: classpath:mapper/*.xml
  #指定mybatis的日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6、在Pom.xml中指定把resources目录中的文件,编译到目标目录中:
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include> <!--包含resources目录下的任意文件-->
                </includes>
            </resource>

        </resources>
7、测试成功:
 

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值