国内数据库应用:申通数据库+Springboot
开放
现在,让我们实际使用代码进行操作,连接到数据库,看看它是否可以与其他数据库相同
首先,创建一个springboot项目,我们不会详细介绍如何创建它;
由于这是一个国内数据库,他有自己的驱动程序:com.oscar.driver
那么这个驱动程序所依赖的程序包是从哪里来的呢?目前,我们在Maven库中找不到它。我们只能在官方网站上提供的安装目录中找到它
引入
引入依赖程序包:
1.介绍OSCAR jar包
我们将放置oscarJDBC16。jar包在项目目录lib中,然后依赖Maven中的这个jar包
<依赖关系>
<groupId>com。oscarjdbc</groupId>
<artifactId>oscarJDBC16</artifactId>
<版本>1.0</版本>
<scope>系统</scope>
<systemiPath>${basedir}/src/main/lib/ocarJDBC16。jar</systemiPath>
</dependency>
2.介绍mybatis依赖包
由于我们仍在使用mybatis生成来快速创建数据库,因此我们需要具有以下依赖项
<依赖关系>
<groupId>组织。mys。生成器</groupId>
<artifactId>mybatis生成器核心</artifactId>
<版本>1.3.7</版本>
</dependency>
<依赖关系>
<groupId>组织。mys。春天启动</groupId>
<artifactId>mybatis春季启动程序</artifactId>
<版本>1.3.2</版本>
</dependency>
快速创建代码
在resources目录中创建generatorConfig.xml
<?XML版本=“1.0”编码=“UTF-8”
<!DOCTYPE生成器配置
PUBLIC“//mybatis.org//DTD mybatis Generator Configuration 1.0//EN”
“http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd“>
<generatorConfiguration>
<!-<ClassPathEntry location=“F:/cache/mysql连接器Java-5.1.28 bin.jar”/>-->
<context id=“testTables”targetRun time=“MyBatis3”>
<commentGenerator>
<property name=“suppressAllComments”value=“true”/>
</commentGenerator>
<jdbcConnection driverClass=“com.oscar.Driver”connectionURL=“jdbc:oscar://192.168.1.xx:2003/osrdb“UserId=”SYSDBA“password=”szoscar55“>
</jdbcConnection>
<JavaTypeResolver>
<property name=“forceBigDecimals”value=“false”/>
</JavaTypeResolver>
<JavaModelGenerator targetPackage=“com.student.modules.database.model”targetProject=“.\src\main\resources\mybatis”>
<property name=“enableSubPackages”value=“false”/>
<property name=“trimStrings”value=“true”/>
</JavaModelGenerator>
<sqlMapGenerator targetPackage=“com.student.modules.database.mapper”targetProject=“.\src\main\resources\mybatis”>
<property name=“enableSubPackages”value=“false”/>
</sqlMapGenerator>
<JavaClientGenerator type=“XMLMAPPER”targetPackage=“com.study.modules.database.dao”targetProject=“.\src\main\resources\mybatis”>
<property name=“enableSubPackages”value=“false”/>
</JavaClientGenerator>
<table tableName=“Auth USER”
EnableCountByExample=“false”
EnableUpdateByExample=“false”
EnableDeleteByExample=“false”
EnableSelectByExample=“false”
SelectByExampleQueryId=“false”
>
<property name=“useActualColumnNames”value=“true”/>
</table>
<table tableName=“testdb”
EnableCountByExample=“false”
EnableUpdateByExample=“false”
EnableDeleteByExample=“false”
EnableSelectByExample=“false”
SelectByExampleQueryId=“false”
>
<property name=“useActualColumnNames”value=“true”/>
</table>
</context>
</generatorConfiguration>
创建另一个测试类
包com.study;
导入org.mybatis。发电机api。MyBatisGenerator;
导入org.mybatis。发电机配置配置
导入org.mybatis。发电机配置XML。configurationParser;
导入org.mybatis。发电机内部的defaultShellCallback;
导入Java。io.文件;
导入Java。util。ArrayList;
导入Java。util。列表
公开课mybatisTest{
公共静态void generator()引发异常{
List<String>warnings=newArrayList<String>();
File configFile=新文件(“src/main/resources/generatorConfig.ml”);
配置分析器cp=新的配置分析器(警告);
配置配置=cp。parseConfiguration(configFile);
Default ShellCallback=new Default Shell回调(true);
MyBatisGenerator MyBatisGenerator=新的MyBatis生成器(配置、回调、警告);
MyBatisGenerator。generate(null);
}
公共静态void main(String[]args){
试试{
生成器();
系统出来println(“生成器已完成”);
}Catch(异常e){
e.PrintStackTrace();
}
}
}
请注意,在XML文件中,记得修改数据库连接地址
运行它并查看效果:
我们将把生成的文件复制到项目中
YML配置数据库连接
弹簧:
数据源:
URL:jdbc:oscar://192.168.1.61:2003/osrdb?servertimezone=utc&usessl=false
用户名:SYSDBA
密码:szoscar55
驱动程序类名:com.oscar.Driver
#用于mybatis
Mybatis:
映射器位置:classpath:com/study