Springboot集成MyBatis逆向工程

Springboot集成MyBatis逆向工程

本次主要说明mybatis逆向工程的创建以及集成springboot

逆向概念

  • 以前做开发的时候,mapper文件和实体类都是手写,效率较低且没有什么意义,通过mybatis逆向工程可以一键创建所有需要的实体类和mapper文件。

步骤

新建一个springboot工程

  • 打开project Structure,new一个module,选择SpringInitializr,点击next(这里需要联网才能创建)
  • 输入自己的group和java版本号
  • 选择web后点击springweb,点击next后点击finish后apply完成创建

数据库建表

  • sql语句如下:
(
   id                   int(10)                        not null auto_increment,
   name                 varchar(20)                    null,
   age                  int(10)                        null,
   constraint PK_T_STUDENT primary key clustered (id)
);
insert into t_student(name,age) values("zhangsan",25);
insert into t_student(name,age) values("lisi",28);
insert into t_student(name,age) values("wangwu",23);
insert into t_student(name,age) values("Tom",21);
insert into t_student(name,age) values("Jck",55);
insert into t_student(name,age) values("Lucy",27);
insert into t_student(name,age) values("zhaoliu",75);

加入插件和依赖

  • 在pom文件加入mybatis和mysql依赖和逆向工程的插件
<dependencies>
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>2.0.0</version>
	</dependency>

	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.32</version>
	</dependency>
</dependencies>
<build>
<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>
</build>

加入逆向配置文件

  • 在项目根目录下新建GeneratorMapper.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">
<generatorConfiguration>
    <!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="C:\Java\maven\maven_repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar"/>

    <!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
    <context id="tables" targetRuntime="MyBatis3">
        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                        connectionURL=""
                        userId=""
                        password="">
        </jdbcConnection>
        <!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
       生成的 model 放在 eclipse 的哪个工程下面-->
        <javaModelGenerator targetPackage="cn.lstf666.springboot.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>
        <!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
       包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
        <sqlMapGenerator targetPackage="cn.lstf666.springboot.mapper"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
       名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="cn.lstf666.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 数据库表名及对应的 Java 模型类名 -->
        <table tableName="t_student" domainObjectName="Student"
           enableCountByExample="false"
           enableUpdateByExample="false"
           enableDeleteByExample="false"
            enableSelectByExample="false"
            selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

运行插件

  • 点击右边的maven工具栏,点击当前项目,选择plugins,下面可以看到mybatis-generator,点开之后,双击下面的mybatis-generator:generator

创建完成


  • 可以看到所有的实体类和mapper已经创建完成

测试

  1. 分别创建Controller、Service类
    • Controller
@RestController
	public class indexController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("/getS")
    public Student getStudent(Integer id){
        Student student = studentService.getStudentById(id);
        return student;
    }
}
  • Service
	public interface StudentService {
    Student getStudentById(Integer id);
}
	@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student getStudentById(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);

        return student;
    }
}
* mapper上加上注解
@Mapper
public interface StudentMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Student record);

    int insertSelective(Student record);

    Student selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Student record);

    int updateByPrimaryKey(Student record);
}
* 启动类加上mapperScan注解
@SpringBootApplication
@MapperScan(basePackages = "cn.lstf666.mapper")
public class MybatisdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(MybatisdemoApplication.class, args);
	}
}
  1. 配置数据库连接信息

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:
    spring.datasource.username=
    spring.datasource.password=
    
  2. 配置xml的映射位置

    • 因为框架规定了mapper与xml必须在同级目录下,而java包下只有.java文件可以被编译到targets目录下,所以需要手动指定xml的映射位置。
    • 打开pom文件,build标签下加入以下代码
    <resources>
    	<resource>
    		<directory>src/main/java</directory>
    		<includes>
    			<include>**/*.xml</include>
    		</includes>
    	</resource>
    </resources>
    
  3. 启动

    • 运行主程序,点击左边的绿色启动符号
    • 打开浏览器地址栏,输入:http://localhost:8080/getS?id=1
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值