mybaits 整合spring

第一、        
传统Dao 模式开放

1.1创建映射文件

<?xml version="1.0" encoding="UTF-8" ?>

SELECT * FROM  account

<!–进行条件判断

如果条件符合就加上where  如果不行就去掉–>

AND id=#{id}

1.2
创建Dao接口

package com.itheima.dao;

import com.itheima.po.Gustomer;

import java.util.List;

public interface CustomerDao {

//通过id查询客户

public List findCutomerById(Gustomer gustomer);

}

1.3创建Dao实现类

package com.itheima.impl;

import com.itheima.dao.CustomerDao;

import com.itheima.po.Gustomer;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {

@Override

//通过id查询产品

public List findCutomerById(Gustomer gustomer) {

/**虽然继承SqlSessionDaoSupport有getSqlSession这个方法,

*但是还是要注入getSqlSessionFactory,因为源码中有set方法,通过getSqlSessionFactory来拿到getSqlSession

* 要通过spring注入,用bean

*/

return this.getSqlSession().selectList(“com.itheima.po.CustomerMapper.findCutomerById”, gustomer);

}

}

1.4编写applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd

">

<context:component-scan base-package=“com.itheima.service”/>

<context:property-placeholder location=“classpath:db.properties”/>

<tx:annotation-driven transaction-manager=“transactionManager”/>

1.5进行单元测试

package com.itheima.Test;

import com.itheima.dao.CustomerDao;

import com.itheima.mapper.CustomerMapper;

import com.itheima.po.Gustomer;

import com.itheima.service.CustomerServiece;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;

import java.util.List;

/**

* 测试Dao

*/

//@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境

@RunWith(SpringJUnit4ClassRunner.class)

//ContextConfiguration使用注解引入配置文件

@ContextConfiguration(locations = “classpath:applicationContext.xml”)

public class DaoTest {

@Autowired //与junit整合,不需要再spring xml配置扫描 bean

private CustomerDao customerDao;

@Test

public void demo01(){

//因为是List集合  所以先用list接受,

List list=new ArrayList();

//要调用sett方法,才能进行操作,创建对象

Gustomer gustomer=new Gustomer();

gustomer.setId(11);

// 执行映射文件中定义的Sql 并返回映射结果集

list= customerDao.findCutomerById(gustomer);//打印输出

for (Gustomer cutomerById : list) {

System.out.println(cutomerById);

}

}

第二、        
Mapper代理开发(基于MapperFactoryBean)

2.1
创建GustomerMapper接口

package com.itheima.mapper;

import com.itheima.po.Gustomer;

import java.util.List;

public interface CustomerMapper {

// 通过id查询客户

public List findCutomerById(Gustomer gustomer);

}

2.2
创建映射文件

<?xml version="1.0" encoding="UTF-8" ?>

SELECT * FROM  account

AND id=#{id}

2.3编写applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd

">

<context:component-scan base-package=“com.itheima.service”/>

<context:property-placeholder location=“classpath:db.properties”/>

<tx:annotation-driven transaction-manager=“transactionManager”/>

2.4进行单元测试

@Test

public void demo02(){

//因为是List集合  所以先用list接受,

List list=new ArrayList();

//要调用sett方法,才能进行操作,创建对象

Gustomer gustomer=new Gustomer();

gustomer.setId(11);

// 执行映射文件中定义的Sql 并返回映射结果集

list= customerDao.findCutomerById(gustomer);

for (Gustomer cutomerById : list) {

System.out.println(cutomerById);

}

}

第三、        
基于Mapper代理开发(基于MapperScannerConfigurer)

第四、        
测试事务

4.1 CustomerMapper接口

package com.itheima.mapper;

import com.itheima.po.Gustomer;

import java.util.List;

public interface CustomerMapper {

// 添加客户

public void addCutomer(Gustomer customer);

}

4.2
Service接口

package com.itheima.service;

import com.itheima.po.Gustomer;

public interface CustomerServiece {

//添加方法

public void addCutomer(Gustomer customer);

}

4.3实现
Service接口

package com.itheima.service;

import com.itheima.mapper.CustomerMapper;

import com.itheima.po.Gustomer;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

//service层

@Service

//@Transactional 是声明式事务管理 编程中使用的注解

@Transactional

public class CustomerServieceImp  implements CustomerServiece {

//注解注入CustomerMapper

@Autowired

private CustomerMapper customerMapper;

//    添加客户

@Override

public void addCutomer(Gustomer customer) {

this.customerMapper.addCutomer(customer);

// int i=1/0;//模拟添加操作突然出现的异常

}

}

4.4映射文件编写

INSERT  into

account(username,balance) VALUES  (#{username},#{balance})

4.5编写编写applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd

">

<context:component-scan base-package=“com.itheima.service”/>

<context:property-placeholder location=“classpath:db.properties”/>

<tx:annotation-driven transaction-manager=“transactionManager”/>

4.6单元测试

@Test

public void demo03(){

//创建对象

Gustomer c=new Gustomer();

c.setUsername(“小米”);

c.setBalance(323.1);

//调用方法

customerServiece.addCutomer©;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值