Annotations
Hint
This article is generated by AI translation.
dbVisitor provides several annotations that you can place on interface methods so they act as database access mediators, eliminating verbose JDBC code.
1. Method annotations keep SQL separate from invocation logic
@SimpleMapper
public interface UserMapper {
@Query("select * from users where id > ?")
List<User> listUsers(long searchId);
}
2. Build a BaseMapper
// 1. Create the Configuration
Configuration config = new Configuration();
// 2. Create the Session
Session session = config.newSession(dataSource);
or
Session session = config.newSession(connection);
// 3. Create the Mapper
UserMapper mapper = session.createMapper(UserMapper.class);
List<User> result = mapper.listUsers(2);
Usage Guide
Define any number of methods on your Mapper interface and annotate them with the appropriate SQL annotation to interact with the database.
- @Query: runs a query that returns a result set and maps it to the method return value.
- @Insert: executes an INSERT statement.
- @Update: executes an UPDATE statement.
- @Delete: executes a DELETE statement.
- @Execute: executes any SQL, e.g., INSERT, UPDATE, DELETE, or DDL.
- @Call: executes a stored procedure.
- @Segment: defines a SQL snippet that can be referenced by other SQL methods in the same Mapper through macro rules.
- Rules: add powerful behaviors to SQL statements via rule definitions within SQL.