Spring集成MyBatis

使用MyBatis,需要创建MyBatis框架中的某些对象,使用这些对象,就能使用mybatis提供的功能了。

  1. 需要有Dao接口的代理对象,例如StudentDao接口,需要一个他的代理对象,使用SqlSession.getMapper(StudentDao.class),得到dao代理对象。
  2. 需要由SqlSessionFactory,创建SqlSessionFactory才能使用openSession得到SqlSession对象。
  3. 在处理大型项目的时候,MyBatis提供的数据源(PooledDataSource)难以满足项目需求,通常会更换一个连接池对象。
Spring容器 (面对Spring开发,从Spring中获得对象)
DataSource对象,数据源
SqlSessionFactory对象
dao接口的代理对象1~n
注:以上对象都由Spring使用IOC创建好对象。

实现步骤:

  1. 使用msyql数据库,使用学生表student(id int 主键列 自动增长,

    ​ name varchar(80) ,

    ​ age int )。

  2. 创建maven项目

  3. 加入依赖:spring依赖,mybatis依赖,mysql驱动,junit依赖,mybatis-spring依赖(mybatis网站上提供的,用来在spring项目中创建mybatis对象),spring事务相关的依赖。

  4. 创建实体类Student

  5. 创建Dao接口和Mapper文件写SQL语句

  6. 写mybatis主配置文件

  7. 创建service接口其实现类

  8. 创建spring的配置文件

    • 声明数据源DataSource,使用阿里的Druid连接池
    • 声明SqlSessionFactoryBeanlei,在这个类内部创建的是SqlSessionFactory对象
    • 声明MapperScannerConfiguration类,在内部创建dao代理对象,创建的对象都放在spring容器中。
    • 声明service对象,将上一步中的dao赋值给service属性,让其进行业务层的操作
  9. 测试dao访问数据库

