案例描述
平台自身的查询不满足查询需求时,可自定义Dao层自己写SQL来查询
实现思路
自定义Dao层接口与XML映射
Service实现类中调运Dao层接口
自定义的Controller中,调运service层接口查询数据,并返回数据
具体代码类
1、Controller
package com.project.demo.controller;
import com.je.core.base.AbstractDynaController;
import com.je.core.base.MethodArgument;
import com.project.demo.service.DemoSsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/je/ssmDemo")
public class DemoController extends AbstractDynaController {
@Autowired
DemoSsmService demoSsmService;
/**
* 查询所有数据
* @param param
*/
@RequestMapping(
value = {"/doDemo"},
method = {RequestMethod.POST},
produces = {"application/json; charset=utf-8"}
)
@ResponseBody
public void doDemo(MethodArgument param) {
demoSsmService.doDemo();
}
}
2、Service与ServiceImpl
//Servcie
package com.project.demo.service;
public interface DemoSsmService {
public String doDemo();
}
//ServiceImpl
package com.project.demo.service;
import com.project.demo.dao.DemoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component("demoSsmService")
public class DemoServiceImpl implements DemoSsmService {
@Autowired
DemoDao demoDao;
@Override
public String doDemo() {
List<Map> list = demoDao.selectAll();
System.out.println(list.size());
return null;
}
}
3、Dao层
package com.project.demo.dao;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
@Mapper
public interface DemoDao {
public List<Map> selectAll();
}
4、Dao层映射xml
注意:Dao层映射的xml需要放在resources/public目录下,才能被扫描到>注意:Dao层映射的xml需要放在resources/public目录下,才能被扫描到
<?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.project.demo.dao.DemoDao">
<select id="selectAll" resultType="java.util.Map">
select * from je_core_enduser
</select>
</mapper>
5、 配置文件更改
注意:在public/beans.xml加上dao层映射的xml扫描
<property name="mapperLocations" value="classpath*:mybatis/mapper/*.xml"></property>
<!-- MapperScanner 扫描Mapper 创建代理 -->
<property name="basePackage" value="com.je.**.mapper,com.project.demo.mapper" />
6、修改je-web.xml
<resource>
<directory>src/main/resources/public/mybatis</directory>
<targetPath>mybatis</targetPath>
</resource>
最后编辑: 呼丽华 文档更新时间: 2024-08-21 10:58 作者:呼丽华