今天我们在 Springboot 上集成 Mybatis 。首先创建一个 maven 项目。
添加依赖
<! - Springboot 依赖 -> <dependency> <proupId> org.springframework.boot <proupi> <petifactid> Springbootstarter <Artifactid> </Dependency> <Dedidency> <ProupID> org.springframework.boot </proupActid> <Artifactid> printemps-boot-starter-web </letifactid> </Dependency> <! - 测试 -> <dependency> <proupId> org.springframework.boot </proupId> <ArtefactId> Spring-Boot-starter-test </ artifactid> <ccope> test </cope> </sependency> <! - 集成 Mybatis -> <dependency> <proupId> org.mybatis.spring.boot </prôdId> <ArtefactId> Mybatis-Spring-Boot-starter <proudactid> <version> 1.3.0 </ version> </pedigency> <Dedency> <proupId> MySQL </roulid> <ArtefactId> MySQL-Connector-Java </Retifactid> <DERNÉRATION> 5.1.35 </DERNIERSE> </DENDENCENCE>
在 Src / Main / Resources / 目录下新建一个 Application.Properties 配置文件 , 里面写数据源的配置
printemps.datasource.name = testspring.datasource.url = jdbc: mysql: //127.0.0.1: 3306 / testspring.datasource.username = rootspring.datasource.password = root = root =
配置文件有两种写法 , 我们还可以写成 YML 文件形式 , 在 SRC / Main / Ressources / 目录下新建一个 Application.yMl 文件 ((两种方式选其中一种就可以了)))))
Spring: DataSource: URL: JDBC: MySQL: //127.0.0.1: 3306 / TEST Nom d'utilisateur: Root Motword: Root Driver-Class-Name: com.mysql.cj.jdbc.driver
在本地数据库中创建 Test 数据库 , 并新建一个表 T_User
Créer un test de base de données; Créer un tableau `t_user` (` user_id` int (11) pas null auto_increment, `user_name` varchar (225) pas null,` mot de passe` varchar (225) pas nul
这里我们使用 Mybatis-Generator 插件 , 自动生成实体类 , Mappeur , 以及 Mappeur.xml 文件
在 pom 中添加 construire 依赖 (放在层次外面 放在层次外面
<fuild> <Sessiers> <Resource> <Arnutory> SRC / Main / Resources </ Directory> <filtring> true </filter> <cluscludes> <cord> générateur / ** </cold> </cluses> </ ressource> </ ressources> <flugins> <plugin> <groupId> org.mybatis.generator </prôdId> <ArtefactId> Mybatis-Generator-Maven-Plugin </ Artifactid> <Version> 1.3.4-SNAPSHOT </ Version> <Detendances> <Dedency> <ProupId> MysQL </rom groupement> <ArtefactId> MySQL-Connector-Java </ Artifactid> <DersonD> 5.1.39 </DERNIFRIMINATION> </ Dependency> <Dedency> <ProupId> Org.Mybatis.Generator </ GroupId> <Artefactive> Mybatis-Generator-core </distifactid> </nulture> 1.3.4-snapshot </De version> </ Dépendard> <frision> 1.3.4-SNAPSHOT </De version>> </ Dépendard> <Dusined> 1.3.4-SNAPSHOT </Dudition> </ Dépendard> <frision> 1.3.4-SNAPSHOT </ Version> </ Dépendard> <frision> 1.3.4-SNAPSHOT </DERNÉRENCE> <Cverwrite> true </verswrite> <FIGIGURAGEFILE> SRC / Main / Resources / Generator / GeneratorConfig.xml </ ConfigurationFile> </ Configuration> </ Plugin> </ Plugins> </ Build>
在 SRC / Main / Resources / 下面新建一个目录 Générateur , 在此目录下新建一个 GénératorConfig.xml 文件 (注意根据自己的目录 , 调整路径))
<? xml version = "1.0" Encoding = "UTF-8"?> <! Doctype GeneratorConfiguration public "- // Mybatis.org//dtd Mybatis Generator Configuration 1.0 // en" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <GeneratorConcure> < ConnectionUrl = "JDBC: mysql: //127.0.0.1: 3306 / test" userId = "root" password = "root"> </ jdbcconnection> <javatypereSolver> <propriété name = "ForceBigDecimal TargetPackage = "com.lw.study.dao.domain" TargetProject = "src / main / java"> <propriété name = "ActiveBpackages" value = "true" /> <propriété name = "TrimStrings" value = "true" /> </ javamodelGenerator> <! - 生成映射文件存放位置 -> <sqlmapGenerator TargetPackage = "SQLMAP TargetProject = "Src / Main / Resources /"> <propriété name = "ActiveBpackages" value = "true" /> </ sqlmapGenerator> <! - 生成 Dao 类存放位置 -> <! - 客户端代码 , 生成易于使用的针对 Modèle 对象和 xml 配置文件 的代码 的代码 type = "AnnotatedMapper", 生成 Java Model 和基于注解的 MAPPER 对象 Type = "mixte type = "xmlmapper", 生成 sqlmap xml 文件和独立的 mappeur 接口 -> <javaclientGenerator type = "xmlmapper" ciblePackage = "com.lw.study.dao.mapper" cibleProject = "src / main / java"> < <! - 生成对应表及类名 -> <! - <Table Schema = "LOANDB" TableName = "T_AUDIT_JOB" DomainObjectName = "AuditJob"> -> <! - <Propriété Name = "UseActualColumnNames" Value = "True" /> -> <! - <GeneratedKey Column = "id" sqlstatement = "MysQL" <! - </ table> -> <table tablename = "t_user" domainObjectName = "user" apliableCountByExample = "false" activeUpDateByExample = "false" ENTIPELETEEBYEXample = "false" actidElectByExample = "false" selectByExampleQueId = "false"> </pall
然后运行 Générateur 配置文件。
在点击左上角的➕ , 选择 Maven
Travail dérectory 选择自己的项目路径。然后 Appliquer , 在右上角运行就可以了 , 就会在指定的目录下生成三个文件 (实体类 , , , , , , , , , 类 , 和 Mappeur.xml 文件))
最后在 src / main / java / com / lw / étude / 目录下新建一个 mybatisconfig 目录 , 里面新建两个配置类。
Mybatisconfig.java
@ Configuration @ itreAtransActionManagementPublic class mybatisconfig {@bean @configurationproperties (prefix = "spring.datasource") public dataSource dataSource () {return new org.apache.tomcat.jdbc.pool.datasource (); } @Bean (name = "sqlSessionFactory") public sqlSessionFactory sqlSessionFactoryBean () lève une exception {sqlSessionFactoryBean SqlSessionFactoryBean = new sqlSessionFactoryBean (); SQLSessionFactoryBean.SetDataSource (DataSource ()); PathMatchingResourcePatterNResolver Resolver = new PathMatchingResourcePatterNResolver (); sqlSessionFactoryBean.SetMapperLocations (résolver.getResources ("classpath: / sqlmap / *. xml")); return sqlSessionFactoryBean.getObject (); }}MybatisperscannerConfig.java
@ Configuration @ AutoConfiGurefter (mybatisconfig.class) classe publique MyBatisPerscannerConfig {@Bean public MapperscannerConfigurer MAPPERScannerConfigurer () {MAPPERSCANNERCONFIGER MAPPERSCANNERCONFIGURER = New MapperscannerConfiger (); MAPPERSCANNERCONFIGURER.SETSQLSESSESSEFACTORYBEAnNAME ("SqlSessionFactory"); MAPPERSCANNERCONFIGURER.SETBASEPACKAGE ("com.study.dao.mapper"); retour MAPPERScannerConfigurer; }}创建一个 Springboot 的启动类 , application.java
@SpringbootApplication (ScanBasEpackages = {"com.lw.study. *"}) @ Mapperscan ("com.lw.study.mapper") Application de classe publique {public static void main (string [] args) {springApplication.run (application.class, args); }}好了 , 到这里我们在 Springboot 中通过 Générateur 插件的方式集成 Mybatis 就完成了。大家可以自己写一个测试类 , 使用 Mappeur 中的方法看能否在数据库中查到数据。
总结
以上所述是小编给大家介绍的创建 Springboot 工程并集成 Mybatis 的方法 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!