SSM implementation of paging fuzzy query
I. preparations
1,pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!--Reverse generation with paging--> <dependency> <groupId>com.itfsw</groupId> <artifactId>mybatis-generator-plugin</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.3.2</version> <scope>compile</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.7.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--MyBatis integration spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--pojo convert to json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.3</version> </dependency> </dependencies>
2,web.xml
<!--Configure global load through listener spring configuration file--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Solve post Method medium request Requested jumble--> <filter> <filter-name>ce</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--Whether asynchronous is supported--> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>ce</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--To configure mvc-servlet--> <servlet> <servlet-name>ds</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc/SpringMvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ds</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3,srping.xml
<!--1,open Spring Annotation driven--> <context:component-scan base-package="com.k9503.service"/> <!--2,Load db.properties configuration file--> <context:property-placeholder location="classpath:mybatis/properties/db.properties"/> <!--3,To configure c3p0 Connection pool--> <bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--4,Configure transaction management--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dateSource"/> </bean> <!--5,Enable transaction management annotation driver--> <tx:annotation-driven transaction-manager="transactionManager"/> <!--6,Administration MyBatis Core profile--> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dateSource"/> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!--7,Administration MyBatis The relationship between mapping files and interfaces in--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.k9503.mapper"/> <property name="sqlSessionFactoryBeanName" value="factory"/> </bean>
4,springMvc.xml
<!--open controller Annotation of layers--> <context:component-scan base-package="com.k9503.controller"/> <!--open mvc Unique notes--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="utf-8"/> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <!--Configure default servlet--> <mvc:default-servlet-handler/> <!--Handle jsp Page suffix--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
5. MyBatis reverse generation / gener.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <!-- Build profile --> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--Paging plugins--> <plugin type="com.itfsw.mybatis.generator.plugins.LimitPlugin"/> <commentGenerator> <!-- Remove comments or not,true Express is,false no --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 1.Connection database information --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/k9503?characterEncoding=UTF-8" userId="root" password="171009"> </jdbcConnection> <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolves to Integer,by true Shi Ba JDBC DECIMAL and NUMERIC Type resolves to java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 2.pojo Build configuration of class targetPackage Represents the destination folder targetProject Represents the destination address where the current destination folder is placed --> <javaModelGenerator targetPackage="com.k9503.pojo" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 3.sql Mapping file generation configuration --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources\mybatis"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 4.mapper Interface configuration --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.k9503.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 5.Database table and entity class mapping --> <table tableName="emp" domainObjectName="Emp"> </table> <table tableName="dept" domainObjectName="Dept"> </table> </context> </generatorConfiguration>
6,mybatis.xml
<configuration> <settings> <! -- log information can be output in the console: sql statements and parameters -- > <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <! -- 2. Represent type alias -- > <typeAliases> <! -- 2.1 the alias type attribute of each type indicates the type address corresponding to the alias alias alias indicates the alias -- > <typeAlias type="java.util.List" alias="list"/> <! -- package means package. Scan all classes under the package in the form of package. The alias is the current class name -- > <package name="com.k9503.pojo"/> </typeAliases> </configuration>
II. Display page
1.1, show
<!--Conditional query--> <form action="/emp/show" method="post" id="fm" style="text-align: center"> <h2 style="text-align: center">Staff management</h2> Full name:<input type="text" name="name" value="${page.name}"/> Birthday:<input type="date" name="startBirth" value="<fmt:formatDate value='${page.startBirth}' pattern='yyyy-MM-dd'/>" >- <input type="date" name="endBirth" value="<fmt:formatDate value='${page.endBirth}' pattern='yyyy-MM-dd'/>" > Department:<select name="deptId"> <option value="0">--Please choose--</option> <c:forEach var="dept" items="${depts}"> <option value="${dept.id}"<c:if test="${dept.id==page.deptId}"> selected=true</c:if>>${dept.dname}</option> </c:forEach> </select> <input type="hidden" id="curr" name="currPage" value="1" > <input type="submit" value="search"> </form> <!--Exhibition--> <table align="center" cellpadding="15" cellspacing="0" border="1" bgcolor="#add8e6"> <tr> <td>number</td> <td>Full name</td> <td>Age</td> <td>Gender</td> <td>wages</td> <td>allowance</td> <td>Birthday</td> <td>Entry time</td> <td>Superior</td> <td>department</td> <td>operation| <input type="button" onclick="location.href='/emp/add'" value="Newly added"></td> </tr> <c:if test="${not empty list}"> <c:forEach var="emp" items="${list}" varStatus="vs"> <tr <c:if test="${vs.count%2==1}"> bgcolor="#7fffd4" </c:if>> <td>${emp.id}</td> <td>${emp.name}</td> <td>${emp.age}</td> <td>${emp.sex}</td> <td>${emp.salary}</td> <td>${emp.bonus}</td> <td><fmt:formatDate value="${emp.birth}" pattern="yyyy-MM-dd"/></td> <td><fmt:formatDate value="${emp.hiredate}" pattern="yyyy-MM-dd"/></td> <td>${emp.leader}</td> <td>${emp.dept.dname}</td> <td> <a onclick="return confirm('Are you sure to delete?')" href="/emp/deleteById/${emp.id}" id="del">delete</a>| <a href="/emp/selectById/${emp.id}">modify</a></td> </tr> </c:forEach> </c:if> </table> <!--paging--> <table align="center" cellpadding="10" cellspacing="10"> <tr> <td><a href="javascript:toPage(1)">home page</a></td> <td><a href="javascript:toPage(${page.currPage-1})">Previous page</a></td> <c:forEach var="i" begin="1" end="${page.totalPage}"> <td><a href="javascript:toPage(${i})">${i}</a></td> </c:forEach> <td><a href="javascript:toPage(${page.currPage+1})">next page</a></td> <td><a href="javascript:goPage()">Jump</a>To the first<input size="2" type="text" value="${page.currPage}" id="go">page</td> <td><a href="javascript:toPage(${page.totalPage})">Tail page</a></td> </tr> </table>
1.2 service layer
//Query all @Override public List<Emp> selectAll(PageBean page) { EmpExample example = new EmpExample(); EmpExample.Criteria criteria = example.createCriteria(); if (page.getName()!=null&&!page.getName().trim().equals("")){ criteria.andNameLike("%"+page.getName()+"%"); } if (page.getStartBirth()!=null){ criteria.andBirthGreaterThanOrEqualTo(page.getStartBirth()); } if (page.getEndBirth()!=null){ criteria.andBirthLessThanOrEqualTo(page.getEndBirth()); } if (page.getDeptId()!=null&&page.getDeptId()!=0){ criteria.andDeptidEqualTo(page.getDeptId()); } //Total records queried int count = (int) empMapper.countByExample(example); int size=page.getSize(); int totalPage=(count%size==0)?(count/size):(count/size+1); page.setCount(count); page.setTotalPage(totalPage); //paging int startRow=(page.getCurrPage()-1)*size; example.limit(startRow,size); List<Emp> list = empMapper.selectByExample(example); for (Emp emp:list){ Dept dept = deptMapper.selectByPrimaryKey(emp.getDeptid()); emp.setDept(dept); } return list; }
1.3 controller layer
@Autowired private IEmpService service; @RequestMapping("/show") public ModelAndView show(PageBean page, ModelAndView mav){ List<Emp> list=service.selectAll(page); List<Dept> depts=service.selectDepts(); mav.addObject("list",list); mav.addObject("depts",depts); mav.addObject("page",page); mav.setViewName("show"); return mav; }
Three, new
1.1 new page
<form action="/emp/addEmp" method="post" style="text-align: center" id="add"> <h2 style="text-align: center">Add information</h2> <input type="hidden" name="id" value=""> Full name:<input type="text" name="name" value=""/><br><br> Age:<input type="text" name="age" value=""/><br><br> Gender:     <input type="radio" name="sex" value=""/>male <input type="radio" name="sex" value=""/>female<br><br> Wages:<input type="text" name="salary" value=""/><br><br> Allowance:<input type="text" name="bonus" value=""/><br><br> Birthday:   <input type="date" name="birth" value="<fmt:formatDate value='${emp.birth}' pattern='yyyy-MM-dd'/>"/><br><br> Entry time: <input type="date" name="hiredate" value="<fmt:formatDate value='${emp.hiredate}' pattern='yyyy-MM-dd'/>"/><br><br> Superior:<input type="text" name="leader" value=""/><br><br> Department:   <select name="deptid"> <option value="0">--Please choose--</option> <c:forEach var="dept" items="${depts}"> <option value="${dept.id}" <c:if test="${dept.id==emp.deptid}" >selected='true'</c:if> > ${dept.dname} </option> </c:forEach> </select><br><br> <input type="submit" value="Submission">       <input type="reset" value="cancel"> </form>
1.2 service layer
@Override public int addEmp(Emp emp) { return empMapper.insertSelective(emp); }
1.3 controller layer
//New implementation @RequestMapping("/addEmp") public String addEmp(Emp emp){ int i=service.addEmp(emp); return "redirect:/emp/show"; } //Add successfully jump @RequestMapping("/add") public String add(Model model){ List<Dept> depts = service.selectDepts(); model.addAttribute("depts",depts); return "add"; }
IV. modification and deletion
1.1 modify page
<form action="/emp/update" method="post" style="text-align: center"> <h2 style="text-align: center">Modify information</h2> <input type="hidden" name="id" value="${emp.id}"> Full name:<input type="text" name="name" value="${emp.name}"/><br><br> Age:<input type="text" name="age" value="${emp.age}"/><br><br> Gender:     <input type="radio" name="sex" value="${emp.sex}"<c:if test="${emp.sex=='male'}">checked='true'</c:if>/>male <input type="radio" name="sex" value="${emp.sex}"<c:if test="${emp.sex=='female'}">checked='true'</c:if>/>female<br><br> Wages:<input type="text" name="salary" value="${emp.salary}"/><br><br> Allowance:<input type="text" name="bonus" value="${emp.bonus}"/><br><br> Birthday:   <input type="date" name="birth" value="<fmt:formatDate value='${emp.birth}' pattern='yyyy-MM-dd'/>" /><br><br> Entry time: <input type="date" name="hiredate" value="<fmt:formatDate value='${emp.hiredate}' pattern='yyyy-MM-dd'/>"/><br><br> Superior:<input type="text" name="leader" value="${emp.leader}"/><br><br> Department:   <select name="deptid"> <option value="0">--Please choose--</option> <c:forEach var="dept" items="${depts}"> <option value="${dept.id}" <c:if test="${dept.id==emp.deptid}" >selected='true'</c:if> > ${dept.dname} </option> </c:forEach> </select><br><br> <input type="submit" value="Submission">       <input type="reset" value="cancel"> </form>
1.2 service layer
@Override//delete public int deleteById(int id) { return empMapper.deleteByPrimaryKey(id); } @Override//Query single public Emp selectById(int id) { return empMapper.selectByPrimaryKey(id); } @Override//modify public int updateById(Emp emp) { return empMapper.updateByPrimaryKeySelective(emp); }
1.3 controller layer
//delete @RequestMapping("/deleteById/{id}") public String delete(@PathVariable String id) { int i = service.deleteById(Integer.parseInt(id)); return "redirect:/emp/show"; } //Execution modification @RequestMapping("/update") public String updateById(Emp emp){ int i=service.updateById(emp); return "redirect:/emp/show"; } //Query single @RequestMapping("/selectById/{id}") public String selectById(@PathVariable String id, Model model){ Emp emp = service.selectById(Integer.parseInt(id)); model.addAttribute("emp",emp); List<Dept> depts = service.selectDepts(); model.addAttribute("depts",depts); return "update"; }