1. POM文件添加依赖
<!-- Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- oracle --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency>
2.controller
@Resource private ControllerService service; @RequestMapping(value="/GetIndoorCheckItemEntities",method=RequestMethod.GET) @ApiOperation("获取数据库内容") public ArrayList<IndoorCheckItemEntity> GetIndoorCheckItemEntities(){ return service.GetIndoorCheckItemEntities(); }
3. service
@Service public class ControllerService { @Autowired private IIndoorInfoDao dao; public ArrayList<IndoorCheckItemEntity> GetIndoorCheckItemEntities() { return dao.GetWscheckItemsOfIndoor(); } }
4. DAO
public interface IIndoorInfoDao { public ArrayList<IndoorCheckItemEntity> GetWscheckItemsOfIndoor(); }
5. entity
public class IndoorCheckItemEntity implements Serializable { private static final long serialVersionUID = -6556793741331167103L; private int id; private int itemId; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getItemId() { return itemId; } public void setItemId(int itemId) { this.itemId = itemId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
6. 数据库连接字符串:
spring.datasource.url = jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.88)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ))
spring.datasource.username = XXXXXX
spring.datasource.password = YYYYYY
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#XML文件位置 指定DAO与SQL语句关联
mybatis.mapperLocations=classpath:mapper/*.xml
7.入口函数配置扫描mapper
@SpringBootApplication @EnableSwagger2 @MapperScan("com.pkyou.Sample.Dao") public class Main { public static void main(String[] args) throws Exception { SpringApplication.run(Main.class, args); } }
8. SQL语句
<?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.pkyou.Sample.Dao.IIndoorInfoDao"> <select id="GetWscheckItemsOfIndoor" resultType="com.pkyou.Sample.Entyties.IndoorCheckItemEntity"> select id itemId,name from yn_app_wscheckitems where parent_id=0 and type='1' </select> </mapper>
9. 项目结构