1.Pom.xml添加plugin
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!--生成代码插件-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
</plugin>
2.mybatis-generator-config.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="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库的相关配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password=""/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类生成的位置 -->
<javaModelGenerator targetPackage="com.example.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="mapping" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="com.example.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 相关表的配置 -->
<table tableName="users" domainObjectName="Users" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
3.新建一个Mybatis-Generator启动器
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-G6YC6WPJ-1606727439208)(C:\Users\ThinkPad\AppData\Roaming\Typora\typora-user-images\image-20201130165932212.png)]
完整代码:
Users:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users implements Serializable {
private Integer id;
private String name;
private String password;
}
UsersMapper:
package com.example.dao;
import com.example.bean.Users;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UsersMapper {
int deleteByPrimaryKey(@Param("id") Integer id);
int insert(@Param("record")Users record);
int insertSelective(@Param("record") Users record);
Users selectByPrimaryKey(@Param("id") Integer id);
int updateByPrimaryKeySelective(@Param("record") Users record);
int updateByPrimaryKey(@Param("record") Users record);
}
UsersMapper.xml:
<?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.dao.UsersMapper">
<resultMap id="BaseResultMap" type="com.example.bean.Users">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
</resultMap>
<sql id="Base_Column_List">
id, name, password
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from users
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from users
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.bean.Users">
insert into users (id, name, password
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.example.bean.Users">
insert into users
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="password != null">
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.bean.Users">
update users
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.bean.Users">
update users
set name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
UsersService
public interface UsersService {
Users selectByPrimaryKey(Integer id);
}
UsersServiceImpl
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
UsersMapper usersMapper;
@Override
public Users selectByPrimaryKey(Integer id) {
return this.usersMapper.selectByPrimaryKey(id);
}
}
UsersController
@RestController
public class UsersController {
@Autowired
UsersService usersService;
@RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
public Users get(@PathVariable("id") Integer id){
return usersService.selectByPrimaryKey(id);
}
}
@Autowired
UsersService usersService;
@RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
public Users get(@PathVariable("id") Integer id){
return usersService.selectByPrimaryKey(id);
}
}