A very lightweight MySQL operation framework is recommended

Time:2021-10-28

A very lightweight MySQL operation framework is recommended:Portal

Default support function
method explain
boolean has(Serializable id) Check whether the record exists according to the primary key
E findById(Serializable id) Find objects by primary key
List<E> findByIds(Collection<? extends Serializable> ids) Find objects in batch based on primary key
List<E> find() Query all records
List<E> findByColumn(String column, Serializable value) Query records according to specified fields
List<E> findByColumn(String column, Collection<? extends Serializable> values) Query records according to specified fields
List<E> find(Conditions conditions) Please refer to the following case for the specific usage of condition query
Paginator<E> findByPage(Conditions conditions, Integer pageSize, Integer pageNumber) Paging query
Integer delete(Serializable id) Delete by primary key
Integer delete(List<? extends Serializable> ids) Delete by primary key
Integer delete(String column, Collection<? extends Serializable> values) Delete by field
Integer insert(E entity) Save object
Integer insert(List<E> entitys) Batch save objects
Integer update(E entity) Modify object (modify according to primary key)
Integer update(String id, String key, Object value) modify
Integer update(List<? extends Serializable> ids, Map<String, Object> data) Batch modification
Integer update(String id, Map<String, Object> data) modify
Integer update(List<? extends Serializable> ids, String key, Object value) modify
Step 1: inject JDBC template
@Bean
public JdbcTemplate jdbcTemplate() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
    return jdbcTemplate;
}

It can also be injected through XML configuration files

Step 2: define objects
@Data
@Table ("orders") // define the table name
public class Order implements Entity {
     @Generated // set auto increment attribute
     private Integer orderId;
     @Primarykey // define the primary key
     private String orderCode;
     private String remark;
}
Step 3: define the repository
@Repository
public class OrderRepository extends JdbcRepository<Order> {
    //nothing to do
}
Step 4: add, delete, modify and query
//Add
Order order = new Order();
order.setOrderCode(String.valueOf(System.currentTimeMillis()));
order.setRemark(LocalDateTime.now().toString());
orderRepository.insert(order);

//Modification
order.setRemard("remark");
orderRepository.update(order);
  
//Inquiry
orderRepository.findById("1593238076676"));
  
//Delete
orderRepository.delete("1593238076676");
other

If you need to execute complex SQL, you can use the built-injdbcTemplateObject to execute the corresponding SQL.

this.jdbcTemplate.query(sql, args);

If you want to specify a data source, you can override the method

/**
*If you want to specify a jdbctemplate, you can modify it through this method
*
* @param jdbcTemplate
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

Conditions condition usage

Conditions conditions = new Conditions()
  . equal ("ordercode", "1593238616437") // equal to
  . lessthenorequal ("OrderID", 13) // less than
  . greaterthen ("OrderID", 10) // greater than
  . desc ("OrderID") // descending
  . ASC ("ordercode") // ascending order
  .limit(10);// Limit paging
orderRepository.find(conditions).forEach(x -> {
  logger.info("{}", x.toString());
});

Personally, I think it is very suitable for rapid development. Simple project use!

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]