介绍
本文从零开始讲解SpringBoot整合MyBatis逆向工程及MyBatis通用Mapper的详细步骤和示例代码。MyBatis是一款优秀的ORM框架,通过逆向工程可以将关系型数据库的表结构生成对应的Java Bean,以及相关的Mapper和XML映射文件,以减少开发量。而MyBatis通用Mapper可以进一步提高开发效率,省去了大量的Mapper编写工作。SpringBoot则提供了一种快速开发、方便集成的利器,通过SpringBoot整合MyBatis逆向工程和MyBatis通用Mapper,可以快速完成项目的开发。
环境准备
- JDK1.8
- Maven3.0以上
- IntelliJ IDEA或Eclipse
- SpringBoot
- MyBatis逆向工程插件generator
- MyBatis通用Mapper插件mapper-helper
MyBatis逆向工程
配置逆向工程插件
pom.xml文件中增加MyBatis Generator插件(这里使用的是MySQL数据库):
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
配置generator.xml
在src/main/resources/generator目录下创建generatorConfig.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>
<context id="MySql" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.example.demo.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
该文件的作用是配置逆向工程的相关信息,包括连接数据库的信息、生成代码的信息、生成的表和实体类等。
生成代码
在IDEA或Eclipse中打开终端,输入以下命令即可生成代码:
mvn mybatis-generator:generate
生成的代码在src/main/java和src/main/resources中。
MyBatis通用Mapper
配置通用Mapper插件
pom.xml文件中增加mapper-helper插件(这里使用的是MySQL数据库):
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.github.abel533</groupId>
<artifactId>mapper-helper-maven-plugin</artifactId>
<version>3.0.5</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<configFile>src/main/resources/generator/generatorConfig.xml</configFile>
<type>MYSQL</type>
<extend>true</extend>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置MyBatis
在src/main/resources目录下创建mybatis-config.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.example.demo.model" />
</typeAliases>
<mappers>
<mapper resource="mapper/UserMapper.xml" />
</mappers>
<plugins>
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<property name="IDENTITY" value="MYSQL"/>
</plugin>
</plugins>
</configuration>
该文件的作用是将实体类别名设置为包路径,将Mapper映射文件加入到配置文件中,并配置通用Mapper插件。
配置Mapper
在src/main/resources/mapper目录下创建UserMapper.xml文件,存放SQL语句:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.UserMapper">
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.example.demo.model.User">
insert into user (name, age) values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER});
</insert>
<update id="updateByPrimaryKey" parameterType="com.example.demo.model.User">
update user
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user where id = #{id,jdbcType=INTEGER}
</delete>
</mapper>
使用通用Mapper
在src/main/java/com/example/demo/service/impl/UserServiceImpl.java中,注入UserMapper:
@Service
public class UserServiceImpl implements UserService {
//注入Mapper
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(int id) {
return this.userMapper.selectByPrimaryKey(id);
}
@Override
public int saveUser(User user) {
return this.userMapper.insert(user);
}
@Override
public int updateUser(User user) {
return this.userMapper.updateByPrimaryKey(user);
}
@Override
public int deleteUser(int id) {
return this.userMapper.deleteByPrimaryKey(id);
}
}
完成以上步骤后,即完成了整合MyBatis逆向工程和MyBatis通用Mapper的工作。
示例
这里提供两个示例:
查询用户
访问地址:http://localhost:8080/user/1
UserController.java:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/{id}")
public User getUserById(@PathVariable("id") int id) {
return this.userService.getUserById(id);
}
}
添加用户
访问地址:http://localhost:8080/user
UserController.java:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public int saveUser(@RequestBody User user) {
return this.userService.saveUser(user);
}
}
User.java:
public class User {
private Integer id;
private String name;
private Integer age;
// getter、setter方法省略
}
以上即为完成了SpringBoot整合MyBatis逆向工程及MyBatis通用Mapper的完整攻略,并提供了两个示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解 - Python技术站