SSM(二)--spring+mybatis

1,导入整合的相关依赖

<!--业务层开始-->
<!--spring和mybatis整合-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.1</version>
</dependency>
<!--spring核心框架-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<!--spring事务管理-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<!--aop切面相关-->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.9</version>
</dependency>
<!--业务层结束-->

2,创建spring的核心配置文件applicationContext.xml,将sqlMapConfig.xml中配置的都替换掉,直至不再需要sqlMapConfig.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启注解扫描包,创建业务层所有类对象-->
    <context:component-scan base-package="com.it.service"></context:component-scan>
    <!--
    	引入外部属性文件,替换sqlMapConfig.xml中的
    	<properties resource="jdbc.properties"></properties>
    -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--
    	创建数据源,替换sqlMapConfig.xml中的
	<environments default="development">
    		<environment id="development">
        		<transactionManager type="JDBC"/>
        			<dataSource type="POOLED">
            			<property name="driver" value="${jdbc.driver}"/>
            			<property name="url" value="${jdbc.url}"/>
           			 <property name="username" value="${jdbc.username}"/>
            			<property name="password" value="${jdbc.password}"/>
        			</dataSource>
    		</environment>
	</environments>
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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>
    <!--声明式事务-->
    <!--1.创建事务管理器对象-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.配置通知对象-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!--该类方法只读的事务, 如果有事务,加入事务执行,如果没有事务,非事务执行-->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" isolation="DEFAULT"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" isolation="DEFAULT"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" isolation="DEFAULT"/>
            <!--该类方法非只读的事务,如果有事务,加入事务执行,如果没有事务,则创建事务执行-->
            <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!--3.aop配置:切面配置-->
    <aop:config>
        <!--配置切面,指定切入点-->
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.it.service.impl.*.*(..))"></aop:advisor>
    </aop:config>
    <!--创建sqlSessionFactory对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源对象-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置方法一:引入SqlMapConfig.xml文件-->
        <!--<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>-->
        <--
        配置方法二:别名映射,,替换sqlMapConfig.xml中的
        <typeAliases>
    		<!--配置别名,引用时只需要写简单类名,不需要全类名-->
    		<package name="com.it.domain"></package>
	</typeAliases>
        -->
        <property name="typeAliasesPackage" value="com.it.domain"></property>
        <!--可以注入其他的属性-->
        <!--<property name="configurationProperties" value=""></property>-->
    </bean>
    <!--扫描dao层接口的包, 创建动态代理对象, 存入到spring容器中
    	替换sqlMapConfig.xml中的
    	<mappers>
    		<!--映射该包下的全部映射文件-->
    		<package name="com.it.dao"></package>
	</mappers>
	包括在测试类中通过动态代理创建的对象
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.it.dao"></property>
    </bean>
</beans>

3.测试spring和mybatis整合

@Test
public void testMybatisAndSpring(){
    //创建spring容器
    ApplicationContext ac  = new ClassPathXmlApplicationContext("applicationContext.xml");
    //获取容器中的对象
    AccountDao accountDao = ac.getBean(AccountDao.class);
    //执行方法
    List<Account> accountList = accountDao.findAll();
    //遍历结果
    for (Account account : accountList) {
        System.out.println(account);
    }
}

4.编写业务层接口AccountService

public interface AccountService {
    /*查询全部*/
    public List<Account> findAll();

    /*根据id查*/
    public Account findById(Integer id);

    /*增加*/
    public void save(Account account);

    /*删除*/
    public void delete(Integer id);

    /*修改*/
    public void update(Account account);
}

5.编写业务层接口的实现类AccountSeviceImpl.java

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao;

    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }

    @Override
    public void delete(Integer id) {
        accountDao.delete(id);
    }

    @Override
    public void update(Account account) {
        accountDao.update(account);
    }
}

6.测试service

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestService {
    @Autowired
    AccountService accountService;

    @Test
    public void test() {
        List<Account> accountList = accountService.findAll();
        for (Account account : accountList) {
            System.out.println(account);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值