1、新建一个springboot项目,数据库建表
2、关于oracle驱动安装:安装方法
3、依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 持久层 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!-- Mybatis 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!-- Oracle连接驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
4、application.yml
spring:
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: name
password: pwd
driver-class-name: oracle.jdbc.driver.OracleDriver
mybatis:
type-aliases-package: com.busy.service.model
mapper-locations: classpath:mapper/**/*.xml
mapper:
mappers: com.busy.service.comon.BaseMapper
identity: ORACLE
pagehelper:
helperDialect: oracle
reasonable: true
supportMethodsArguments: true
params: count=countSql
5、新建包 com.busy.service.common 添加公共mapper
package com.busy.service.common;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface BaseMapper<T> extends Mapper<T>,MySqlMapper<T> {
}
6、新建包 com.busy.service.model 添加User类
package com.busy.service.model;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "R_TEST_USER_R")
public class User {
@Id
@Column(name = "U_ID")
private String id;
@Column(name = "U_NAME")
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
7、新建包 com.busy.service.mapper 添加mapper接口
package com.busy.service.mapper;
import com.busy.service.common.BaseMapper;
import com.busy.service.model.User;
public interface UserMapper extends BaseMapper<User>{
}
8、在在resource下新建mapper文件夹,添加UserMapper.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.busy.servive.mapper.UserMapper">
</mapper>
9、启动类添加注解
@SpringBootApplication
@MapperScan(basePackages = { "com.busy.service.mapper" })
10、在src/test/java 下新建包com.busy.service 添加测试基类
package com.busy.service;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes= ApplicationStarter.class)
public class BaseTest {
}
11、在测试下新增包 com.busy.service.mapper 新建测试类
package com.busy.service.mapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.busy.service.BaseTest;
import com.busy.service.model.User;
public class UserMapperTest extends BaseTest{
@Autowired
private UserMapper userMapper;
@Test
public void insert() {
User user = new User();
user.setId("1");
user.setName("a");
userMapper.insert(user);
}
@Test
public void select() {
User user = new User();
user.setId("1");
System.out.println(userMapper.selectOne(user).toString());
}
@Test
public void update() {
User user = new User();
user.setId("2");
user.setName("aa");
int updateNum = userMapper.updateByPrimaryKeySelective(user);
System.out.println("更新记录数" + updateNum);
}
@Test
public void delete() {
User user = new User();
user.setId("1");
int updateNum = userMapper.delete(user);
System.out.println("删除记录数" + updateNum);
}
}