首先是请阅读官方文档 http://mybatis.github.io/spring/zh/
1、配置pom.xml,添加jersey-spring, mybatis-spring, c3p0
-------- spring --------- <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-spring3</artifactId> <version>2.19</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> -------- mybatis --------- <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> --------- c3p0 -------- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.10.Final</version> </dependency>
2、配置 web.xml,加入以下信息,注意确保applicationContext.xml的位置正确。
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
3、配置applicationContext.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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.hangyu.dpim" /> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dpimDataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dpimDataSource" /> </bean> ------ mapper 的位置 ------- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hangyu.dpim.mapper" /> </bean> <bean id="dpimDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://192.168.0.75:3306/dpim" /> <property name="user" value="root" /> <property name="password" value="123456" /> <property name="maxPoolSize" value="50" /> <property name="minPoolSize" value="10" /> <property name="maxStatements" value="100" /> <property name="testConnectionOnCheckout" value="true" /> </bean> </beans>
4、建立Mapper,位于src/main/resources/com/hangyu/dpim/mapper/UserMapper.xml
<?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.hangyu.dpim.mapper.UserMapper"> <cache /> <update id="updateUser"> update user set access_token = "12356778788" </update> </mapper>
5、建立映射接口,位于src/main/java/com/hangyu/dpim/mapper/UserMapper.java
package com.hangyu.dpim.mapper; public interface UserMapper { void updateUser(); }
6、封装service,位于src/main/java/com/hangyu/dpim/service/UserService.java
package com.hangyu.dpim.service; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import com.hangyu.dpim.mapper.UserMapper; @Service public class UserService { @Autowired private UserMapper userMapper; @Transactional public void updateUser(){ userMapper.updateUser(); } }
7、接口调用
package com.hangyu.dpim; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import org.springframework.beans.factory.annotation.Autowired; import com.hangyu.dpim.service.UserService; import com.hangyu.dpim.util.RequestResponseUtil; @Path("ibatis") public class ibatis { @Autowired private UserService userService; @Path("/test") @GET public Response Test(){ userService.updateUser(); return RequestResponseUtil.build200CommonResponse(); } }