1.准备工作
CREATE TABLE account(
id int PRIMARY key auto_increment,
name VARCHAR(100),
money double(7,2)
);
2.创建maven工程并导入坐标
3.配置好配置文件
我自己按着视频配置好了一套,通用环境。
在D盘workspace里。
4.过程中碰到的错误。
处理响应数据乱码。
问题:
解决:工厂在产生mapper对象时。得扫描xml文件。
5.Spring整合mybatis。
这个对象让spring产生。
让Spring帮我创建sqlSessionFactory工厂对象。
然后再让spring帮我创建mapper。
然后在service层注入mapper。
sqlMapConfig.xml,相当于之前学的mabatis-config.xml。
数据源也整进spring容器。本来数据源应该在sqlMapConfig.xml。
本来sqlSession仅仅是一个接口。mabatis和spring的整合包里,提供了这个实现类。
mabatis里面的别名也要配置进spring容器。
mybatis里只放别名就行。
映射扫描mapper可以在spring里配置。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsdv
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsdv
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!--组件扫描service和mapper-->
<context:component-scan base-package="com.itheima">
<!--排除controller,它是mvc配置文件扫描的-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加载映射文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置cp03数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--加载mybaitis核心配置文件。-->
<property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:com/itheima/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>
</beans>
sqlMapConfig-spring.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>
<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>
<!--扫描包的别名也可以-->
<!--<package name="com.itheima.domain"/>-->
</typeAliases>
<!--加载映射文件
<mappers>
<mapper resource="com/itheima/mapper/AccoutMapper.xml"></mapper>
<!–也可以扫包–>
<!–<package name="com.itheima.mapper"/>–>
</mappers>-->
</configuration>
有了这个之后,就会产生mapper的实现。在spring容器中。
然后service里可以这么写了。
AccountServiceimpl
@Service("accountService")
public class AccountServiceimpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void save(Account account) {
accountMapper.save(account);
}
@Override
public List<Account> findAll() {
return accountMapper.findAll();
}
}
6.SSM里的事务管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsdv
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsdv
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!--组件扫描service和mapper-->
<context:component-scan base-package="com.itheima">
<!--排除controller,它是mvc配置文件扫描的-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加载映射文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置cp03数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--加载mybaitis核心配置文件。-->
<property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:com/itheima/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>
<!--声明式事务控制-->
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务增强-->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>