Use steps of general mapper:
1. Add dependency: mapper spring boot starter
<!-- currency Mapper starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<!-- mysql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2. Configure four parameters for connecting to the database
server: port: ${port:9091} spring: application: name: user-service datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/springcloud username: root password: 123456 mybatis: type-aliases-package: com.zwhxpp.user.pojo
Because Common Mapper is used, the scanning of alias packages should be specified
3. Write the startup class, configure package scanning through @MapperScan in the startup class, and scan the UserMapper interface in the mapper package
@SpringBootApplication @MapperScan("com.zwhxpp.user.mapper") @EnableDiscoveryClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
Note: @ mapperscan is TK Under mybatis package. import tk.mybatis.spring.annotation.MapperScan;
4. Writing entity classes
@Data @Table(name = "tb_user") public class User{ @Id @KeySql(useGeneratedKeys = true)//Enable auto backfill of primary key private Long id; private String userName;// user name private String password;// password private String name;// full name private Integer age;// Age private Integer sex;// Gender, 1 male, 2 female private Date birthday; // date of birth private Date created;// Creation time private Date updated;// Update time private String note;// remarks }
Because it interacts directly with the database, the annotations in Common Mapper are used: @ Table, @ Id, @ KeySQL
import tk.mybatis.mapper.annotation.KeySql;
@KeySql(useGeneratedKeys = true) / / enable auto backfill of PK
When the primary key is self increasing, the primary key cannot be used when adding a record, but sometimes we need the primary key. We set useGeneratedKeys="true", so that we can obtain the attribute value of the object corresponding to the primary key in the subsequent java code. The value range of useGeneratedKeys is true|false. The default value is: false. Meaning: set whether to use the getgeneratedkeys method of JDBC to obtain the primary key and assign it to the domain model property set by keyProperty.
5. Write the UserMapper interface to operate the database
import com.zwhxpp.user.pojo.User; import tk.mybatis.mapper.common.Mapper; public interface UserMapper extends Mapper<User> { }
6. Write the UserService class and call the method of Mapper interface for database operation
@Service public class UserService { @Autowired private UserMapper userMapper; public User queryById(Long id){ User user = userMapper.selectByPrimaryKey(id); System.out.println(user); return user; } }
7. Write UserController
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User queryById(@PathVariable Long id){ /*try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }*/ User user = userService.queryById(id); return user; } }
General Mapper method:
BaseMapper
@RegisterMapper public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { }@RegisterMapper
public interface BaseMapper<T> extends BaseSelectMapper<T>, BaseInsertMapper<T>, BaseUpdateMapper<T>, BaseDeleteMapper<T> {
}@RegisterMapper
public interface BaseSelectMapper<T> extends SelectOneMapper<T>, SelectMapper<T>, SelectAllMapper<T>, SelectCountMapper<T>, SelectByPrimaryKeyMapper<T>,
ExistsWithPrimaryKeyMapper<T> {
}
1. BaseSelectMapper's method:
Mapper inherits BaseMapper (base mapper), and BaseMapper inherits BaseSelectMapper (base selectMapper)
1) , interface: ` selectbyprimarykeymapper < T >`
Method: ` T selectByPrimaryKey(Object key)`
Note: when querying according to the primary key field, the method parameters must contain the complete primary key attribute, and the query criteria use the equal sign
2) , interface: ` selectmapper < T >`
Method: ` list < T > select (t record)`
Note: query according to the attribute value in the entity, and the query criteria use the equal sign
3) , interface: ` selectallmapper < T >`
Method: ` list < T > selectall()`
Note: the select(null) method can achieve the same effect when querying all results
4) , interface: ` selectonemapper < T >`
Method: ` T selectOne(T record)`
Note: when querying according to the attributes in the entity, there can only be one return value. Multiple results throw exceptions, and the query criteria use the equal sign
5) , interface: ` selectcountmapper < T >`
Method: ` int selectCount(T record)`
Note: according to the total number of attribute queries in the entity, the query criteria use the equal sign
2. BaseInsertMapper method:
Mapper inherits BaseMapper (base mapper), and BaseMapper inherits BaseInsertMapper (base InsertMapper)
1) , interface: ` insertmapper < T >`
Method: ` int insert(T record)`
Note: if you save an entity, the null attribute will also be saved, and the database default value will not be used
2) , interface: ` insertselectivemapper < T >`
Method: ` int insertSelective(T record)`
Note: when saving an entity, the null attribute will not be saved, and the database default value will be used
3. BaseUpdateMapper method:
1) . interface: ` updatebyprimarykeymapper < T >`
Method: ` int updateByPrimaryKey(T record)`
Note: update all fields of the entity according to the primary key, and the null value will be updated
2) . interface: ` updatebyprimarykeyselectivemapper < T >`
Method: ` int updateByPrimaryKeySelective(T record)`
Description: update the non null value of the property according to the primary key
4. Method of BaseDeleteMapper
1) , interface: ` deletemapper < T >`
Method: ` int delete(T record)`
Note: delete according to the entity attribute as the condition, and the query condition uses the equal sign
2) , interface: ` deletebyprimarykeymapper < T >`
Method: ` int deleteByPrimaryKey(Object key)`
Note: delete according to the primary key field. The method parameter must contain the complete primary key attribute
ExampleMapper
@RegisterMapper public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { }@RegisterMapper
public interface ExampleMapper<T> extends SelectByExampleMapper<T>, SelectOneByExampleMapper<T>, SelectCountByExampleMapper<T>,
DeleteByExampleMapper<T>,UpdateByExampleMapper<T>, UpdateByExampleSelectiveMapper<T> {}
Method of ExampleMapper:
1) , interface: ` selectbyexamplemapper < T >`
Method: ` list < T > selectbyexample (object example)`
Description: query according to the Example condition
Key: this query supports specifying query columns through the 'Example' class and the 'selectProperties' method
2) , interface: ` selectonebyexamplemapper < T >`
Method: ` T selectOneByExample(Object example)`
Note: query a record according to the Example condition
3) , interface: ` selectcountbyexamplemapper < T >`
Method: ` int selectCountByExample(Object example)`
Note: the total number of queries based on the Example criteria
4) . interface: ` updatebyexamplemapper < T >`
Method: ` int updateByExample(@Param("record") T record, @Param("example") Object example)`
Note: update all the attributes contained in the entity 'record' according to the Example condition, and the null value will be updated
5) . interface: ` updatebyexampleselectivemapper < T >`
Method: ` int updateByExampleSelective(@Param("record") T record, @Param("example") Object example)`
Note: update the non null attribute value contained in the entity 'record' according to the Example condition
6) , interface: ` deletebyexamplemapper < T >`
Method: ` int deleteByExample(Object example)`
Description: delete data according to the Example condition
RowBoundsMapper
@RegisterMapper public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { } @RegisterMapper public interface RowBoundsMapper<T> extends SelectByExampleRowBoundsMapper<T>, SelectRowBoundsMapper<T> { }
1) , interface: ` selectrowboundsmapper < T >`
Method: ` list < T > selectbyrowbounds (t record, rowbounds, rowbounds)`
Note: perform paging query according to entity attributes and rowboundaries
2) , interface: ` selectbyexamplerowboundsmapper < T >`
Method: ` list < T > selectbyexampleandrowbounds (object example, rowbounds, rowbounds)`
Note: perform paging query according to the example condition and rowboundaries