目录
目录
一、前提
1、本文章主要写一个springBoot+mybatis+oracle的一个模板;
2、数据源,oracle
create table SYSTEM.STUDENT
(
name VARCHAR2(12),
age NUMBER,
sex VARCHAR2(2)
);
insert into student(name,age,sex) values('lisi',20,'男');
insert into student(name,age,sex) values('wangli',34,'女');
CREATE TABLE `lesson`(
`id` VARCHAR(12) PRIMARY KEY,
`name` VARCHAR(36) NOT NULL ,
`credit` FLOAT DEFAULT 0,
`teacher` VARCHAR(36) DEFAULT 0
);
INSERT INTO lesson (id, name, credit, teacher) VALUES ('1', 'chiness', 5, 'zhangsan');
INSERT INTO lesson (id, name, credit, teacher) VALUES ('2', 'math', 5, 'lisi');
二、操作
1、项目结构
2、pom配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- orcale驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.1-atlassian-hosted</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、po类
@Data
@Accessors(chain = true)
public class Student {
private String name;
private int age;
private String sex;
}
4、xml
<?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.spb.mybatis.mapper.StudentMapper">
<select id="getStudentInfo" resultType="com.spb.mybatis.po.Student">
select * from student
</select>
</mapper>
5、mapper及service层
import java.util.List;
@Mapper
public interface StudentMapper {
List<Student> getStudentInfo();
}
@Service
public class StudentService{
@Autowired
StudentMapper studentMapper;
public List<Student> getStudentInfo() {
return studentMapper.getStudentInfo();
}
}
6、controller层
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/getInfo")
@ResponseBody
public List<Student> getStudentInfo(){
List<Student> studentInfo = studentService.getStudentInfo();
return studentInfo;
}
}
7、页面测试
三、注意
xml文件要放到resources文件夹下,再从配置文件配置其位置:
mybatis.mapper-locations=classpath:mapping/*.xml
如果将xml放在src下,即使在配置文件配置对其位置,target下也不会出现对应的xml,即程序会报错;