主要操作工厂类获取SqlSession
注意导入相关jar包:mybatis-spring-1.2.2.jar,当然也要有mybatics的jar包
AccountMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- com.briup.db.AccountDao是我们定义接口的全限定名字 这样就可以使用接口调用映射的SQL语句了 这个名字一定要和接口对应上 -->
<mapper namespace="com.briup.db.AccountDao">
<update id="update">
update t_account
set
balance=#{1}
where id=#{0}
</update>
</mapper>
spring_mybatis.xml,在这里面直接配置了mybatics的相关配置。。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 读取这个资源文件 读完之后下面就可以用${key}来去文件中的value值了 -->
<!-- 这种方式是我们第一节学习的那种配置方式方式的简写 -->
<context:property-placeholder location="classpath:oracle.properties" />
<!-- dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${driver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<!-- 最大连接数 -->
<property name="maxActive">
<value>80</value>
</property>
<!-- 最大空闲连接数 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间 单位:毫秒 -->
<!-- 超过时间则抛出异常,如果设置为-1表示无限等待 -->
<property name="maxWait">
<value>3000</value>
</property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 起别名 -->
<property name="typeAliasesPackage" value="com.briup.db"></property>
<!-- 是否开启缓存 -->
<property name="configurationProperties">
<props>
<prop key="cacheEnabled">true</prop>
</props>
</property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/briup/db/mybatis/AccountMapper.xml" />
<!--<property name="mapperLocations" value="classpath:com/briup/db/mybatis/*.xml" />-->
</bean>
<!-- 自动扫描映射接口所在的包 -->
<!-- 将来可以通过接口的名字首字母小写作为beanName,从spring容器中拿出自动生成的该接口的实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.briup.db" />
</bean>
</beans>
//
上面采用的是不使用config的方式
<!-- 配置sqlSessionFactory 不使用mybatis-config.xml-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.briup.db"></property>
<property name="configurationProperties">
<props>
<prop key="cacheEnabled">true</prop>
</props>
</property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/briup/db/mybatis/AccountMapper.xml" />
</bean>
或者:
<!-- 配置sqlSessionFactory 使用mybatis-config.xml-->
<!-- 直接读取mybatis-config.xml文件,里面和之前配置的一样 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
之前用mybatis时(5大步)
1.读取加载config(交给了spring来做)
2.扫描mapper(交给了spring来做)
3.获取sqlsession(交给了spring来做)
4.通过getMapper获取实现类对象->
AccountDao dao = session.getMapper(AccountDao.class);
dao.update(1, 1200);
而spring->
AccountDao dao = (AccountDao) container.getBean("accountDao");//通过获取接口类的名字
dao.update(1, 1200);
5.通过对象调用方法:dao.update(1, 1200);
现在用spring时(3大步)
1.通过spring提供的工厂类将相关配置注入进去,生成sqlsession
例如:别名,数据源,扫描映射信息
2.spring通过获取接口类的名字首字母小写的方式获取mapper接口类对应的实现类对象
3.通过对象调用方法
测试:
public class MyBatisTest {
//知识点: spring中使用mybatis
@Test
public void mybatis_update(){
try {
String path = "com/briup/db/mybatis/spring_mybatis.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
//..
//..
//AccountDao dao = session.getMapper(AccountDao.class);
//dao.update(1, 1200);
AccountDao dao = (AccountDao) container.getBean("accountDao");//通过获取接口类的名字
dao.update(1, 1200);
System.out.println(dao.getClass());
container.destroy();
}catch(Exception e) {
e.printStackTrace();
}
}
}
更新完成