Spring 对 Mybatis 的整合思路

作为Bean容器,Spring框架提供了IoC机制,可以接管所有的组件的创建工作并进行依赖管理,整合的主要工作就是把Mybatis框架使用中所涉及的核心组件配置到Spring容器中,交给Spring和创建和管理。

具体来说,业务逻辑对象依赖基于Mybatis技术实现Dao对象,核心是获取SqlSession实例,则需要依赖SqlSessionFactory而SqlSessionFactory是SqlSessionFactoryBuider依据Mybatis配置文件中的数据源、SQL映射文件等信息来构建的。

整合优势:

Spring对Mybatis 进行整合,在对组件实现解耦的同时还能使Mybatis框架的使用变得更加方便和简单。此外,通过Spring提供的声明式事务等服务,能进一步简化开发工作量,提高开发效率。


整合示例

 

1、数据库建表添加测试数据

use mybatis;
CREATE TABLE `user`  (
  `uId` int(4) NOT NULL AUTO_INCREMENT,
  `uName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `uPwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `account` decimal(10, 2) NOT NULL,
  PRIMARY KEY (`uId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;


INSERT INTO `user` VALUES (2, '张三', '123456', 123.00);
INSERT INTO `user` VALUES (9, '李四', '123123', 123.93);
INSERT INTO `user` VALUES (10, '王五', '123123', 200.93);

2、配置项目(重点)

连接数据库的配置文件:db.properties

//mysql驱动类
jdbc.driver=com.mysql.jdbc.Driver

//mysql连接的url地址
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8

//连接mysql数据库的用户名
jdbc.username=root

//连接mysql数据库的密码
jdbc.password=xiaotong

spring核心配置文件:spring.cfg.xml





	
	

	
	

	
	
		
		
		
		
	

	
	
	
		
		
		
		
		
		
			
				
				classpath:mapper/userMapper.xml
			
		
	

	
	
		
	

mybatis 核心配置文件:mybatis.cfg.xml





	
	
		
		
		
	

	
	
		
	

2、实体类:User.java

package entity;

public class User {

    private int uId;

    private String uName;

    private String uPwd;

    private double account;

    public User() {
    }

    public User(int uId, String uName, String uPwd, double account) {
        this.uId = uId;
        this.uName = uName;
        this.uPwd = uPwd;
        this.account = account;
    }

    @Override
    public String toString() {
        return "User{" +
                "uId=" + uId +
                ", uName='" + uName + '\'' +
                ", uPwd='" + uPwd + '\'' +
                ", account=" + account +
                '}';
    }

    public int getuId() {
        return uId;
    }

    public void setuId(int uId) {
        this.uId = uId;
    }

    public String getuName() {
        return uName;
    }

    public void setuName(String uName) {
        this.uName = uName;
    }

    public String getuPwd() {
        return uPwd;
    }

    public void setuPwd(String uPwd) {
        this.uPwd = uPwd;
    }

    public double getAccount() {
        return account;
    }

    public void setAccount(double account) {
        this.account = account;
    }
}

3、数据访问层

dao层接口:userDao.java

package dao;

import entity.User;

import java.util.List;

public interface UserDao {
    List getAllUser();
}

mapper映射文件:userMapper.xml




    

4、服务层

服务层接口:userService.java

package service;

public interface UserService {
	void demo();
}

服务层实现类:userServiceImpl.java

package service;

import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	@Qualifier("userDao")
	private UserDao userDao;
	@Override
	public void demo() {
		// TODO Auto-generated method stub
		System.out.println(userDao.getAllUser());
	}
}

5、测试

junit测试:test.java


import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;

public class test {
	@Test
	public void testDemo(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.cfg.xml");
		UserService service = (UserService)context.getBean("userService");
		service.demo();
	}

}

测试结果:

Spring 对 Mybatis 的整合思路_第1张图片

你可能感兴趣的:(Spring框架,MyBatis框架)