Introduction to the
springboot
The goal is to speed up development and reduce XML configuration. If you don’t want to write a configuration file, just add the corresponding configuration to the configuration file and start the program quickly.
General mapp
General mapper only supports the operation of a single table, the increase, deletion, change and check of a single table, no need to write the corresponding SQL statement in mapper. XML, we only need to call the corresponding interface.
pagehelp
Pagehelper performs a paginated query of the query data.
- First in the Maven project, mapper and PageHelper dependencies are introduced in pom.xml
<! -- pagehelp --> <dependency> <groupId>com.github.pagehelper</groupId> < artifactId > pagehelper - spring - the boot - starter < / artifactId > < version > 1.2.3 < / version > < / dependency > <! Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> The < version > 1.0.0 < / version > < / dependency >Copy the code
2 Create a mymapper. Java file and inherit the mapper interface
Public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T>,ConditionMapper<T>Copy the code
This Java file cannot be placed with other Mappers in case it is scanned. Operations that fetch single table data call this method directly.
3 Add future fields to the configuration file
# JDBC spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / news spring. The datasource. The username = database user name Spring. The datasource. Password database password = spring. The datasource. The driver - class - name = com. Mysql.. JDBC driver spring.freemarker.request-context-attribute=request #mapper mapper.mappers=com.imooc.springboot.mapper.util.MyMapper mapper.not-empty=false mapper.identity=MYSQL #pagehelper pagehelper.helper-dialect = mysql pagehelper.reasonable = true pagehelper.support-methods-arguments = true pagehelper.params= count= countSqlCopy the code
Mapper. mappers, the configuration above, is where the files are in step 2.
4 After controller file is added, methods in controller call methods in server. Although there is a general mapper method, but each time after adding a server method to add the corresponding Mapper method, so the development is also more cumbersome, so we need a general server class, with this class to call the method of the second step.
Public interface BaseService<T> {public interface BaseService<T> {public interface BaseService<T> {List<T> findAll(); /** * add ** @param t entity ** @return */ int save(t t); /** * modify ** @param t * entity * @return */ int updateByPrimaryKey(t t); /** * delete from primary key ** @param t primary key ** @return */ int deleteByPrimaryKey(int t); @return */ TableData< t > getTableData(PageBean PageBean); }Copy the code
The above just encapsulates the basic method of adding, deleting, changing, and checking. Then add the implementation class
public abstract class BaseServiceImpl<T> implements BaseService<T> { @Autowired protected MyMapper<T> mapper; @Override public List<T> findAll() { return mapper.selectAll(); } @Override public int save(T t) { return mapper.insert(t); } @Override public int updateByPrimaryKey(T t) { return mapper.updateByPrimaryKey(t); } @Override public int deleteByPrimaryKey(int t) { return mapper.deleteByPrimaryKey(t); } @Override public TableData<T> getTableData(PageBean bean) { int count = mapper.selectAll().size(); if (count > 0) { PageHelper.startPage((bean.getOffset()/bean.getLimit()) + 1, bean.getLimit()); List<T> list = this.findAll(); return TableData.bulid(count, list); } return TableData.empty(); }}Copy the code
Note: THE editor I use is Eclipse. If I use the IDEA editor, I can leave abstract out here.
Then add corresponding interfaces and implementation classes that inherit the above interfaces and methods, such as a NewsServer interface and a newsserverImpl class
public interface NewsService extends BaseService<SysUser> {
}
Copy the code
@Service
public class NewsServiceImpl extends BaseServiceImpl<SysUser> implements NewsService{
}
Copy the code
5. In order to reduce the pressure on the database server, pageHelper is usually used for paging query when we query data. In order to display the data more clearly, Bootstrap Table is used to display the data. One is client mode, that is, after all the data is captured, it is displayed in front page. The other is the server mode that we will talk about next: the data information to be obtained, such as the page number of data and the size of each page of data, can be sent to the background by the front end through the above parameters, and the background will return the data after getting these parameters. 6. After introducing js CSS files related to Bootstrap table, I started to find some materials on the Internet and found that many of them needed to add the following complicated configurations on the front-end page.
$('# myTABLE '). BootstrapTable ({// request method: 'get', // whether to display row interval striped: (*) cache: false, (*) pagination: true, (*) sortable: False, // sort by sortOrder: PageNumber :1, pageSize: (*) pageSize: (*) pageSize: (*) pageSize: (*) pageSize: (*) pageSize: (*) pageSize: (*) pageSize: (*) pageSize: (*) pageList: [10, 25, 50, 100], // This interface needs to handle fixed arguments passed by the bootstrap table and return json data urls in a specific format: ${contextPath}/mapper/getTableData", // default value is 'limit', pass to server: Limit, offset, search, sort, order Else //queryParamsType: ", //// query parameter. function(params) { var subcompany = $('#subcompany option:selected').val(); var name = $('#name').val(); return { pageNumber: params.offset+1, pageSize: params.limit, companyId:subcompany, name:name }; }, // sidePagination: "server", // false, //Enable the strict search. strictSearch: true, //Indicate which field is an identity field. idField : "id", columns: [], pagination:true });Copy the code
Bootstrap-table. js has a default configuration, so you only need to modify a few configurations.
ContentType: 'application/json',// Post header Application /x-www-form-urlencoded; Charset = utF-8 'dataType: 'json', sidePagination: 'server', // change to serverCopy the code
The queryParams() method is automatically called when we click on the page number of the table to change the page number displayed on each page, and we need to pass this data to the background.
function queryParams(params) { var query={}; query["limit"] = params.limit; Query ["offset"] = params.offset; // Query ["offset"] = params.offset; Return query; }Copy the code
6 with the pagination of the previous step, we need to use pageHelp plug-in, also we put this pagination method on the common server class,
public TableData<T> getTableData(PageBean bean) {
int count = mapper.selectAll().size();
if (count > 0) {
PageHelper.startPage((bean.getOffset()/bean.getLimit()) + 1, bean.getLimit());
List<T> list = this.findAll();
return TableData.bulid(count, list);
}
return TableData.empty();
}
Copy the code
Pagehelper. startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage = pagehelper.startPage Be sure to query the startPage method on the first line of the data statement, not empty or newline. Addendum: Github source code demo