Spring和mybatis结合,Spring管理容器,连接数据库等,mybatis负责管理sql语句,sql的入参和出参等

三种方法:

  • 1、原始dao开发(不怎么用,好奇的宝宝可以自己搜搜。是dao层继承一个整合包SqlSessionDaoSupport的类)
  • 2、mapper代理形式开发之原始版(有多少个接口,就得写多少个property)用整合包MapperFactoryBean
  • 比如
  • <property name="mapperInterface" value="cn.tsu.xiaofeng.mapperInterface.UserMapper"/>
  • 3、mapper代理形式开发之扫描包(直接写包名就行)用整合包MapperScannerConfigurer

1、原始dao开发略

2、mapper代理开发之原始版

  1. 先创建工程,把所有的包都导入进来。

    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>
<package name="cn.tsu.xiaofeng.pojo"/>
</typeAliases>
<mappers>
<package name="cn.tsu.xiaofeng.mapperInterface"/>
</mappers>
</configuration>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <context:property-placeholder location="classpath:db.properties"/>
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <!--sqlsession工厂 相当于mybatis的前两句,创建 new sqlsession工厂
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlsessionFactory = new SqlSessionFactoryBuilder().build(resource);
-->
<bean name="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> <!--dao接口开发方法一 -->
<bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><!--MapperFactoryBean-->
<property name="sqlSessionFactory" ref="SqlSessionFactoryBean"/>
<property name="mapperInterface" value="cn.tsu.xiaofeng.mapperInterface.UserMapper"/>
</bean>

在这里我们以前说过mybatis直接使用时是用

InputStream resource = Resources.getResourceAsStream(“SqlMapConfig.xml”);

SqlSessionFactory sqlsessionFactory = new SqlSessionFactoryBuilder().build(resource);创建sqlsession工厂的,现在通过xml文件在容器中创建sqlsession工厂用的是整合包里的SqlSessionFactoryBean

<bean name="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean>因缺数据源和配置文件,故注入

测试文件junit

public class JunitInterface {
@Test
public void testMapper() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = (UserMapper) ac.getBean(UserMapper.class);<!--在这里直接调用id也可以userMapper-->
User user = mapper.selectUser(1);<!--这里需要遵循四大原则-->
System.out.println(user);
}
}

方法2。直接把上面的bean(dao方法yi)换成下面的bean(dao方法二 扫包),用整合包中的MapperScannerConfigurer,不用SqlSessionFactoryBean

<!--dao接口开发方法二  -->
<bean name="MapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tsu.xiaofeng.mapperInterface" />
</bean>

一旦用了方法二,再用junit测试的时候就不可以通过id使用了,因为我们没有定义name,

为什么方法二中不需要注入sqlsession工厂呢?

因为方法二会自动扫描容器中是否含有sqlsession,有的话直接用,并且把包名所有的的接口都扫描到并且注入实现类。因此当我们调用时,只需要调用实现类即可。比如

public class JunitInterface {
@Test
public void testMapper() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = (UserMapper) ac.getBean(UserMapper.class);<!--在这里不可直接调用id也可以userMapper-->
User user = mapper.selectUser(1);<!--这里需要遵循四大原则-->
System.out.println(user);
}
}

资源测试下载

最新文章

  1. Linux 建立文件夹的链接
  2. OpenGL光照和颜色
  3. 用Canvas制作loading动画
  4. flash中网页跳转总结
  5. Oracle帮助类
  6. mysql事务,SET AUTOCOMMIT,START TRANSACTION
  7. poj 3468 成段增减
  8. Log4Net学习【二】
  9. 在C#中使用NPOI2.0操作Excel2003和Excel2007
  10. 实验九--裸机LCD
  11. Struts2.x jsp页面无法使用jsp:forward跳转到action
  12. 转: Transact-sql游标使用详解~~很详细
  13. PAT1009
  14. 数据可视化之MarkPoint
  15. Java经典编程题50道之四
  16. php学习笔记之一维数组
  17. 泛型约束new()的使用
  18. 使用Jmeter监测服务器性能指标
  19. nginx 隐藏 index.php 和 开启 pathinfo 模式的配置
  20. 信息安全技术PGP实验 20155205 20155218

热门文章

  1. 一个简单的CPP处理框架
  2. 无法加载文件或程序集“System.Net.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”
  3. Python实现 QQ 半自动发送情话,我追到了女神
  4. EfficientNet
  5. C#-Func&lt;&gt;
  6. Vuex mapMutation的基本使用
  7. golang中type关键字使用
  8. 一句话木马变形(截止2020年8月16日通杀D盾、安全狗,微步,webshellKiller)
  9. Python 批量保存word
  10. 使用CrashHandler获取应用crash信息