用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试

这一部分的主要目的是 配置spring-service.xml  也就是配置spring  并测试service层 是否配置成功

用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
在这个基础上面 继续进行spring的配置。

回顾上面  我们已经成功测试通过了Mybatis的配置. 这时候的目录结构是:

一:下面我们继续补充目录结构,在com.peakfortake的文件目录项目  创建service、dto和util文件夹,在spring文件下面创建spring-service.xml
文件。并复制spring-dao.xml的头 到spring-service.xml中 如图

 

二:配置spring-service.xml  这个配置来说 是比较简单的

<?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:tx="http://www.springframework.org/schema/tx"
       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">

    <!--spring service配置-->
    <!-- 扫描service包下面所有使用注解的的类型-->
    <context:component-scan base-package="peakfortake.service"/>

    <!-- 配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- 因为这个spring-service中没有定义dataSource但在spring-dao中已经定义了 所以后期执行的时候 会自动找到-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置基于注解声明事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

三:实现service  并加入注解 最主要的是当中的注解  @service 和@Transactional  尤其是事务的注解

@Service
public class TestUserServiceImpl implements TestUserService {
    //注入依赖
    @Autowired
    private TestUserDao testUserDao;

    @Transactional
    /**
     * 使用注解控制事务方法的优点
     * 1:开发团队达成一致的约定,明确标注事务方法的编码规范
     * 2:保证事务方法的执行时间尽可能短,不要穿插其他网络操作 如HTTP操作或者剥离事务外部方法
     * 3:不是所有的方法都需要事务,如果有一条修改操作 只读操作 不需要事务控制
     */
    public List<String> getAllUserName() {
        List<TestUser> t = testUserDao.getUser();

        List<String> allName = new ArrayList<String>();
        for (TestUser tt : t) {
            allName.add(tt.getUserName());
        }
        return allName;
    }

    public List<TestUser> userList() {
        return testUserDao.getUser();
    }
}

 

四:建立logback日志 并测试 

1、在resource下面建立logback.xml文件 
并在logbakc的官网上面  http://logback.qos.ch/manual/configuration.html 其中的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>


2:测试类是

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class TestUService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TestUserService testUserService;

    @Test
    public void getTN(){
        List<String> mm= testUserService.getAllUserName();
        logger.info("AllName={}",mm.toString());
    }
}

 测试通过:说明  spring配置完成

这个时候我们的文件结构是:

然后最后一步 就是进行
spring-web的配置。。

 

用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc

posted @ 2016-11-28 18:08  草帽boy  阅读(1685)  评论(0编辑  收藏  举报