Spring3+MyBatis 整合多方法使用

[color=blue]
web.xml配置文件
[/color]

<?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.xml
Spring 容器启动监听器
-->
<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>


[color=blue]
applicationContext.xml Spring配置文件
[/color]

<?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" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<!-- 导入属性配置文件 -->
<!--< context:property-placeholder location = "classpath:jdbc.properties" /> -->

<!-- 配置数据库连接池,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="/WEB-INF/config/mybatis_config.xml" />
<property name="dataSource" ref="dataSource" />
<!-- <property name="mapperLocations" value="classpath*:mappers-*.xml" /> -->
</bean>

<bean id="userInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.skillmuster.core.dao.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 可配置多个这样的接口映射 -->
<!--<bean id="userInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
<!-- <property name="mapperInterface" value="com.skillmuster.core.dao.UserMapper2" />-->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />-->
<!--</bean>-->

<!-- MyBatis 映射配置,如果接口和mybatis映射文件在同一路径下且命名相同,可采用自动扫描包的方式来注册各种Mapper -->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!-- <property name="basePackage" value="com.skillmuster.core.dao"/>-->
<!-- markerInterface接口的子接口都参与到这个扫描 -->
<!-- <property name="markerInterface" value="com.skillmuster.cor.dao.UserMapper" /> -->
<!--</bean>-->

<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" 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>


[color=blue]
dispatcher-servlet Spring MVC配置文件
[/color]

<?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 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> -->
<!-- <property name="interceptors">-->
<!-- <list>-->
<!-- <bean class="com.fsj.spring.util.MyHandlerInterceptor"/>-->
<!-- </list>-->
<!-- </property>-->
<!-- <property name="order"><value>1</value></property> -->
<!-- </bean> -->

<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> -->
<property name="cacheSeconds" value="0" />
<!-- 配置一下对json数据的转换 -->
<!-- <property name="messageConverters">-->
<!-- <list>-->
<!-- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean> -->

<!--
③:对模型视图名称的解析,即在模型视图名称添加前后缀
InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- p:prefix="/WEB-INF/view/" p:suffix=".jsp" />-->
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>


<!-- spring2.0的配置处理方式 -->
<!-- 处理器映射,它将收到的HTTP请求映射到bean的名字上 -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">-->
<!-- <property name="order"><value>1</value></property>-->
<!-- </bean>-->
<!-- 视图解析器 -->
<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!-- <property name="viewClass" 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" class="com.iss.is.web.controller.demo.student.SaveStudentController">-->
<!-- <property name="studentService">-->
<!-- <ref local="studentService"/>-->
<!-- </property>-->
<!-- <property name="commandClass">-->
<!-- <value>com.iss.is.dto.StudentDTO</value>-->
<!-- </property>-->
<!-- -->
<!-- </bean> -->

</beans>


[color=blue]
jdbc.properties
[/color]

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.databaseurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123



[color=blue]mybatis-config.xml MyBatis总配置文件[/color]

<?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>



[color=blue]UserMapper.xml [/color]

<?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>

[color=blue]UserMapper 映射接口[/color]

public interface UserMapper {
void insertUser(User user);
List<User> getUser();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值