team表:


player表:


- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.lxygroupId>
- <artifactId>myssm_testartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <packaging>warpackaging>
-
-
-
- <properties>
- <spring.version>5.2.13.RELEASEspring.version>
- <mybatis.version>3.5.6mybatis.version>
- <mybatis.spring.version>1.3.3mybatis.spring.version>
- <pagehelper.version>5.1.10pagehelper.version>
- <mysql.version>8.0.23mysql.version>
- <druid.version>1.2.3druid.version>
- <servlet-api.version>4.0.1servlet-api.version>
- <jackson.version>2.9.6jackson.version>
- <log4j.version>1.2.17log4j.version>
- <junit.version>4.12junit.version>
- <maven.compiler.source>11maven.compiler.source>
- <maven.compiler.target>11maven.compiler.target>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.3.1version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis.spring.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>${druid.version}version>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet-api.version}version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>${jackson.version}version>
- dependency>
-
- <dependency>
- <groupId>log4jgroupId>
- <artifactId>log4jartifactId>
- <version>${log4j.version}version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>javax.annotationgroupId>
- <artifactId>javax.annotation-apiartifactId>
- <version>1.3.2version>
- dependency>
- dependencies>
-
- <build>
- <resources>
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.propertiesinclude>
- <include>**/*.xmlinclude>
- includes>
- <filtering>falsefiltering>
- resource>
- resources>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.apache.tomcat.mavengroupId>
- <artifactId>tomcat7-maven-pluginartifactId>
- <version>2.2version>
- <configuration>
-
- <port>80port>
-
- <path>/path>
- <uriEncoding>UTF-8uriEncoding>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.5version>
- <configuration>
-
- <configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
- <overwrite>trueoverwrite>
- configuration>
- <dependencies>
- <dependency>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-coreartifactId>
- <version>1.3.5version>
- dependency>
- dependencies>
- plugin>
- plugins>
- build>
- project>
Mybatis的配置文件mybatis.xml
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <settings>
- <setting name="logImpl" value="LOG4J" />
- settings>
-
- configuration>
日志配置文件文件log4j.properties:
- # Global logging configuration info warning error
- log4j.rootLogger=DEBUG,stdout
- # Console output...
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
连接数据库的参数配置文件jdbc.properties
- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/myssm_kkb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
- jdbc.username=root
- jdbc.password=linda198721
这里数据库的版本是Mysql8
spring.xml:
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
- <context:component-scan base-package="com.lxy.mapper,com.lxy.service"/>
-
-
- <context:property-placeholder location="classpath*:jdbc.properties"/>
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
-
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="configLocation" value="classpath:mybatis.xml"/>
- <property name="dataSource" ref="dataSource"/>
-
- <property name="typeAliasesPackage" value="com.lxy.pojo"/>
-
-
-
- <property name="plugins">
- <array>
-
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <value>
- reasonable=true
- value>
- property>
- bean>
- array>
- property>
- bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.lxy.mapper"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- bean>
-
-
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- bean>
- beans>
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <context:component-scan base-package="com.lxy.controller"/>
- <mvc:annotation-driven/>
-
- <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/pages/"/>
- <property name="suffix" value=".html"/>
- bean>
-
- <mvc:resources mapping="/img/**" location="/img/"/>
- <mvc:resources mapping="/js/**" location="/js/"/>
- <mvc:resources mapping="/css/**" location="/css/"/>
- <mvc:resources mapping="/fonts/**" location="/fonts/"/>
- <mvc:resources mapping="/static/**" location="/static/"/>
- <mvc:resources mapping="/pages/**" location="/pages/"/>
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- bean>
- beans>
如果您也想要这个项目的话,可以在下发评论,我私信发送给您
- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
- <welcome-file-list>
- <welcome-file>/pages/index.htmlwelcome-file>
- welcome-file-list>
-
-
- <context-param>
-
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath*:spring.xmlparam-value>
- context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
- <servlet>
- <servlet-name>dispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath*:springmvc.xmlparam-value>
- init-param>
-
- <load-on-startup>1load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>dispatcherServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
-
- <filter>
- <filter-name>httpMethodFilterfilter-name>
- <filter-class>
- org.springframework.web.filter.HiddenHttpMethodFilter
- filter-class>
- filter>
- <filter-mapping>
- <filter-name>httpMethodFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
- <filter>
- <filter-name>characterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
-
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
-
- <init-param>
- <param-name>forceRequestEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
-
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>characterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
启动项目,浏览器访问http://localhost:8088/。看到如图所示页面表示项目整合成功。

- "1.0" encoding="UTF-8"?>
- generatorConfiguration
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <generatorConfiguration>
-
- <classPathEntry
- location="D:\repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar" />
- <context id="MyBatis" targetRuntime="MyBatis3">
-
- <commentGenerator>
- <property name="suppressAllComments" value="true" />
- commentGenerator>
-
- <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/myssm_kkb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT"
- userId="a"
- passwor="a">
- jdbcConnection>
-
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false" />
- javaTypeResolver>
-
- <javaModelGenerator targetPackage="com.lxy.pojo"
- targetProject="src\main\java">
- <property name="trimStrings" value="true" />
- javaModelGenerator>
-
- <sqlMapGenerator targetPackage="com.lxy.mapper"
- targetProject="src\main\resources">
- sqlMapGenerator>
-
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="com.lxy.mapper"
- targetProject="src\main\java">
- javaClientGenerator>
-
-
-
- <table tableName="Team">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="Player">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="game">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="GameType">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="Admins">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="AdminRole">
- <property name="useActualColumnNames" value="true"/>
- table>
- context>
- generatorConfiguration>
注意只能运行一次,运行完毕显示BUILD SUCCESS即为成功。

- package com.lxy.test;
-
- import com.lxy.mapper.TeamMapper;
- import com.lxy.pojo.Team;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- import javax.annotation.Resource;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:spring.xml"})
- public class TestMapper {
-
- @Resource
- private TeamMapper teamMapper;
- @Test
- public void test01(){
- Team team = teamMapper.selectByPrimaryKey(1001);
- System.out.println(team);
- }
- }
编写一个QueryVO类,存储查询条件
- package com.lxy.vo;
-
- import org.springframework.format.annotation.DateTimeFormat;
-
- import java.util.Date;
-
- public class QueryVO {
- private String teamName;
- private String chineseName;
- private String coach;
- //@DateTimeFormat(pattern = "yyyy-MM-dd")
- private Date beginDate;
- //@DateTimeFormat(pattern = "yyyy-MM-dd")
- private Date endDate;
- private Integer area;
-
- @Override
- public String toString() {
- return "QueryVO{" +
- "teamName='" + teamName + '\'' +
- ", chineseName='" + chineseName + '\'' +
- ", coach='" + coach + '\'' +
- ", beginDate=" + beginDate +
- ", endDate=" + endDate +
- ", area=" + area +
- '}';
- }
-
- public String getTeamName() {
- return teamName;
- }
-
- public void setTeamName(String teamName) {
- this.teamName = teamName;
- }
-
- public String getChineseName() {
- return chineseName;
- }
-
- public void setChineseName(String chineseName) {
- this.chineseName = chineseName;
- }
-
- public String getCoach() {
- return coach;
- }
-
- public void setCoach(String coach) {
- this.coach = coach;
- }
-
- public Date getBeginDate() {
- return beginDate;
- }
-
- public void setBeginDate(Date beginDate) {
- this.beginDate = beginDate;
- }
-
- public Date getEndDate() {
- return endDate;
- }
-
- public void setEndDate(Date endDate) {
- this.endDate = endDate;
- }
-
- public Integer getArea() {
- return area;
- }
-
- public void setArea(Integer area) {
- this.area = area;
- }
- }
编写service层代码,用来条件分页查询
- package com.lxy.service;
-
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.lxy.mapper.TeamMapper;
- import com.lxy.pojo.Team;
- import com.lxy.pojo.TeamExample;
- import com.lxy.vo.QueryVO;
- import org.springframework.stereotype.Service;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- @Service
- public class TeamService {
- @Resource
- private TeamMapper teamMapper;
-
- public PageInfo
queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){ - TeamExample example = new TeamExample();
- //创建条件的容器
- TeamExample.Criteria criteria = example.createCriteria();
- if (vo!=null){
- if (vo.getTeamName()!=null&&!"".equals(vo.getTeamName().trim())){
- criteria.andTeamNameLike("%"+vo.getTeamName().trim()+"%");
- }
- if(vo.getChineseName()!=null && !"".equals(vo.getChineseName().trim()) ){
- criteria.andChineseNameLike("%"+vo.getChineseName().trim()+"%");
- }
- if(vo.getCoach()!=null && !"".equals(vo.getCoach().trim())){
- criteria.andCoachLike("%"+vo.getCoach().trim()+"%");
- }
- if(vo.getBeginDate()!=null){
- criteria.andCreateTimeGreaterThanOrEqualTo(vo.getBeginDate());
- }
- if(vo.getEndDate()!=null){
- criteria.andCreateTimeLessThanOrEqualTo(vo.getEndDate());
- }
- if(vo.getArea()!=null && vo.getArea()!=-1){
- criteria.andAreaEqualTo(vo.getArea());
- }
- }
- //分页
- PageHelper.startPage(pageNum,pageSize);
- List
list = teamMapper.selectByExample(example); - return new PageInfo<>(list);
- }
- }
编写测试类:
- package com.lxy.test;
-
- import com.github.pagehelper.PageInfo;
- import com.lxy.mapper.TeamMapper;
- import com.lxy.pojo.Team;
- import com.lxy.service.TeamService;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- import javax.annotation.Resource;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:spring.xml"})
- public class TestMapper {
-
- @Resource
- private TeamMapper teamMapper;
-
- @Resource
- private TeamService teamService;
-
- @Test
- public void test02(){
- PageInfo
teamPageInfo = teamService.queryByPage(1, 5, null); - System.out.println(teamPageInfo);
- }
- @Test
- public void test01(){
- Team team = teamMapper.selectByPrimaryKey(1001);
- System.out.println(team);
- }
- }
测试结果:
3.2 控制器层编写controller代码:
- package com.lxy.controller;
-
- import com.github.pagehelper.PageInfo;
- import com.lxy.pojo.Team;
- import com.lxy.service.TeamService;
- import com.lxy.vo.QueryVO;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import javax.annotation.Resource;
-
- @Controller
- @RequestMapping("team")
- @ResponseBody
- public class TeamController {
-
- @Resource
- private TeamService teamService;
-
- @RequestMapping("list")
- public PageInfo
queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){ - if (pageNum==null || pageNum<=0){
- pageNum=1;
- }
- if (pageSize==null || pageSize<=0){
- pageSize=5;
- }
- PageInfo
teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo); - return teamPageInfo;
- }
- }
这样返回给前端是返回的json格式的,包括时间,如果我们要返回正常的时间,需要在Team实体类中的时间字段上添加
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")这样显示的时间正常。
启动程序来测试一下:

这样可以看到返回的json数据正常
将返回值封装为ResultVO,既能返回一个pageInfo,还能返回一个对象,一个集合,以及一条消息。
更改controller代码:
- package com.lxy.controller;
-
- import com.github.pagehelper.PageInfo;
- import com.lxy.pojo.Team;
- import com.lxy.service.TeamService;
- import com.lxy.vo.QueryVO;
- import com.lxy.vo.ResultVO;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import javax.annotation.Resource;
-
- @Controller
- @RequestMapping("team")
- @ResponseBody
- public class TeamController {
-
- @Resource
- private TeamService teamService;
-
- @RequestMapping("list")
- public ResultVO
queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){ - if (pageNum==null || pageNum<=0){
- pageNum=1;
- }
- if (pageSize==null || pageSize<=0){
- pageSize=5;
- }
- PageInfo
teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo); - return new ResultVO<>(teamPageInfo);
- }
- }
启动程序测试:

如果此时想访问localhost的主页面来获取数据,很抱歉目前还获取不到。
因为如果通过主页面来获取条件,初始时时间数据为空,那么程序会想把空指转换为日期类型,所以我们必须要在QueryVO中日期属性上加上日期格式化的注解 如下--
//如果实体类中的日期类型需要从页面获取数据,避免NULL转换为Date类型出现问题
//解决方案2:@InitBinder
//解决方案1:实体类的日期类型属性上添加注解
方案一:
日期格式化:
- package com.lxy.vo;
-
- import org.springframework.format.annotation.DateTimeFormat;
-
- import java.util.Date;
-
- public class QueryVO {
- private String teamName;
- private String chineseName;
- private String coach;
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- private Date beginDate;
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- private Date endDate;
- private Integer area;
-
- @Override
- public String toString() {
- return "QueryVO{" +
- "teamName='" + teamName + '\'' +
- ", chineseName='" + chineseName + '\'' +
- ", coach='" + coach + '\'' +
- ", beginDate=" + beginDate +
- ", endDate=" + endDate +
- ", area=" + area +
- '}';
- }
-
- public String getTeamName() {
- return teamName;
- }
-
- public void setTeamName(String teamName) {
- this.teamName = teamName;
- }
-
- public String getChineseName() {
- return chineseName;
- }
-
- public void setChineseName(String chineseName) {
- this.chineseName = chineseName;
- }
-
- public String getCoach() {
- return coach;
- }
-
- public void setCoach(String coach) {
- this.coach = coach;
- }
-
- public Date getBeginDate() {
- return beginDate;
- }
-
- public void setBeginDate(Date beginDate) {
- this.beginDate = beginDate;
- }
-
- public Date getEndDate() {
- return endDate;
- }
-
- public void setEndDate(Date endDate) {
- this.endDate = endDate;
- }
-
- public Integer getArea() {
- return area;
- }
-
- public void setArea(Integer area) {
- this.area = area;
- }
- }
这样就可以访问到主页面了:

方案二:
在controller中添加注解方法:
- @InitBinder
- protected void initDateFormatBinder(WebDataBinder binder){
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
- }
添加这个方法后,实体类中可不添加注解
teamService添加代码:
- package com.lxy.service;
-
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.lxy.mapper.TeamMapper;
- import com.lxy.pojo.Team;
- import com.lxy.pojo.TeamExample;
- import com.lxy.vo.QueryVO;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- @Service
- public class TeamService {
- @Resource
- private TeamMapper teamMapper;
-
- @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
- public PageInfo
queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){ - TeamExample example = new TeamExample();
- //创建条件的容器
- TeamExample.Criteria criteria = example.createCriteria();
- if (vo!=null){
- if (vo.getTeamName()!=null&&!"".equals(vo.getTeamName().trim())){
- criteria.andTeamNameLike("%"+vo.getTeamName().trim()+"%");
- }
- if(vo.getChineseName()!=null && !"".equals(vo.getChineseName().trim()) ){
- criteria.andChineseNameLike("%"+vo.getChineseName().trim()+"%");
- }
- if(vo.getCoach()!=null && !"".equals(vo.getCoach().trim())){
- criteria.andCoachLike("%"+vo.getCoach().trim()+"%");
- }
- if(vo.getBeginDate()!=null){
- criteria.andCreateTimeGreaterThanOrEqualTo(vo.getBeginDate());
- }
- if(vo.getEndDate()!=null){
- criteria.andCreateTimeLessThanOrEqualTo(vo.getEndDate());
- }
- if(vo.getArea()!=null && vo.getArea()!=-1){
- criteria.andAreaEqualTo(vo.getArea());
- }
- }
- //分页
- PageHelper.startPage(pageNum,pageSize);
- List
list = teamMapper.selectByExample(example); - return new PageInfo<>(list);
- }
-
- //添加操作,再添加一个事务用来回滚
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
- public int addTeam(Team team){
- return teamMapper.insertSelective(team);
- }
- }
teamController添加代码:
- package com.lxy.controller;
-
- import com.github.pagehelper.PageInfo;
- import com.lxy.pojo.Team;
- import com.lxy.service.TeamService;
- import com.lxy.vo.QueryVO;
- import com.lxy.vo.ResultVO;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import javax.annotation.Resource;
-
- @Controller
- @RequestMapping("team")
- @ResponseBody
- public class TeamController {
-
- @Resource
- private TeamService teamService;
-
- @RequestMapping(value = "list",method = RequestMethod.GET)
- public ResultVO
queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){ - if(pageNum==null || pageNum<=0){
- pageNum=1;
- }
- if(pageSize==null || pageSize<=0){
- pageSize=5;
- }
- PageInfo
teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo); - return new ResultVO<>(teamPageInfo);
- }
-
- //添加操作
- @RequestMapping(value = "",method = RequestMethod.POST)
- public ResultVO
addTeam(Team team){ - int i = teamService.addTeam(team);
- if (i==1){
- return new ResultVO<>();
- }
- return new ResultVO<>(500,"服务器内部异常,请稍后再试!");
- }
- }

第一步:点击更新按钮时,首先应该根据此条数据的id来返回整条数据,让数据达到一种回显的效果
第二步:等所有数据都修改之后,再次点击提交时才是真正的更新
teamService添加代码:
- /**
- * 根据主键查询
- * @param teamId
- * @return
- */
- @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
- public Team queryById(Integer teamId){
- return teamMapper.selectByPrimaryKey(teamId);
- }
teamController添加代码:
- //根据主键查询
- @RequestMapping(value = "{id}",method =RequestMethod.GET)
- public ResultVO
update(@PathVariable("id") Integer teamId){ - Team team = teamService.queryById(teamId);
- return new ResultVO<>(team);
- }
teamService添加代码:
- /**
- * 更新 根据主键更新
- * @param team
- * @return
- */
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
- public int updateTeam(Team team){
- return teamMapper.updateByPrimaryKeySelective(team);
- }
teamController添加代码:
- @RequestMapping(value = "{id}",method = RequestMethod.PUT)
- public ResultVO
updateTeam(@PathVariable("id") Integer teamId,Team team){ - team.setTeamId(teamId);
- int i = teamService.updateTeam(team);
- if(i==1){
- return new ResultVO
(); - }
- return new ResultVO<>(500,"服务器内部异常,请稍后再试!");
- }
此删除功能也应该为逻辑删除,即删除时不是数据直接消失,而是数据在显示状态但不能操作。
teamService添加代码:
- /**
- * 根据主键删除 逻辑删除
- * @param teamId
- * @return
- */
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
- public int deleteTeam(Integer teamId){
- Team team = teamMapper.selectByPrimaryKey(teamId);
- team.setIsDel(1);
- return teamMapper.updateByPrimaryKeySelective(team);
- }
teamController添加代码:
- @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
- public ResultVO
delete(@PathVariable("id") Integer teamId){ - int delete = teamService.deleteTeam(teamId);
- if (delete==1){
- return new ResultVO<>();
- }
- return new ResultVO<>(500,"服务器内部异常,请稍后再试!");
- }
七、文件上传teamController添加代码:
- @RequestMapping(value = "{id}",method = RequestMethod.POST)
- public ResultVO
uploadLogo(@RequestParam("logo") MultipartFile myFile, HttpServletRequest request, - @PathVariable("id") Integer teamId){
- //文件上传到指定位置
- String path = request.getServletContext().getRealPath("/img/uploadFile");
- //获取原始文件的名称
- String originalFilename = myFile.getOriginalFilename();
- //定义文件的新名称:随机名称+原有的后缀名
- String randomName= UUID.randomUUID().toString().replace("-","");
- int index = originalFilename.lastIndexOf(".");
- String hz = originalFilename.substring(index);
- String logoName=randomName+hz;
- int num=0;
- try {
- myFile.transferTo(new File(path+"/"+logoName));
- System.out.println("上传成功!"+path+"/"+logoName);
- Team team = new Team();
- team.setTeamId(teamId);
- team.setTeamLogo(logoName);
- num = teamService.updateTeam(team);
- if(num==1){
- return new ResultVO
(); - }else {
- return new ResultVO<>(500,"服务器内部异常,请稍后再试!");
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- return new ResultVO<>(500,"图片上传出现问题!"+e.getMessage());
- }
- }

至此SSM整合完毕,如果你想要此项目来练习一下,请给我一个三连并在下发评论,我会及时联系你给你发前端素材和后端源码,感谢观看!!!