spring-mybatis集成 xml配置

来源:互联网 发布:seo蜘蛛侠破解版 编辑:程序博客网 时间:2024/06/05 15:10

web.xml

<?xml version="1.0" encoding="UTF-8"?>  <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">     <!-- Spring和mybatis的配置文件 -->      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>/WEB-INF/spring-mybatis.xml</param-value>      </context-param>      <!-- 编码过滤器 -->      <filter>          <filter-name>encodingFilter</filter-name>          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>          <async-supported>true</async-supported>          <init-param>              <param-name>encoding</param-name>              <param-value>UTF-8</param-value>          </init-param>      </filter>      <filter-mapping>          <filter-name>encodingFilter</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping>      <!-- Spring监听器 -->      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>      <!-- 防止Spring内存溢出监听器 -->      <listener>          <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>      </listener>      <!-- Spring MVC servlet -->      <servlet>          <servlet-name>SpringMVC</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>/WEB-INF/spring-mvc.xml</param-value>          </init-param>          <load-on-startup>1</load-on-startup>          <async-supported>true</async-supported>      </servlet>      <servlet-mapping>          <servlet-name>SpringMVC</servlet-name>                <url-pattern>/</url-pattern>      </servlet-mapping>      <welcome-file-list>          <welcome-file>/index.jsp</welcome-file>      </welcome-file-list>  </web-app>  

**

核心配置文件 spring-mybatis.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-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/>    <!-- 自动扫描的方式将service 和respository(dao)读取到spring上下文中 -->      <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>      <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">          <property name="dataSource" ref="dataSource" />          <!-- 自动扫描mapping.xml文件 -->           <property name="configLocation" value="classpath:MyBatisConfig.xml"></property>    </bean>      <!--配置模版类sqlSessionTempalte 这是spring和mybatis集成的核心配置-->    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>    </bean>    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->      <bean id="transactionManager"     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">          <property name="dataSource" ref="dataSource" />      </bean>      <!--使用注解处理事务,处理spring Service中的 @Transactional注解-->    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>    <!-- 启用自动扫描的方式创建mapper bean -->      <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配置

<?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: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">          <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->          <property name="prefix" value="/WEB-INF/jsp/" />          <property name="suffix" value=".jsp" />      </bean>  </beans>  

mybatisConfig.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>       <!-- 设置延迟加载,在用到属性的时候会加载,可以配合子查询减少资源占用 -->    <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>     <!-- 对事务的管理和连接池的配置    <environments default="development">          <environment id="development">              <transactionManager type="JDBC" />              <dataSource type="POOLED">                   <property name="driver" value="${driver}" />                <property name="url" value="${url}" />                  <property name="username" value="root" />                  <property name="password" value="1" />              </dataSource>          </environment>      </environments>      -->      <mappers>          <mapper resource="com/learn/chapter2/mapper/roleMapper.xml" />      </mappers>  </configuration>

log4j配置文件

log4j.rootLogger=DEBUG,Console#Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%nlog4j.appender.logfile.File=jbit.loglog4j.logger.com.ibatis=DEBUGlog4j.logger.java.sql.Connection=DEBUGslog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFO

文件的目录

这里写图片描述

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

@Servicepublic 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<Role> findRoleByMap(String roleName, String note) {        // TODO Auto-generated method stub        return this.roleMapper.findRoleByMap(roleName, note);    }    @Transactional(propagation=Propagation.SUPPORTS)    public List<Role> 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<Role> findAll() {        // TODO Auto-generated method stub        return this.roleMapper.findAll();    }}
原创粉丝点击