Springboot+MyBatis+JPA集成

 

 

1.前言

Springboot最近可谓是非常的火,本人也在项目中尝到了甜头。之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Springboot我决定尝试一下Springboot+MyBatis+JPA三项集成,集成过程中遇到了很多问题,但最后总算是集成成功了,现在记录一下方法。

1.1 如何使用MyBatis Generator自动生成xxxMapper.java接口以及xxxMapper.xml文件

以前用过SpringMVC,知道写xxxMapper.java接口以及xxxMapper.xml文件的辛苦,这次集成最先想到的就是先解决如何使用如何使用MyBatis Generator自动生成这些文件的问题。

先扔出MyBatis Generator的官网->请戳这里

我使用的Maven集成插件的方式,IDE使用的是IDEA

1.1.1 创建项目
 
20180126110629.png
 
20180126110757.png
 
20180126110812.png
1.1.2 修改pom.xml

添加了Druid依赖和MyBatis Generator插件

其他依赖请自行添加

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study.springboot</groupId> <artifactId>mybatis</artifactId> <version>1.0</version> <packaging>war</packaging> <name>demo</name> <description>springboot+mybatis+jpa</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!--<scope>provided</scope>--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 阿里巴巴连接池Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- MyBatis Generator 插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 
1.1.3 配置generatorConfiguration

generatorConfig.xml放在resource文件夹下

常用配置已经注释了,请自行查看

