之前一直没有时间写点总结,对于一些细节总是查阅资料把握不够,今天梳理下,完善下方便后续快速迭代。
准备工作。创建一张表
--单表操作 CREATE TABLE tb_user (
userid number(4) NOT NULL primary key,
user_name varchar2(100) unique not null,
pwd varchar2(100),
age number(3) ,
sex varchar(2),
birthday date
);
create sequence seq_user; --插入测试数据
insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'张三','123456',10,'男',sysdate);
insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'李四','123456',10,'男',sysdate);
insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'王五','123456',10,'男',sysdate);
insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'赵六','123456',10,'男',sysdate);
select * from tb_user;
既然是springboot整合mybatis + oracle
0 添加环境信息,贴上application.properties
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.username=scott
spring.datasource.password=****
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.pian.dto
1 贴上pom依赖 ,oracle使用此版本可以直接maven下载。
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.oracle
ojdbc6
11.2.0.3
2 创建dto实体
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User implements Serializable {
private int userid ;
private String userName;
private String pwd ;
private int age;
private String sex;
private Date birthday;
}
3 创建dao层,使用mapper注解,springboot会自动创建实体类
/**
* @Auther: Administrator
* @Date: 2020/2/8 0008 20:43
* @Description:
*/
@Mapper
public interface UserMapper {
//增
User selectById(int userId);
}
4 创建service层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User selectById(int userId){
return userMapper.selectById(userId);
}
}
5 常见测试方法
/**
* @Auther: Administrator
* @Date: 2020/2/8 0008 21:14
* @Description:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {App.class})
public class AppTest {
@Autowired
private UserService userService;
@Test
public void testSelectById(){
User user = userService.selectById(2);
System.out.println(user);
}
}
6 直接测试开始,测试结果
0208 220431.810- WARN[ main] com.zaxxer.hikari.util.DriverDataSource -68: Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
0208 220432.057- INFO[ main] com.zaxxer.hikari.pool.PoolBase -516: HikariPool-1 - Driver does not support get/set network timeout for connections. (oracle.jdbc.driver.T4CConnection.getNetworkTimeout()I)
0208 220432.063- INFO[ main] com.zaxxer.hikari.HikariDataSource -123: HikariPool-1 - Start completed.
User(userid=2, userName=null, pwd=123456, age=10, sex=男, birthday=Sat Feb 08 20:40:45 CST 2020)
0208 220432.239- INFO[ Thread-2] o.s.w.c.s.GenericWebApplicationContext -993: Closing org.springframework.web.context.support.GenericWebApplicationContext@30b6ffe0: startup date [Sat Feb 08 22:04:27 CST 2020]; root of context hierarchy
0208 220432.264- INFO[ Thread-2] com.zaxxer.hikari.HikariDataSource -381: HikariPool-1 - Shutdown initiated...
0208 220432.280- INFO[ Thread-2] com.zaxxer.hikari.HikariDataSource -383: HikariPool-1 - Shutdown completed.
完美!!!
总结,传统的mybaits需要mybatis-config.xml,springboot整合后,直接在application.properties配置参数就可以了。
@mapper注解自动创建接口类。另外直接在启动类添加
@MapperScan("com.XX.dao") 也是直接可以的,@mapper注解就可以省略了。