SpringBoot集成Mybatis

x33g5p2x  于2021-11-22 转载在 Spring  
字(4.4k)|赞(0)|评价(0)|浏览(688)

SpringBoot集成Mybatis

1、新建springboot工程

详情见之前发布的关于springboot集成jsp的文章1-4步
点击这里查看

2、添加mysql依赖和mybatis依赖

在<dependenci>标签下添加依赖

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.mybatis.spring.boot</groupId>
  7. <artifactId>mybatis-spring-boot-starter</artifactId>
  8. <version>2.0.0</version>
  9. </dependency>

在<build>标签下添加

  1. <resources>
  2. <resource>
  3. <directory>src/main/java</directory>
  4. <includes>
  5. <include>**/*.xml</include>
  6. </includes>
  7. </resource>
  8. </resources>

3、使用Mybatis提供的逆向工程生成实体类

3.1、在使用逆向工程前先准备一张学生表
  1. CREATE TABLE `t_student` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(32) NOT NULL,
  4. `sex` varchar(32) DEFAULT NULL,
  5. `age` int(11) NOT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8
3.2在项目的根目录下创建GeneratorMapper.xml文件

3.3、添加配置信息
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <generatorConfiguration>
  4. <!--指定连接数据库的JDBC 驱动包所在位置,指定到你本机的完整路径-->
  5. <classPathEntry location="D:\JDBCjarPackage\mysql-connector-java-8.0.23.jar"/>
  6. <!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本-->
  7. <context id="tables" targetRuntime="MyBatis3">
  8. <!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成-->
  9. <commentGenerator>
  10. <property name="suppressAllComments" value="true" />
  11. </commentGenerator>
  12. <!--配置数据库连接信息-->
  13. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/whynode" userId="root" password="123">
  14. <property name="nullCatalogMeansCurrent" value="true"/>
  15. </jdbcConnection>
  16. <!--生成实体类,targetPackage 指定实体类的包名,targetProject 指定 生成的实体类哪个工程下面-->
  17. <javaModelGenerator targetPackage="com.why.domain" targetProject="src/main/java">
  18. <property name="enableSubPackages" value="false"/>
  19. <property name="trimStrings" value="false"/>
  20. </javaModelGenerator>
  21. <!--生成 MyBatis的Mapper.xml文件,targetPackage 指定 mapper.xml文件的包名,targetProject指定生成的mapper.xml放在哪个工程下面 -->
  22. <sqlMapGenerator targetPackage="com.why.dao" targetProject="src/main/java">
  23. <property name="enableSubPackages" value="false"/>
  24. </sqlMapGenerator>
  25. <!--生成 MyBatis的 Mapper接口类文件,targetPackage 指定 Mapper 接口类的包名,targetProject 指定生成的Mapper接口放哪个工程下面 -->
  26. <javaClientGenerator type="XMLMAPPER" targetPackage="com.why.dao" targetProject="src/main/java">
  27. <property name="enableSubPackages" value="false"/>
  28. </javaClientGenerator>
  29. <!--数据库表名及对应的Java模型类名-->
  30. <!--一张表对应一个table-->
  31. <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  32. </context>
  33. </generatorConfiguration>
3.4、添加插件
  1. <plugin>
  2. <groupId>org.mybatis.generator</groupId>
  3. <artifactId>mybatis-generator-maven-plugin</artifactId>
  4. <version>1.3.6</version>
  5. <configuration>
  6. <configurationFile>GeneratorMapper.xml</configurationFile>
  7. <verbose>true</verbose>
  8. <overwrite>true</overwrite>
  9. </configuration>
  10. </plugin>

4、双击maven工程中plugins中mybatis-generator下的generate

双击后就自动生成了实体类,dao接口和mapper映射文件了

接下来是测试:

编写控制器类

  1. @Controller
  2. public class MyController {
  3. @Autowired
  4. private StudentService studentService;
  5. @RequestMapping("/student")
  6. public @ResponseBody Object student(Integer id){
  7. Student student = studentService.queryById(id);
  8. return student;
  9. }
  10. }

StudentService接口

  1. public interface StudentService {
  2. Student queryById(Integer id);
  3. }

接口实现类

  1. @Service
  2. public class StudentServiceImpl implements StudentService {
  3. @Resource
  4. private StudentMapper studentMapper;
  5. @Override
  6. public Student queryById(Integer id) {
  7. return studentMapper.selectByPrimaryKey(id);
  8. }
  9. }

springBoot核心配置文件

  1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://localhost:3306/whynode
  3. spring.datasource.username=root
  4. spring.datasource.password=123

启动项目测试

相关文章