一开始看了其他人的博客我以为搭建Spring boot应该三下五除二就弄好了,但没想到还是遇到许多问题,想做个记录,避免以后忘了。
SQL里选这些,这边其实就是选maven的pom文件里要引入哪些包,有一次我把我认识的都选上了,结果maven下载了一晚上都没有下载完包,一开始不用选择太多,不然有些包要一些额外的配置导致项目起不来,这步Next以后就是Finish啦,完成后要等待maven下载包,有时很慢有时很快
在resources下添加application.yml和application-dev.yml,
加这两个文件(其他大部分也是)是学习这篇博客SpringBoot整合Mybatis完整详细版
文件内容如下
application.yml
spring:
profiles:
active: dev
application-dev.yml(参考博客的是mysql的,和我的这边的oracle的url写法不一样)
server:
port: 8080
spring:
datasource:
username: 用户名
password: 密码
url: jdbc:oracle:thin:@ip:1521/实例
driver-class-name: oracle.jdbc.driver.OracleDriver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.example.entity
#showSql
logging:
level:
com:
example:
mapper : debug
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.2version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
<executions>
<execution>
<id>Generate MyBatis Artifactsid>
<goals>
<goal>generategoal>
goals>
execution>
executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.2version>
dependency>
dependencies>
plugin>
在resources文件下添加generatorConfig.xml(改一下自己的jar包,数据库url,还有包的相关信息)
<generatorConfiguration>
<classPathEntry location="C:/Users/Robbie\.m2/repository/com/oracle/ojdbc/ojdbc8/19.3.0.0/ojdbc8-19.3.0.0.jar" />
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@ip:1521/ypymespt" userId="用户名"
password="密码">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
javaTypeResolver>
<javaModelGenerator targetPackage="com.ssmn.ypymesp.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="mapping"
targetProject="src/main/resources">
<property name="enableSubPackages" value="false" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.ssmn.ypymesp.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
javaClientGenerator>
<table schema="" tableName="YPYMESP_USER">table>
context>
generatorConfiguration>
加入插件以后,右边maven projects正常会出现mybatis-generator,不行的话刷新,或者把上面的plugin重新复制到pom,
双击generatre,如果你是mysql的正常应该可以了,但是oracle的可能会出现报错
这时候需要在刚刚的plugin添加一个依赖
<dependency>
<groupId>cn.easyprojectgroupId>
<artifactId>orai18nartifactId>
<version>12.1.0.2.0version>
dependency>
别看我这好像轻描淡写,加一下完事了,其实我面临好多问题,
UserSerivce.java
package com.ssmn.ypymesp.service;
import com.ssmn.ypymesp.entity.YpymespUser;
import com.ssmn.ypymesp.mapper.YpymespUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
YpymespUserMapper userMapper;
public YpymespUser select(String id) {
return userMapper.selectByPrimaryKey(id);
}
}
UserController
package com.ssmn.ypymesp.controller;
import com.ssmn.ypymesp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getUser/{id}")
public String getUser(@PathVariable String id) {
//生成的实体要加个toString
return userService.select(id).toString();
}
}
在启动类加个注解,就是mapper所在的包
@MapperScan("com.ssmn.ypymesp.mapper")
比如我的:
YpymespApplication.java
package com.ssmn.ypymesp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.ssmn.ypymesp.mapper")
@SpringBootApplication
public class YpymespApplication {
public static void main(String[] args) {
SpringApplication.run(YpymespApplication.class, args);
}
}
还有就是生成的mapper,也要加个注解@Repository
YpymespUserMapper.java
package com.ssmn.ypymesp.mapper;
import com.ssmn.ypymesp.entity.YpymespUser;
import com.ssmn.ypymesp.entity.YpymespUserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface YpymespUserMapper {
int countByExample(YpymespUserExample example);
int deleteByExample(YpymespUserExample example);
int deleteByPrimaryKey(String id);
int insert(YpymespUser record);
int insertSelective(YpymespUser record);
List<YpymespUser> selectByExample(YpymespUserExample example);
YpymespUser selectByPrimaryKey(String id);
int updateByExampleSelective(@Param("record") YpymespUser record, @Param("example") YpymespUserExample example);
int updateByExample(@Param("record") YpymespUser record, @Param("example") YpymespUserExample example);
int updateByPrimaryKeySelective(YpymespUser record);
int updateByPrimaryKey(YpymespUser record);
}
现在可以运行代码看看了,浏览器输入对应接口和id
参考的文章有: