博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring与mybatis的整合
阅读量:3963 次
发布时间:2019-05-24

本文共 6689 字,大约阅读时间需要 22 分钟。

spring与mybatis的整合

配置文件的更改

applicationContext.xml

db.properties

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=rootjdbc.maxTotal=30jdbc.maxIdle=10jdbc.initialSize=5

mybatis-config.xml

DAO的方式使用mybatis与Spring的整合

  • 使用传统的DAO开发方式进行MyBatis与Spring框架的整合,可以使用mybatis-spring包中提供的SqlSessionTemplate类或SqlSessionDaoSupport类实现。
  • SqlSessionTemplate:它负责管理MyBatis的SqlSession,调用MyBatis的SQL方法,当调用SQL方法时,SqlSessionTemplate将保证使用的SqlSession和当前的Spring事务是相关的,他还管理SqlSession的生命周期,包括必要的关闭,提交,回滚操作
  • SqlSessionDaoSupport:它继承了DaoSupport类,主要作为DAO的基类使用,可以通过SqlSessionDaoSupport类的getSqlSession()方法来获取所需的SqlSession.

代码

在上面的代码基础上添加

Customer

package com.yzb.chapter10;import java.io.Serializable;/** 客户持久化层* */public class Customer implements Serializable {    private Integer id;    private String username;    private String jobs;    private String phone;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getJobs() {        return jobs;    }    public void setJobs(String jobs) {        this.jobs = jobs;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    @Override    public String toString() {        return "Customer{" +                "id=" + id +                ", username='" + username + '\'' +                ", jobs='" + jobs + '\'' +                ", phone='" + phone + '\'' +                '}';    }}

Dao

package com.yzb.chapter10;public interface Dao {    public Customer findCustomerById(Integer id);}

DaoImpl

package com.yzb.chapter10;import org.mybatis.spring.support.SqlSessionDaoSupport;/** 继承SqlSessionDaoSupport* */public class DaoImpl extends SqlSessionDaoSupport implements Dao {    /*    * 通过ID查询客户    * */    @Override    public Customer findCustomerById(Integer id) {        return this.getSqlSession().selectOne("com.yzb.chapter10.CustomerMapper.findCustomerById", id);    }}

CustomerMapper.xml

Test

package com.yzb.chapter10;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {    public static void main(String[] args) {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       // Dao customerDao = (Dao) applicationContext.getBean("customerDao");        Dao bean = applicationContext.getBean(Dao.class);        Customer customerById = bean.findCustomerById(1);        System.out.println(customerById);    }}

applicationContext.xml

mybatis-config.xml

mapper接口

使用Dao开发方式的缺点:会出现大量的重复代码,在方法中也需要指定映射文件中执行语句的id,并且不能保证编写id的正确性,所以这是可以使用Mapper接口编程。

MapperFactoryBean

它是MyBatis-Spring提供的一个根据Mapper接口生成Mapper对象的类,该类在Spring配置文件使用下面这些参数

  • mapperInterface:用于指定接口;
  • SqlSessionFactory:用于指定SqlSessionFactory
  • SqlSessionTemplate:用于指定SqlSessionTemplate,如果SqlSessionFactory同时启用,则只会启用SqlSessionTemplate.

代码

CustomerMapper

package com.yzb.chapter10.example;import com.yzb.chapter10.Customer;public interface CustomerMapper {    public Customer findCustomerById(Integer id);}

CustomerMapper.xml

applicationContext.xml

mybatis-config.xml

MapperScannerConfigurer

  • 对于接口的配置,在mybatis-config.xml中对于mapper.xml的文件的配置,和在appllicationContext.xml中基于Mapper(MapperFactoryBean的配置) ,如果接口过多很麻烦,所以可以用MapperScannerConfigure来代替他们两个的作用
    -它是一种自动扫描的形式来配置MyBatis中的映射器。
    在这里插入图片描述

applicationContex.xml

添加这个代码

去掉这个代码

mybatis-config.xml

去掉相关的mapper.xml的配置文件

运行Test1的代码同样可以出来运行结果

事务

CustomerMapper

package com.yzb.chapter10.example;import com.yzb.chapter10.Customer;public interface CustomerMapper {    public Customer findCustomerById(Integer id);    public void addCustomer(Customer customer);}

CustomerMapper.xml

insert into t_customer (username,jobs,phone) values(#{username},#{jobs},#{phone})

CustomerService

package com.yzb.chapter10.service;import com.yzb.chapter10.Customer;public interface CustomerService{    public void addCustomer(Customer customer);}

CustomerServiceImlp

package com.yzb.chapter10.service;import com.yzb.chapter10.Customer;import com.yzb.chapter10.example.CustomerMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Service@Transactionalpublic class CustomerServiceImpl implements CustomerService {    @Autowired    private CustomerMapper customerMapper;    @Override    public void addCustomer(Customer customer) {        this.customerMapper.addCustomer(customer);        //模拟异常        //int i = 1/0;    }}

applicationContext.xml

Test2

package com.yzb.chapter10.service;import com.yzb.chapter10.Customer;import com.yzb.chapter10.Dao;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test2 {    public static void main(String[] args) {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       // Dao customerDao = (Dao) applicationContext.getBean("customerDao");        CustomerService bean = applicationContext.getBean(CustomerService.class);        Customer customer = new Customer();        customer.setUsername("123");        customer.setJobs("456");        customer.setPhone("789");        bean.addCustomer(customer);    }}

转载地址:http://szwki.baihongyu.com/

你可能感兴趣的文章
awk 运算符
查看>>
awk 控制结构
查看>>
awk 格式化输出
查看>>
awk 正则表达式
查看>>
awk 函数
查看>>
awk 向命令传递参数
查看>>
awk I/O
查看>>
grep 精萃
查看>>
java switch语句
查看>>
java try-with-resources 语句
查看>>
DB2 行转列
查看>>
DB2 认证路线图
查看>>
一个类似行转列的问题
查看>>
遇到问题该如何解决
查看>>
美国金融体系
查看>>
DB CHNGPGS_THRES 参数
查看>>
DB2 特殊寄存器(Special Registers)
查看>>
在ORDER BY 子句中加入主键或唯一键
查看>>
DB2 UPDATE 语句
查看>>
SQL PL 精萃
查看>>