SpringBoot集成Mybatis

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

SpringBoot集成Mybatis

1、新建springboot工程

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

2、添加mysql依赖和mybatis依赖

在<dependenci>标签下添加依赖

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>

在<build>标签下添加

<resources>
	<resource>
		<directory>src/main/java</directory>
		<includes>
		<include>**/*.xml</include>
		</includes>
	</resource>
</resources>

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

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

3.3、添加配置信息
<?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>
    <!--指定连接数据库的JDBC 驱动包所在位置,指定到你本机的完整路径-->
    <classPathEntry location="D:\JDBCjarPackage\mysql-connector-java-8.0.23.jar"/>
    <!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本-->
    <context id="tables" targetRuntime="MyBatis3">
        <!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--配置数据库连接信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/whynode" userId="root" password="123">
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>
        <!--生成实体类,targetPackage 指定实体类的包名,targetProject 指定 生成的实体类哪个工程下面-->
        <javaModelGenerator targetPackage="com.why.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>
        <!--生成 MyBatis的Mapper.xml文件,targetPackage 指定 mapper.xml文件的包名,targetProject指定生成的mapper.xml放在哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.why.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成 MyBatis的 Mapper接口类文件,targetPackage 指定 Mapper 接口类的包名,targetProject 指定生成的Mapper接口放哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.why.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--数据库表名及对应的Java模型类名-->
        <!--一张表对应一个table-->
        <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>
3.4、添加插件
<plugin>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.3.6</version>
	<configuration>
		<configurationFile>GeneratorMapper.xml</configurationFile>
		<verbose>true</verbose>
		<overwrite>true</overwrite>
	</configuration>
</plugin>

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

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

接下来是测试:

编写控制器类

@Controller
public class MyController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/student")
    public @ResponseBody Object student(Integer id){
        Student student = studentService.queryById(id);
        return student;
    }
}

StudentService接口

public interface StudentService {

    Student queryById(Integer id);

}

接口实现类

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Override
    public Student queryById(Integer id) {

        return studentMapper.selectByPrimaryKey(id);
    }
}

springBoot核心配置文件

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

启动项目测试

相关文章