步骤1:首先配置pom.xml引入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
步骤2:配置文件application.properties
server.port=8080
spring.datasource.driverClassName:com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=HO20220504
spring.datasource.username=sa
spring.datasource.password=123456
步骤3:写实体类,分别按照入参出参配置实体类并生成get和set方法。这里不赘述。
步骤4:写service类以及其实现类。
package com.sinqi.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.sinqi.bo.Patient;
@Repository
public class dbserviceimpl {
@Autowired
JdbcTemplate mssql ;
public List<Patient> getPat(String patientName,String idCard,String cardNo){
RowMapper<Patient> map_pat = new BeanPropertyRowMapper<Patient>(Patient.class);
//通过入参拼sql语句。
String sql = "select table05 as patientName, table01 as patientId ,table15 as idCard , "
+ "table02 as cardNo ,vaa35 as patientPhone, \r\n" +
"vaa10 as patientAge , case abw01 when '1' then '男' when '2' then '女' else '未知' end as patientGender\r\n" +
"from table1 \r\n" +
"where vaa05 like '%"+patientName+"%' and vaa15 like '%"+idCard+"%' and vaa02 like '%"+cardNo+"%' ";
List<Patient> list_pat = mssql.query(sql, map_pat);
return list_pat ;
}
}
service类
package com.sinqi.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sinqi.bo.Patient;
import com.sinqi.service.impl.dbserviceimpl;
@Service
public class dbservice {
@Autowired
dbserviceimpl impl ;
public List<Patient> getPat(String patientName,String idCard,String cardNo){
return impl.getPat(patientName, idCard, cardNo);
}
}
步骤5:写controller类
package com.sinqi.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinqi.bo.PatInfo;
import com.sinqi.bo.Patient;
import com.sinqi.service.dbservice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TeacherController {
@Autowired
dbservice service ;
produces= {"application/xml;charset=utf-8"} 这句作用是以xml形式返回,如果不写就以json格式返回了。
@RequestMapping(value="/patient" ,method=RequestMethod.POST,produces= {"application/xml;charset=utf-8"})
@ResponseBody
public List<Patient> getparent(@RequestBody PatInfo pat){
String name = pat.getPatientName() ;
String idcard = pat.getIdCard() ;
String cardno = pat.getCardNo() ;
List<Patient> list_pat = service.getPat(name, idcard, cardno) ;
System.out.println("方法进来了");
return list_pat ;
}
}
步骤6:使用postman进行测试
这是已xml形式传入参数
也可以json格式传参。