springmvc mybatis 調用sql , 轉成json

1.代碼

@Autowired
    private	 SqlSessionFactoryBean  sqlSessionFactory  ;
	@RequestMapping(value = "f3")
	public String f3( @RequestBody Map<String,String>  map,HttpServletRequest request, HttpServletResponse response) throws Exception {
		SqlSessionFactory object = sqlSessionFactory.getObject();
		SqlSession session = null;
		try {
			session = object.openSession();
			Object selectOne = session.selectOne("test.get2", map);
			 MappedStatement mappedStatement = session.getConfiguration().getMappedStatement("test.get2");
			String jsonString = JsonMapper.toJsonString(selectOne);
			System.out.println(11);
		} finally {
			session.close();
		}
		return "f1";
	}
<mapper namespace="test">
	<select id="get1" resultType="TestData">
		SELECT 
			 *
		FROM test_data a
		WHERE a.id = #{id}
	</select>
	
		<select id="get2" resultType="map">
		SELECT 
			*
		FROM test_data a
		WHERE a.id = #{id}
	</select>
</mapper>

2.前臺

$.ajax({
			        url :'${ctx}/test/testData/f3?book=java',
			        type: 'post',
			        dataType : "json",
			        contentType:'application/json;charset=UTF-8',//關鍵是要加上這行
			        data : JSON.stringify( {id:'22',remarks:'aaa'}),
			        async : false, // 同步請求
			        success : function(data) {
			             debugger ;
			        }
			    });

 

3,轉成json   ,在數據庫就轉成 json

        <select id="get2" resultType="map">
        SELECT 
            id,user_id, date_format(create_date,'%Y-%m-%d') as cdate
        FROM test_data a
        WHERE a.id = #{id}
    </select>javascript

4, 數據對比

 

a.id AS "id",
		a.user_id AS "user.id",
		a.office_id AS "office.id",
		a.area_id AS "area.id",
		a.name AS "name",
		a.sex AS "sex",
		a.in_date AS "inDate",
		a.create_by AS "createBy.id",
	     date_format(create_date,'%Y-%m-%d')  AS "createDate",
		a.update_by AS "updateBy.id",
		date_format(update_date,'%Y-%m-%d') AS "updateDate",
		a.remarks AS "remarks",
		testint,
		a.del_flag AS "delFlag"

上面能夠從新 封裝成mapjava

相關文章
相關標籤/搜索