spring-mybatis集成 xml配置

web.xml

  
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true"> 

      
    <context-param>  
        <param-name>contextConfigLocationparam-name>  
        <param-value>/WEB-INF/spring-mybatis.xmlparam-value>  
    context-param>  
      
    <filter>  
        <filter-name>encodingFilterfilter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
        <async-supported>trueasync-supported>  
        <init-param>  
            <param-name>encodingparam-name>  
            <param-value>UTF-8param-value>  
        init-param>  
    filter>  
    <filter-mapping>  
        <filter-name>encodingFilterfilter-name>  
        <url-pattern>/*url-pattern>  
    filter-mapping>  
      
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
    listener>  
      
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>  
    listener>  

      
    <servlet>  
        <servlet-name>SpringMVCservlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
        <init-param>  
            <param-name>contextConfigLocationparam-name>  
            <param-value>/WEB-INF/spring-mvc.xmlparam-value>  
        init-param>  
        <load-on-startup>1load-on-startup>  
        <async-supported>trueasync-supported>  
    servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVCservlet-name>  
              <url-pattern>/url-pattern>  
    servlet-mapping>  
    <welcome-file-list>  
        <welcome-file>/index.jspwelcome-file>  
    welcome-file-list>  

web-app>  

**

核心配置文件 spring-mybatis.xml

**

  
<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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
        
    <context:annotation-config/>
      
    <context:component-scan base-package="com" use-default-filters="false" >
    <context:include-filter type="annotation"         expression="org.springframework.stereotype.Repository"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    context:component-scan>  

    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/user_manage" />  
        <property name="username" value="root" />  
        <property name="password" value="1" />  
    bean>  

      
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
           
        <property name="configLocation" value="classpath:MyBatisConfig.xml">property>


    bean>  

    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory">constructor-arg>
    bean>



      
    <bean id="transactionManager"  
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    bean>  

    
    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>


      
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.learn.chapter2.mapper" />  
         <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate">property>
        <property name="annotationClass" value="org.springframework.stereotype.Repository">property>
    bean>  

beans>  

springmvc的xml配置

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
                        http://www.springframework.org/schema/aop    
                        http://www.springframework.org/schema/beans/spring-aop-4.0.xsd  
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                        http://www.springframework.org/schema/tx    
                        http://www.springframework.org/schema/context/spring-tx-4.0.xsd ">
   <mvc:annotation-driven />
    <context:component-scan base-package="com.*" />   
      
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
          
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    bean>  


beans>  

mybatisConfig.xml配置

  
  
<configuration>   
    
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="defaultExecutorType" value="REUSE"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="defaultStatementTimeout" value="25000"/>
    settings>

     <typeAliases>
        <typeAlias type="com.learn.chapter2.po.Role" alias="role"/>
    typeAliases> 

      

    <mappers>  
        <mapper resource="com/learn/chapter2/mapper/roleMapper.xml" />  

    mappers>  

configuration>

log4j配置文件

log4j.rootLogger=DEBUG,Console

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%n
log4j.appender.logfile.File=jbit.log
log4j.logger.com.ibatis=DEBUG
log4j.logger.java.sql.Connection=DEBUGs
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO

文件的目录

spring-mybatis集成 xml配置_第1张图片

service和mapper中都定义了方法,类似,实现类如下

@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Transactional(propagation=Propagation.SUPPORTS)
    public Role getRole(Long id) {
        // TODO Auto-generated method stub
        return this.roleMapper.getRole(id);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public int deleteRole(Long id) {
        // TODO Auto-generated method stub
        return this.roleMapper.deleteRole(id);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    public int insertRole(Role role) {
        // TODO Auto-generated method stub
        return this.roleMapper.insertRole(role);
    }
    @Transactional(propagation=Propagation.SUPPORTS)
    public int countFirstName(String firstname) {
        // TODO Auto-generated method stub
        return this.roleMapper.countFirstName(firstname);
    }
    @Transactional(propagation=Propagation.SUPPORTS)
    public List findRoleByMap(String roleName, String note) {
        // TODO Auto-generated method stub
        return this.roleMapper.findRoleByMap(roleName, note);
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    public List findRoles(String roleName) {
        // TODO Auto-generated method stub
        return this.roleMapper.findRoles(roleName);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public void updateRole(String roleName, String note, Long id) {
        // TODO Auto-generated method stub
        this.roleMapper.updateRole(roleName, note, id);
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    public List findAll() {
        // TODO Auto-generated method stub
        return this.roleMapper.findAll();
    }

}

你可能感兴趣的:(java框架)