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. 聊天泡泡(仿微信)By-H罗

    在做私信时,聊天泡泡仿着QQ做时,聊天泡泡底图有露出,不怎么好看,微信的就比较好看,当时就因为那2行纠结了好久 - (void)viewDidLoad { [super viewDidLoad]; / ...

  2. 《Effective Python》笔记——第4章 元类及属性

    一.用属性取代get和set方法 常规的get和set方法如下: class OldResistor(): def __init__(self, ohms): self.__ohms = ohms d ...

  3. 基于6U VPX 的mSATA高性能数据存储板

    一.板卡概述 该产品系我司自主研发.基于标准6U VPX架构. 二.产品特性       大存储容量8TB       读写方式RAID0 ,读写速率2GB/s       四路x4 SRIO@5Gb ...

  4. CoRR 2018 | Horovod: Fast and Easy Distributed Deep Learning in Tensorflow

    将深度学习模型的训练从单GPU扩展到多GPU主要面临以下问题:(1)训练框架必须支持GPU间的通信,(2)用户必须更改大量代码以使用多GPU进行训练.为了克服这些问题,本文提出了Horovod,它通过 ...

  5. JVM学习——类加载机制(学习过程)

    JVM--类加载机制 2020年02月07日14:49:19-开始学习JVM(Class Loader) 类加载机制 类加载器深入解析与阶段分解 在Java代码中,类型的加载.连接与初始化过程中都是在 ...

  6. DotNet Dictionary 实现简介

    一:前言 本来笔者对DotNet的Hashtable及Dictionary认识一直集中在使用上,一个直接用object 一个可以用泛型,以前也只大概看过Hashtable的实现.最近查MSDN时发现有 ...

  7. BI系统要自研还是采购?这篇文章告诉你

    首先说一下目前市面上的BI工具都有哪些吧.总体上,其实分为免费和付费两大阵营.免费阵营里,为首的当然是GA(也就是谷歌分析).PowerBI,但是由于有墙的限制,很多公司没法使用前者.付费阵营里,近两 ...

  8. 传统式与自助式BI分析平台有什么区别

    如今自助式BI分析平台已经成为众多企业进行数据分析工作时的首选,究竟自助式BI分析平台在数据分析中有哪些优势,可以受到企业如此的青睐与追捧呢?小编将在本文中,跟大家一起来了解自助式BI分析平台的概念. ...

  9. tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:56281 npm ERR! network This is most likely not a problem with npm itself npm ERR! network and is related to network

    tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:56281npm ERR! networ ...

  10. linux 提权防护

    linux 用户提权 利用系统漏洞或者程序等方面的缺陷使一个低权限用户,获得他们原本不具有的权限或者得到root权限 下面介绍几种可能得到root访问权限的方式 1, 内核开发 防护:及时更新补丁 臭 ...