操作

  1. 自己建表,三个属性(id(主键),name,age)

  2. 创建maven项目(IDEA)

  3. 具体依赖:

    • junit (单元测试)

    • spring-context (spring依赖)

    • spring-tx ,spring-jdbc(spring事务的依赖)

    • mybatis(mybatis依赖)

    • mybatis-spring(mybatis和spring集成的依赖)

    • mysql-connector-java(msyql驱动)

    • druid(阿里连接池)

    • <!-- resource插件 -->
      <build>
      <resources>
      <resource>
      <directory>src/main/java</directory>
      <includes>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
      </includes>
      <filtering>false</filtering>
      </resource>
      </resources>
      </build>
  4. 创建实体类

    public class Student {
    private Integer id;
    private String name;
    private Integer age; public Integer getId() {
    return id;
    } @Override
    public String toString() {
    return "Student{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", age=" + age +
    '}';
    } public void setId(Integer id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Integer getAge() {
    return age;
    } public void setAge(Integer age) {
    this.age = age;
    }
    }
  5. 创建Dao接口和mapper文件

    public interface StudentDao {
    
        int insertStudent(Student student);
    
        List<Student> selectStudents();
    
    }
    <!--  -->
    <?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.wang.dao.StudentDao"> <insert id="insertStudent">
    insert into student(name,age) values(#{name},#{age})
    </insert> <select id="selectStudents" resultType="com.wang.domain.Student">
    select id,name,age from student
    </select> </mapper>
  6. 写MyBatis主配置文件


    <?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> <typeAliases>
    <package name="com.wang.domain"/>
    </typeAliases> <!--MyBatis的SQL语句和映射配置文件,指定其他Mapper文件的位置-->
    <!-- 找到相应的sql语句-->
    <mappers>
    <mapper resource="com/wang/dao/StudentDao.xml"/>
    </mappers>
    </configuration>
  7. 创建service接口和实现类

    public interface StudentService {
    
        int addStudent(Student student);
    
        List<Student> queryStudent();
    
    }
    public class StudentServiceImpl implements StudentService {
    
    	private StudentDao studentDao;
    
    	public void setStudentDao(StudentDao studentDao){
    this.studentDao = studentDao;
    } @Override
    public int addStudent(Student student) {
    int i = studentDao.insertStudent(student);
    return i;
    } @Override
    public List<Student> queryStudent() {
    List<Student> students = studentDao.selectStudents();
    return students;
    }
    }
  8. 创建spring配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 声明数据源-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="jdbc:mysql://localhost:3306/learnjdbc" />
    <property name="username" value="root" />
    <property name="password" value="wang" />
    </bean> <!-- 声明SqlSessionFactoryBean-->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 指定数据源 -->
    <property name="dataSource" ref="myDataSource"/> <!-- 指定MyBatis主配置文件 -->
    <property name="configLocation" value="classpath:MyBatisConfig.xml" /> </bean> <!-- 声明MapperScannerConfiguration
    SqlSession.getMapper(StudentDao.class);
    MapperScannerConfigurer作用是:
    循环basePackage所表示的包,把保重的每一个接口都找到,调用SqlSession.getMapper()把每个
    dao接口都创建出dao对象,dao代理对象放在容器中。
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 指定SqlSessionFactory对象名称 -->
    <property name="sqlSessionFactoryBeanName" value="factory"/>
    <!-- 指定基本包,dao接口所在的包名 -->
    <property name="basePackage" value="com.wang.dao"/>
    </bean> <!-- 声明service-->
    <bean id="studentService" class="com.wang.service.impl.StudentServiceImpl">
    <property name="studentDao" ref="studentDao"/>
    </bean>
    </beans>

接下来就可以通过junit进行测试了。

结尾:

  • 该过程中的一部分可以使用注解的方式进行(略)
  • MyBatis通常使用配置文件的方式,spring通常使用注解的方式
  • ssm框架的整合实际上就是MyBatis和spring框架的整合
  • 接下来再添加springMvc后,在控制层调用相应的业务处理层即是service的方法就可以实现ssm框架的运行。

Spring和MyBatis框架整合的更多相关文章

  1. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  2. SSM(Spring+SpringMVC+MyBatis)框架整合开发流程

    回忆了 Spring.SpringMVC.MyBatis 框架整合,完善一个小demo,包括基本的增删改查功能. 开发环境 IDEA MySQL 5.7 Tomcat 9 Maven 3.2.5 需要 ...

  3. 框架整合——Spring与MyBatis框架整合

    Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...

  4. Spring+SpringMVC+Mybatis框架整合流程

    一:基本步骤 新建Maven项目,导入相关依赖.(推荐) ————–Mybatis配置 —————- 新建entity包,并根据数据库(表)新建相关实体类. 新建dao包,并根据业务创建必要的mapp ...

  5. SSM Spring SpringMVC Mybatis框架整合Java配置完整版

    以前用着SSH都是老师给配好的,自己直接改就可以.但是公司主流还是SSM,就自己研究了一下Java版本的配置.网上大多是基于xnl的配置,但是越往后越新的项目都开始基于JavaConfig配置了,这也 ...

  6. Spring + Spring MVC + MyBatis框架整合

    ---恢复内容开始--- 一.Maven Web项目创建 如有需要,请参考:使用maven创建web项目 二.Spring + Spring MVC + MyBatis整合 1.Maven引入需要的J ...

  7. Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)

    项目结构: (代码里面都有注释) 一.在pom文件中依赖jar包 因为我这里分了模块,所以有父子级的共两个pom文件 父级: <?xml version="1.0" enco ...

  8. Spring+SpringMVC+mybatis框架整合

    1.jdbc.properties 1 driverClassName=com.mysql.jdbc.Driver 2 url=jdbc\:mysql\://127.0.0.1\:3306/slsal ...

  9. spring+springMVC+mybatis框架整合——配置文件说明

    如下图 web.xml配置说明: spring配置文件说明-1: spring配置文件说明-2: spring配置助记:  扫注(base) 读配(loc) 数据源(和comb(使用c3p0数据源)) ...

随机推荐

  1. ios xib约束适配要点

    基本上以下几点就能解决除横屏以外的适配问题 1.页边距约束 (Leading and Trailing space) 页边间距约束分前部间距约束(Leading space constaint)和尾部 ...

  2. 类(静态)变量和类(静态)static方法以及main方法、代码块,final方法的使用,单例设计模式

    类的加载:时间 1.创建对象实例(new 一个新对象时) 2.创建子类对象实例,父类也会被加载 3.使用类的静态成员时(静态属性,静态方法) 一.static 静态变量:类变量,静态属性(会被该类的所 ...

  3. PHP+mysql常考题

    PHP+mysql常考题 来自<PHP程序员面试笔试宝典>,涵盖了近三年了各大型企业常考的PHP面试题,针对面试题提取出来各种面试知识也涵盖在了本书. 常考的mysql基础题 问题:设教务 ...

  4. Note/Solution -「洛谷 P6466」分散层叠算法

    \(\mathcal{Description}\)   Link.   给定 \(m\) 个长度为 \(n\) 的有严格升序且不包含重复元素的序列 \(a_1,a_2,\cdots,a_m\),\(q ...

  5. VSCode官方的配置同步方案

    前言 这几天在迁移电脑工作环境,对于VSCode,我实在不想从头做下载插件.配置代码规则这样的事情,于是求助百度,搜索结果靠前的解决方案基本都是使用Setings Sync插件,于是我就从了. 经过好 ...

  6. PHP7.x环境下安装redis扩展

    注:以下介绍的安装方式为PHP的安装路径为/usr/local/php,如果你的服务器上PHP的安装目录不一致请按实际情况处理. 首先下载PHP7的redis扩展 wget https://githu ...

  7. 使用SpringBoot整合MybatisPlus出现 : java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    解决方案一: 将测试类的包路径改为和主启动类的一致 解决方法二: 不想改测试类的路径 就在测试类上添加要测试的类的classes

  8. jmeter变量嵌套:__V

    问题复现 ${name_${n}} 下面没有获取到结果 解决方案 __V是用于执行变量名表达式 ${__V(name_${n})} 获取到结果

  9. 一个快速制作表格的方法,和熬夜做表say拜拜

    如今已是大数据时代了,统计工作是非常繁琐的一项工作,通常统计老师为了录单工作到下半夜或者是通宵,现在有了很多制作表单的软件,可以大大减轻基层统计老师的工作量,也增加了会员资料的保密性,给我们统计工作带 ...

  10. 【windows 操作系统】线程句柄HANDLE与线程ID的关系

    什么是句柄 句柄是一种指向指针的指针.我们知道,所谓指针是一种内存地址.应用程序启动后,组成这个程序的各对象是住留在内存的.如果简单地理解,似乎我们只要获知这个内存的首地址,那么就可以随时用这个地址访 ...