<?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"> <generatorConfiguration> <!-- 可以用于加载配置项或者配置文件,在整个配置文件中就可以使用${propertyKey}的方式来引用配置项 resource:配置资源加载地址,使用resource,MBG从classpath开始找,比如com/myproject/generatorConfig.properties url:配置资源加载地址,使用URL的方式,比如file:///C:/myfolder/generatorConfig.properties 注意,两个属性只能选址一个 另外,如果使用了mybatis-generator-maven-plugin,那么在pom.xml中定义的properties都可以直接在generatorConfig.xml中使用 <properties resource="" url="" /> --> <!--<properties resource="db.properties"/>--> <!-- 在MBG工作的时候,需要额外加载的依赖包 location属性指明加载jar/zip包的全路径 --> <classPathEntry location="C:\Users\Semi\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/> <!-- context:生成一组对象的环境 id:必选,上下文id,用于在生成错误时提示 defaultModelType:指定生成对象的样式 1. conditional:类似hierarchical 2. flat:所有内容(主键,blob)等全部生成在一个对象中 3. hierarchical:主键生成一个XXKey对象(key class),Blob等单独生成一个对象,其他简单属性在一个对象中(record class) targetRuntime: 1. MyBatis3:默认的值,生成基于MyBatis3.x以上版本的内容,包括XXXBySample 2. MyBatis3Simple:类似MyBatis3,只是不生成XXXBySample introspectedColumnImpl:类全限定名,用于扩展MBG --> <context id="MyBatis" targetRuntime="MyBatis3"> <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表 一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 --> <property name="autoDelimitKeywords" value="false"/> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化Java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <!-- 不生成注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- JDBC连接配置 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123456"/> <!-- Java类型处理器 用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和NUMERIC数据类型 --> <javaTypeResolver> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认 scale>0;length>18:使用BigDecimal scale=0;length[10,18]:使用Long scale=0;length[5,9]:使用Integer scale=0;length<5:使用Short --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Java模型创建器,是必须要的元素 负责: 1. key类(见context的defaultModelType) 2. java类 3. 查询类 targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制 targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录 --> <javaModelGenerator targetPackage="com.study.springboot.mybatis.model" targetProject="E:\WorkSpace\study\springboot\mybatis\src\main\java"> <!-- 自动为每一个生成的类创建一个构造方法,构造方法包含了所有的field,而不是使用setter --> <!--<property name="constructorBased" value="false"/>--> <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false --> <property name="enableSubPackages" value="false"/> <!-- 是否创建一个不可变的类,如果为true 那么MBG会创建一个没有setter方法的类,取而代之的是类似constructorBased的类 --> <!--<property name="immutable" value="false"/>--> <!-- 设置一个根对象 如果设置了这个根对象,那么生成的keyClass或者recordClass会继承这个类,在Table的rootClass属性中可以覆盖该选项 注意: 如果在key class或者record class中有root class相同的属性,MBG就不会重新生成这些属性了,包括: 1. 属性名相同,类型相同,有相同的getter/setter方法 --> <!--<property name="rootClass" value="com._520it.mybatis.domain.BaseDomain"/>--> <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成SQL map的XML文件生成器 注意,在Mybatis3之后,我们可以使用mapper.xml文件+Mapper接口(或者不用mapper接口) 或者只使用Mapper接口+Annotation,所以,如果javaClientGenerator配置中配置了需要生成XML的话,这个元素就必须配置 targetPackage/targetProject:同javaModelGenerator --> <sqlMapGenerator targetPackage="mapper" targetProject="E:\WorkSpace\study\springboot\mybatis\src\main\resources"> <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 对于mybatis来说,即生成Mapper接口 注意,如果没有配置该元素,那么默认不会生成Mapper接口 targetPackage/targetProject:同javaModelGenerator type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下) 1. ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中)不会生成对应的XML 2. MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中 3. XMLMAPPER:会生成Mapper接口,接口完全依赖XML 注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.study.springboot.mybatis.mapper" targetProject="E:\WorkSpace\study\springboot\mybatis\src\main\java"> <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false --> <property name="enableSubPackages" value="false"/> <!-- 可以为所有生成的接口添加一个父接口,但是MBG只负责生成,不负责检查 <property name="rootInterface" value=""/> --> </javaClientGenerator> <!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素 选择的table会生成一下文件: 1. SQL map文件 2. 生成一个主键类 3. 除了BLOB和主键的其他字段的类 4. 包含BLOB的类 5. 一个用户生成动态查询的条件类(selectByExample, deleteByExample)可选 6. Mapper接口(可选) tableName(必要):要生成对象的表名,%表示通配,即为该数据库下的所有表生成xml文件 注意:大小写敏感问题.正常情况下,MBG会自动的去识别数据库标识符的大小写敏感度,在一般情况下 MBG会根据设置的schema,catalog或tablename去查询数据表,按照下面的流程: 1. 如果schema,catalog或tablename中有空格,那么设置的是什么格式,就精确的使用指定的大小写格式去查询 2. 否则,如果数据库的标识符使用大写的,那么MBG自动把表名变成大写再查找 3. 否则,如果数据库的标识符使用小写的,那么MBG自动把表名变成小写再查找 4. 否则,使用指定的大小写格式查询 另外,如果在创建表的时候,使用的""把数据库对象规定大小写,就算数据库标识符是使用的大写,在这种情况下也会使用给定的大小写来创建表名 这个时候,请设置delimitIdentifiers="true"即可保留大小写格式 可选: 1. schema:数据库的schema 2. catalog:数据库的catalog 3. alias:为数据表设置的别名,如果设置了alias,那么生成的所有的SELECT SQL语句中,列名会变成:alias_actualColumnName 4. domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面 5. enableInsert(默认true):指定是否生成insert语句 6. enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get) 7. enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句 8. enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update) 9. enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete) 10. enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句 11. enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询) 12. enableUpdateByExample(默认true)MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性) 13. modelType:参考context元素的defaultModelType,相当于覆盖 14. delimitIdentifiers:参考tableName的解释,注意,默认的delimitIdentifiers是双引号,如果类似MYSQL这样的数据库,使用的是`(反引号,那么还需要设置context的beginningDelimiter和endingDelimiter属性) 15. delimitAllColumns:设置是否所有生成的SQL中的列名都使用标识符引起来.默认为false,delimitIdentifiers参考context的属性 注意,table里面很多参数都是对javaModelGenerator,context等元素的默认属性的一个复写 --> <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!-- 参考javaModelGenerator的constructorBased属性 --> <!--<property name="constructorBased" value="false"/>--> <!-- 默认为false,如果设置为true,在生成的SQL中,table名字不会加上catalog或schema --> <!--<property name="ignoreQualifiersAtRuntime" value="false"/>--> <!-- 参考javaModelGenerator的immutable属性 --> <!--<property name="immutable" value="false"/>--> <!-- 指定是否只生成domain类,如果设置为true,只生成domain类,如果还配置了sqlMapGenerator,那么在mapper XML文件中,只生成resultMap元素 --> <!--<property name="modelOnly" value="false"/>--> <!-- 参考javaModelGenerator的rootClass属性 --> <!--<property name="rootClass" value=""/>--> <!-- 参考javaClientGenerator的rootInterface属性 --> <!--<property name="rootInterface" value=""/>--> <!-- 如果设置了runtimeCatalog,那么在生成的SQL中,使用该指定的catalog,而不是table元素上的catalog --> <!--<property name="runtimeCatalog" value=""/>--> <!-- 如果设置了runtimeSchema,那么在生成的SQL中,使用该指定的schema,而不是table元素上的schema --> <!--<property name="runtimeSchema" value=""/>--> <!-- 如果设置了runtimeTableName,那么在生成的SQL中,使用该指定的tablename,而不是table元素上的tablename --> <!--<property name="runtimeTableName" value=""/>--> <!-- 注意,该属性只针对MyBatis3Simple有用 如果选择的runtime是MyBatis3Simple,那么会生成一个SelectAll方法,如果指定了selectAllOrderByClause,那么会在该SQL中添加指定的这个order条件 --> <!--<property name="selectAllOrderByClause" value="age desc,username asc"/>--> <!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate --> <!--<property name="useActualColumnNames" value="false"/>--> <!-- generatedKey用于生成生成主键的方法 如果设置了该元素,MBG会在生成的<insert>元素中生成一条正确的<selectKey>元素,该元素可选 column:主键的列名 sqlStatement:要生成的selectKey语句,有以下可选项: Cloudscape:相当于selectKey的SQL为:VALUES IDENTITY_VAL_LOCAL() DB2 :相当于selectKey的SQL为:VALUES IDENTITY_VAL_LOCAL() DB2_MF :相当于selectKey的SQL为:SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1 Derby :相当于selectKey的SQL为:VALUES IDENTITY_VAL_LOCAL() HSQLDB :相当于selectKey的SQL为:CALL IDENTITY() Informix :相当于selectKey的SQL为:select dbinfo('sqlca.sqlerrd1') from systables where tabid=1 MySql :相当于selectKey的SQL为:SELECT LAST_INSERT_ID() SqlServer :相当于selectKey的SQL为:SELECT SCOPE_IDENTITY() SYBASE :相当于selectKey的SQL为:SELECT @@IDENTITY JDBC :相当于在生成的insert元素上添加useGeneratedKeys="true"和keyProperty属性 --> <!--<generatedKey column="" sqlStatement=""/>--> <!-- 该元素会在根据表中列名计算对象属性名之前先重命名列名,非常适合用于表中的列都有公用的前缀字符串的时候 比如列名为:CUST_ID,CUST_NAME,CUST_EMAIL,CUST_ADDRESS等 那么就可以设置searchString为"^CUST_",并使用空白替换,那么生成的Customer对象中的属性名称就不是 custId,custName等,而是先被替换为ID,NAME,EMAIL,然后变成属性:id,name,email 注意,MBG是使用java.util.regex.Matcher.replaceAll来替换searchString和replaceString的 如果使用了columnOverride元素,该属性无效 --> <!--<columnRenamingRule searchString="" replaceString=""/>--> <!-- 用来修改表中某个列的属性,MBG会使用修改后的列来生成domain的属性 column:要重新设置的列名 注意,一个table元素中可以有多个columnOverride元素 --> <!--<columnOverride column="username">--> <!-- 使用property属性来指定列要生成的属性名称 --> <!--<property name="property" value="userName"/>--> <!-- javaType用于指定生成的domain的属性类型,使用类型的全限定名 --> <!--<property name="javaType" value=""/>--> <!-- jdbcType用于指定该列的JDBC类型 --> <!--<property name="jdbcType" value=""/>--> <!-- typeHandler:用于指定该列使用到的TypeHandler,如果要指定,配置类型处理器的全限定名 注意,mybatis中,不会生成到mybatis-config.xml中的typeHandler 只会生成类似:where id = #{id,jdbcType=BIGINT,typeHandler=com._520it.mybatis.MyTypeHandler}的参数描述 --> <!--<property name="jdbcType" value=""/>--> <!-- 参考table元素的delimitAllColumns配置,默认为false --> <!--<property name="delimitedColumnName" value=""/>--> <!-- ignoreColumn设置一个MGB忽略的列,如果设置了改列,那么在生成的domain中,生成的SQL中,都不会有该列出现 column:指定要忽略的列的名字 delimitedColumnName:参考table元素的delimitAllColumns配置,默认为false 注意,一个table元素中可以有多个ignoreColumn元素 --> <!--<ignoreColumn column="deptId" delimitedColumnName=""/>--> </table> </context> </generatorConfiguration> 
1.1.4 生成xxxMapper.java接口以及xxxMapper.xml文件
 
20180126111603.png

注意,如果存放xxxMapper.java和xxxMapper.xml文件的包不存在会报错

1.1.5 修改自动生成的实体类

对于model包下的实体类需要添加基于JPA的注解,否则JPA接口无法引用

例如对于User.java实体类需要进行如下修改


package com.study.springboot.mybatis.model;

import javax.persistence.*;

@Entity
@Table(name = "user") public class User { @Id @GeneratedValue @Column(name = "id") private Integer id; @Column(name = "user_name") private String userName; @Column(name = "passwd") private String passwd; @Column(name = "nick_name") private String nickName; @Column(name = "telephone") private String telephone; @Column(name = "email") private String email; @Column(name = "authority") private Integer authority; @Column(name = "department") private String department; @Column(name = "create_at") private String createAt; @Column(name = "update_at") private String updateAt; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd == null ? null : passwd.trim(); } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName == null ? null : nickName.trim(); } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone == null ? null : telephone.trim(); } public String getEmail() { return email; } public void setEmail(String email) { this.email = email == null ? null : email.trim(); } public Integer getAuthority() { return authority; } public void setAuthority(Integer authority) { this.authority = authority; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department == null ? null : department.trim(); } public String getCreateAt() { return createAt; } public void setCreateAt(String createAt) { this.createAt = createAt == null ? null : createAt.trim(); } public String getUpdateAt() { return updateAt; } public void setUpdateAt(String updateAt) { this.updateAt = updateAt == null ? null : updateAt.trim(); } } 
1.1.6 添加xxxJPA接口,例如UserJPA.java

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.io.Serializable;

public interface UserJPA extends Serializable, JpaRepository<User, Integer>, JpaSpecificationExecutor<User> { } 
1.1.7 测试

测试之前需要配置application.yml(你的有能是application.properties,改一下后缀名即可,我比较喜欢.yml文件的简洁)

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
mybatis:
  # 重要配置
  type-aliases-package: com.study.springboot.mybatis.model mapper-locations: classpath:mapper/*.xml 

对于每一个xxxMapper.java接口,需要使用@Component注解

自动生成的xxxMapper.java接口是不带@Component注解的

@Component
public interface UserMapper { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); } 

在入口类中需要添加@MapperScan("com.study.springboot.mybatis.mapper")注解

@SpringBootApplication
@MapperScan("com.study.springboot.mybatis.mapper")
public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } } 

打开测试类,为测试类添加@MapperScan("com.study.springboot.mybatis.mapper")注解

@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan("com.study.springboot.mybatis.mapper") public class MybatisApplicationTests { @Autowired private UserMapper userMapper; @Autowired private UserJPA userJPA; @Test public void contextLoads() { System.out.println(userMapper.selectByPrimaryKey(11).getUserName()); System.out.println(userJPA.findOne(11).getUserName()); } } 
 
20180126110153.png

最后如果出现

java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSession

的错误,请检查本地Maven仓库的配置,有可能是没有Maven没有下载需要引用的包

转载于:https://www.cnblogs.com/yueguanguanyun/p/9258288.html

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建Spring Boot MyBatis项目的步骤如下: 1. 首先,确保你的开发环境中已经安装了Java JDK和Maven。 2. 打开IDE(集成开发环境),如IntelliJ IDEA或Eclipse,在IDE中选择“新建项目”。选择“Spring Initializer”或“Spring Boot”项目类型。 3. 在项目设置中,你需要选择Spring Boot版本,并设置项目的基本信息,如项目名称、包名等。 4. 在依赖项中,选择添加MyBatis和相关的数据库驱动依赖项。你可能还需要添加其他依赖项,如Web、JPA等,根据你的实际需求选择添加。 5. 完成项目设置后,点击“完成”按钮,IDE会自动为你创建一个基本的Spring Boot项目结构。 6. 接下来,在项目结构中创建数据库配置文件,如application.properties或application.yml。配置数据库连接信息,如数据库URL、用户名、密码等。 7. 创建数据库表,并编写对应的实体类。 8. 创建MyBatis的Mapper接口和Mapper XML文件,定义对数据库表的CRUD操作。 9. 在Spring Boot的启动类中添加注解@SpringBootApplication,以启用Spring Boot和自动配置。 10. 编写控制器类和服务类,处理用户请求和业务逻辑。 11. 使用@Service注解将服务类注入Spring容器。 12. 使用@Autowired注解将服务类注入到控制器类中。 13. 运行项目,在浏览器中输入URL进行测试。 以上就是创建Spring Boot MyBatis项目的基本步骤。根据你的实际需求,你还可以添加其他功能和模块,如安全认证、缓存、日志等。祝你项目顺利!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值