记一次Mybatis+SpringBoot+Oracle的基本配置
代码目录:
MybatisUtil 和mybatis-config.xml可以暂时忽略
package com.example.springbootoracle;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.springbootoracle.mapper")
public class SpringbootOracleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootOracleApplication.class, args);
}
}
package com.example.springbootoracle.controller;
import com.example.dbutil.MybatisUtil;
import com.example.springbootoracle.model.User;
import com.example.springbootoracle.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Iterator;
import java.util.List;
@Controller
@RequestMapping("/user")
public class SysUserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(User user) {
int i = userService.addUser(user);
return i;
}
@ResponseBody
@RequestMapping(value="/getnamebyid",method = RequestMethod.GET)
public void getNameById(@RequestParam(value="id",required = true)String id){
String books = userService.findNameById(id);
System.out.println(books);
}
}
package com.example.springbootoracle.mapper;
import com.example.springbootoracle.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface UserMapper {
Integer insert(User user);
String findNameById(@Param("id") String id);
}
package com.example.springbootoracle.model;
public class User {
private String id;
private String name;
private String age;
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;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com.example.springbootoracle.service;
import com.example.springbootoracle.mapper.UserMapper;
import com.example.springbootoracle.model.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service(value = "userService")
public class UserService {
@Resource
private UserMapper userMapper;
public int addUser(User user) {
int insert = userMapper.insert(user);
return insert;
}
public String findNameById(String id){
return userMapper.findNameById(id);
}
}
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.example.springbootoracle.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.example.springbootoracle.model.User" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id,name,age
</sql>
<insert id="insert" parameterType="com.example.springbootoracle.model.User" >
insert into user_ (id, name, age)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=VARCHAR})
</insert>
<select id="findNameById" resultType="java.lang.String" parameterType="java.lang.String">
select name from user_ where id=#{id,jdbcType=VARCHAR}
</select>
</mapper>
pom.xml:
<?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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-oracle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-oracle</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-jdbc</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>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
经历了非常多次的查询语句正常,但是Intelij 输出查询结果都是null,足足6个小时的debug,一度自闭,最后想想是不是,OracleSqlDeveloper占用着我的端口,搞到我对数据库的操作经常执行不了?于是我先关掉我的OracleSqlDeveloper客户端,非常的无语,代码顺利运行。
测试url:
http://localhost:8080/user/getnamebyid?id=111
控制台输出检索结果
sql 语句:
create table user_(
id VARCHAR2(4) primary key not null,
name VARCHAR2(4) not null,
age VARCHAR2(4) not null
)
insert into user_ values('111','sper','21');
select * from user_ where id = '111';