昨天整合了spring和restlet,实现了restful服务,但是数据库这部分还没着落,今天再整合mybatis,这样1个完整的restful服务就搭建完成了。
项目目录
1、配置数据库连接池和mybatis
如果是纯spring服务,则在spring的配置文件application.xml里配置,如果是restlet则在restlet-servlet.xml里配置,这里接着昨天的restlet服务所以在restlet-servlet.xml里配置。
首先配置数据库dataSource,这里采用c3p0连接池。
然后配置sqlSessionFactory,整合spring和mybatis,配置上mapperLocations,就会自动扫描sql文件。
配置sqlSessionTemplate,注入到dao里,通过sqlSessionTemplate操作数据。
xml version="1.0"?> <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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="dataSource" 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.1:3306/xxxx?useUnicode=true"/> <property name="user" value="xxx" /> <property name="password" value="xxx" /> <property name="initialPoolSize"> <value>15value> property> <property name="maxPoolSize" value="50" /> <property name="minPoolSize" value="10" /> <property name="maxIdleTime" value="600" /> <property name="idleConnectionTestPeriod" value="360" /> <property name="preferredTestQuery" value="select 1" /> <property name="acquireIncrement" value="5" /> <property name="acquireRetryAttempts" value="50" /> <property name="acquireRetryDelay" value="1000" /> <property name="breakAfterAcquireFailure" value="true" /> <property name="autoCommitOnClose" value="false" /> <property name="testConnectionOnCheckout" value="true" /> <property name="maxStatements" value="100" /> <property name="maxStatementsPerConnection" value="5" /> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:../sql/*.xml" /> bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory">constructor-arg> bean> beans>
2、创建保存sql的xml文件first-sql.xml
在sql文件里配置sql语句,查询的配置成select标签,修改的配置成update标签。
查询结果和Bean的映射通过restMap,查询list的配置方式和单条是一样的,只不过在dao里用selectList。
参数用#{参数名}
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.chen.mybatis"> <select id="queryOneCar" resultMap="carMapper" > select * from ixintui_table where msgid= #{msgId} select> <select id="queryCarList" resultMap="carMapper" > select * from ixintui_table where msgid LIKE '73309%' select> <select id="queryCarMap" resultType="java.util.HashMap" > select * from ixintui_table where msgid='733095477' select> <update id="updateCity" > update city_list set engineno='0' where city_code='jinan' update> <resultMap id="carMapper" type="com.chen.rest.IxintuiBean"> <result property="messageId" column="msgid" javaType="java.lang.String">result> <result property="plateNumber" column="plate_number" javaType="java.lang.String">result> <result property="token" column="token" javaType="java.lang.String">result> resultMap> mapper>
3、创建dao层
注入sqlSessionTemplate,查询1条结果用sqlSessionTemplate.selectOne;
查询list用selectList;
查询map用selectMap,在sql.xml里resultType写java.util.HashMap。
package com.chen.rest; import org.mybatis.spring.SqlSessionTemplate; import org.restlet.data.Form; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.Post; import org.restlet.resource.ServerResource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; /** * @author chenjie * @version 1.0 * @since 2017-02-09 */ public class QueryCar extends ServerResource { @Resource private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } /** * post请求 * 通过Representation来获取请求参数 * */ @Post public Representation postQueryCar(Representation entity) { Representation representation = new StringRepresentation("hello post"); try { //查询1条结果 IxintuiBean bean = (IxintuiBean)sqlSessionTemplate.selectOne("queryOneCar","733095477"); System.out.println("selectOne="+bean); //查询map Map map = sqlSessionTemplate.selectMap("queryCarMap", "msgid"); System.out.println("selectMap="+map); //查询list Listlist = sqlSessionTemplate.selectList("queryCarList"); for(int i=0;i ) { System.out.println(i+"==="+list.get(i)); } //修改update sqlSessionTemplate.update("updateCity"); } catch(Exception e) { e.printStackTrace(); } return representation; } }
4、创建bean
package com.chen.rest; /** * @author chenjie * @version 1.0 * @since 2017-02-09 */ public class IxintuiBean { private String messageId; private String plateNumber; private String token; public String getMessageId() { return messageId; } public void setMessageId(String messageId) { this.messageId = messageId; } public String getPlateNumber() { return plateNumber; } public void setPlateNumber(String plateNumber) { this.plateNumber = plateNumber; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } @Override public String toString() { return "IxintuiBean{" + "messageId='" + messageId + '\'' + ", plateNumber='" + plateNumber + '\'' + ", token='" + token + '\'' + '}'; } }
5、部署服务,测试