Java生成MyBatis Mapper教程
一、整体流程
以下是生成MyBatis Mapper的整体流程:
步骤 | 操作 |
---|---|
1 | 创建Java实体类 |
2 | 创建MyBatis Mapper接口 |
3 | 创建Mapper XML文件 |
4 | 配置MyBatis配置文件 |
5 | 生成Mapper接口的实现类 |
在下面的教程中,将逐步指导你完成每一步骤的操作。
二、操作步骤
1. 创建Java实体类
首先,我们需要创建一个Java类来定义我们的实体。例如,我们创建一个名为User
的类。
public class User {
private int id;
private String name;
// 省略其他属性和方法
}
2. 创建MyBatis Mapper接口
接下来,我们需要创建一个MyBatis Mapper接口,用于定义与实体相关的数据库操作方法。在接口中,我们可以使用注解或XML来映射SQL语句。
public interface UserMapper {
// 添加用户
void addUser(User user);
// 根据ID查询用户
User getUserById(int id);
// 更新用户信息
void updateUser(User user);
// 删除用户
void deleteUser(int id);
}
3. 创建Mapper XML文件
接下来,我们需要创建一个Mapper XML文件,其中定义了与MyBatis Mapper接口相对应的SQL语句。
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<insert id="addUser" parameterType="com.example.model.User">
INSERT INTO user (id, name) VALUES (#{id}, #{name})
</insert>
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
<update id="updateUser" parameterType="com.example.model.User">
UPDATE user SET name = #{name} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
4. 配置MyBatis配置文件
然后,我们需要在MyBatis的配置文件中配置Mapper扫描路径和数据库连接信息。
<!-- mybatis-config.xml -->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置数据库连接信息 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 配置Mapper扫描路径 -->
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
5. 生成Mapper接口的实现类
最后,我们需要使用MyBatis的代码生成工具来生成Mapper接口的实现类。
public class MyBatisGenerator {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
Configuration configuration = new Configuration();
Context context = new Context(ModelType.CONDITIONAL);
context.setId("mybatis-generator");
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setDriverClass("com.mysql.jdbc.Driver");
jdbcConnectionConfiguration.setConnectionURL("jdbc:mysql://localhost:3306/mydatabase");
jdbcConnectionConfiguration.setUserId("root");
jdbcConnectionConfiguration.setPassword("password");
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetPackage("com.example.model");
javaModelGeneratorConfiguration.setTargetProject("src/main/java");
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setTargetPackage("com.example.mapper");
javaClientGeneratorConfiguration.setTargetProject("src/main/java");
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName("user");
tableConfiguration.setDomainObjectName("User");
context.addTableConfiguration(tableConfiguration);
configuration.addContext(context);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator generator = new MyBatis