【Mybatis】整合spring

概述:spring可以提供IOC和AOP的管理。Mybatis提供数据持久化到数据库。通过整合,可以利用spring ioc,获得持久化到数据库的mapper。

一、整合jar包

版本不同,说一下包括哪些

spring的:ioc,aop(aop联盟,织入,cglib),beans。context,tx(事务),core等等。

Mybatis的

spring-Mybatis的

数据库的。连接数据库jar包,数据库连接池jar包(C3P0|DBCP)

 

二、搭建项目结构

com.t.dao、com.t.test、com.t.domain、com.t.mapper。配置文件夹config。Lib文件夹

三、添加spring配置文件

applicationContext.xml

四、添加Mybatis的配置文件

配置文件sqlMapConfig.xml.还有对应实体类的Mapper.xml文件

五、三种不同开发方法的配置介绍

mybatis提要:使用mybatis开发持久层。需要一个通过配置文件获得的sqlSession。

预先思考:调用mybatis,除去类映射的配置文件,不说,需要一个初始化的xml(sqlMapConfig.xml)和sqlSession,最终获得一个mapper。或者直接通过sqlSession来直接使用,这种较麻烦。那么整合之后,一定需要的入参就是配置文件,还有数据库的连接配置。

package com.test.test;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.test.domain.User;
import com.test.mapper.UserMapper;

public class test1 {

	public static SqlSession sqlSession;
	public static UserMapper mapper;
	public static void init() throws IOException {
//
		SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//加载配置,获得sqlsession工程,从而获得sqlsession
		SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("sqlMapConfig.xml"));
		
		sqlSession = sqlSessionFactory.openSession();
		//获得mapper
		mapper = sqlSession.getMapper(UserMapper.class);
		System.out.println("test1 init ");
	}
	@Test
	public void testAddUser2() throws Exception {
		init();
		//使用mapper,CRUD
		User user = new User(1,"addmapmth");
		mapper.addUser2(user);
		System.out.println(user);
		sqlSession.commit();
		sqlSession.close();
	}
	
}

spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 加入数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置sqlSessionFactory.注入数据源与配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>

<!-- 原始Dao开发,在Dao中调用sqlSession,如sqlSession.selectOne()。

<bean id="userDao" class="com.t.dao.UserDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
 -->

<!-- Mapper动态代理开发 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="mapperInterface" value="com.t.mapper.UserMapper"></property>
</bean>





<!-- 配置mapper的动态代理 
<bean id="mapperScannerConfiguer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.t.mapper"></property>
</bean>
-->

<!-- 加入bean -->	


</beans>

 

1.简单dao开发

概述:通过在Dao类中调用交给spring的sqlSession。

package com.t.dao;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.t.domain.User;

public class UserDao extends SqlSessionDaoSupport  {
	
	
	@Test
	public void addUser2(User user)
	{
		//获得session
		SqlSession session = super.getSqlSession();
		
		//opensession
		session.update("addUser2", user);
		
		//session.commit();
		
	}
}

2.设置mapper的动态代理

/*
	 * 通过动态代理获得userMapper
	 * 
	 * 
	 * 
	 * */
	@Test
	public  void TestuserMapper() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserMapper mapper = (UserMapper) context.getBean("userMapper");	
		User user = new User();
		user.setUsername("userMapper");
		mapper.addUser2(user);
	}
	

3.设置mapper的动态代理和spring的扫描


	/*
	 * 通过扫描包,生成mapper
	 * 
	 * 
	 * 
	 * */
	@Test
	public void testfun() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserMapper mapper = context.getBean(UserMapper.class);	
		User user = new User();
		user.setUsername("zxc");
		mapper.addUser2(user);
		
	}
	

六、最后着重

着重介绍最后一种,较为方便

sqlMapConfig.xml

<?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>
<!-- 设置自动扫描,将包下的类路径生成别名为类名。举例:com.t.domain.User 别名:User -->
<package name="com.t.domain"/>
</typeAliases>
<mappers>
<!-- 扫描包下的接口文件与对应的mapper.xml文件,注意:文件与类同名,文件的namespace要命名为接口的类路径 -->
<package name="com.t.mapper"/>
</mappers>

</configuration>

 

七、总结

开发流程。

构建架构。加入jar包,搭建包结构,配置spring的context.xml(配置数据库连接池,配置sqlSessionFactory,注入dataSource,注入MyBatis配置文件),添加整合mapper的扫描配置的bean。

加入新业务。加入domain,设置对应mapper的方法和xml。加入spring的配置文件

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值