Spring3+MyBatis 整合多方法使用
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>/WEB-INF/view/welcome.jsp</welcome-file> </welcome-file-list> <!-- 配置加载Spring配置文件路径 --><!-- <context-param>--><!-- <param-name>contextConfigLocation</param-name>--><!-- <param-value>/WEB-INF/applicationContext.xml</param-value>--><!-- </context-param>--> <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xmlSpring 容器启动监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <!-- 配置字符过滤器 --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> <!-- 强制进行转码 --> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置 Spring view分发器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml, /WEB-INF/config/dispatcher-servlet.xml </param-value> </init-param> <!-- 启动加载一次 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <!-- 这里可以用 / 但不能用 /* ,拦截了所有请求会导致静态资源无法访问 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置session超时 --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- 配置异常页面 --> <error-page> <error-code>404</error-code> <location>/error_page.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error_page.jsp</location> </error-page> <!-- 配置要使用到的标签 --> <jsp-config> <taglib> <taglib-uri>http://www.springframework.org/tags</taglib-uri> <taglib-location>/WEB-INF/tld/spring.tld</taglib-location> </taglib> <taglib><taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri><taglib-location>/WEB-INF/tld/c.tld</taglib-location></taglib> </jsp-config> </web-app>
<?xml version="1.0" encoding="UTF-8" standalone="no"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" default-autowire="byName" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 加载读取properties配置文件参数 --><bean id="propertyConfigurer" /> --><!-- 配置数据库连接池,使用dbcp --><bean id="dataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.databaseurl}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><!-- 连接池启动时的初始值 --><property name="initialSize" value="1" /><!-- 连接池的最大值 --><property name="maxActive" value="500" /><!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --><property name="maxIdle" value="2" /><!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --><property name="minIdle" value="1" /></bean><!-- MyBatis定义数据源,同意加载配置 --><bean id="sqlSessionFactory" value="/WEB-INF/config/mybatis_config.xml" /><property name="dataSource" ref="dataSource" /><!--<property name="mapperLocations" value="classpath*:mappers-*.xml" /> --></bean><bean id="userInfoDao" value="com.skillmuster.core.dao.UserMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean><!-- 可配置多个这样的接口映射 --><!--<bean id="userInfoDao" value="com.skillmuster.core.dao.UserMapper2" />--><!--<property name="sqlSessionFactory" ref="sqlSessionFactory" />--><!--</bean>--><!-- MyBatis 映射配置,如果接口和mybatis映射文件在同一路径下且命名相同,可采用自动扫描包的方式来注册各种Mapper --><!--<bean value="com.skillmuster.core.dao"/>--><!-- markerInterface接口的子接口都参与到这个扫描 --><!--<property name="markerInterface" value="com.skillmuster.cor.dao.UserMapper" /> --><!--</bean>--><!-- 配置事物管理 --><bean id="transactionManager" ref="dataSource" /></bean><!-- 申明事务通知 --><tx:advice id="txAdivice" transaction-manager="transactionManager"><tx:attributes><tx:method name="insert*" isolation="READ_COMMITTED"propagation="REQUIRED"rollback-for="Exception" /><tx:method name="query*" propagation="NOT_SUPPORTED" isolation="DEFAULT" rollback-for="java.io.IOException" no-rollback-for="java.lang.ArithmeticException,java.lang.*" timeout="30" read-only="true" /></tx:attributes></tx:advice><!-- 将通知和切入点联接 --><aop:config><!-- 分开配置 --><!-- <aop:pointcut expression="execution(* com.iss.is.service.impl..*.*(..))" id="allServiceMethod" /> --><!--<aop:advisor advice-ref="txAdivice" pointcut-ref="allServiceMethod" />--><!-- 一起配置 --><!--<aop:advisor advice-ref="txAdivice" pointcut="exection(* com.skillmuster.core.service.*ServiceImpl.*(..))" />--></aop:config></beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName"> <!-- default-autowire="byName",约定优于配置 --> <!-- @Controller 请求映射注解扫描,必须加上这个,不然请求controller时会出现no mapping url错误--><mvc:annotation-driven /> <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd --><mvc:resources mapping="/img/**" location="/img/"/><mvc:resources mapping="/js/**" location="/js/"/><mvc:resources mapping="/css/**" location="/css/"/><mvc:resources mapping="/html/**" location="/html/"/> <!-- ①:注解扫描,对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.skillmuster.core" /> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,//添加拦截器,类级别的处理器映射 --><!--<bean value="0" /> <!-- 配置一下对json数据的转换 --><!-- <property name="messageConverters">--><!-- <list>--><!-- <bean p:suffix=".jsp" />--> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <!-- spring2.0的配置处理方式 --><!-- 处理器映射,它将收到的HTTP请求映射到bean的名字上 --><!-- <bean value="org.springframework.web.servlet.view.JstlView" />--><!-- <property name="contentType">--><!-- <value>text/html;charset=UTF-8</value>--><!-- </property>--><!-- 页面路径 --><!-- <property name="prefix" value="/WEB-INF/pages/" />--><!-- <property name="suffix" value=".jsp" />--><!-- </bean> --><!-- Controller配置 --><!-- <bean name="/saveStudent.do" name="code"> jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.databaseurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123
<?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 alias="userinfo" type="com.skillmuster.core.pojo.User" /> </typeAliases> <!-- 实体接口映射资源 --><mappers><mapper resource="com/skillmuster/core/dao/UserMapper.xml"/></mappers></configuration>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.skillmuster.core.dao.UserMapper"><resultMap type="userinfo" id="userResultMap"><id property="id" column="id" /><result property="username" column="username" /><result property="password" column="password" /></resultMap><insert id="insertUser" parameterType="userinfo"><![CDATA[insert into user(username,password) values(#{username},#{password}) ]]> </insert> <!-- mybsits_config中配置的alias类别名,也可直接配置resultType为类路劲 --><select id="getUser" resultType="userinfo">select * from user </select><!-- 当使用该Mybatis与Spring整合的时候,该文件必须和相应的Mapper接口文件同名,并在同一路径下 --></mapper>
public interface UserMapper { void insertUser(User user); List<User> getUser();}