前言:实现功能(此处仅仅展示小区管理)
????????
一、进行配置
1、applicationContext.xml
<?xml version="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/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:config/jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<property name="configLocation" value="classpath:config/mybatis.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.property.management.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<context:component-scan base-package="com.property.management"></context:component-scan>
<tx:annotation-driven></tx:annotation-driven>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
2、jdbc:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///management?severTimezone=UTC&useSSL=false&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
jdbc.username=*****
jdbc.password=*****
这里账号密码需要根据自己的数据库进行添加
3、mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="classpath:config/applicationContext.xml"></import>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/register"/>
<mvc:exclude-mapping path="/user/captcha"/>
<mvc:exclude-mapping path="/asset/**"/>
<mvc:exclude-mapping path="/css/**"/>
<mvc:exclude-mapping path="/images/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/lib/**"/>
<mvc:exclude-mapping path="/ueditor/**"/>
<bean class="com.property.management.intercept.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 文件上传 id是固定的值 不可改变-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传大小限制 50M-->
<property name="maxUploadSize">
<value>52428800</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
</beans>
?4、mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.property.management.entity"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
? ? ? ?5、pom.xml
<?xml version="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.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>property-management-system</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>property-management-system Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<!--json数据格式的依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<!--spring 测试依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring 事务的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.20</version>
</dependency>
<!--mybatis 环境 依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.9</version>
</dependency>
<!--mybatis+spring 整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--spring 对于jdbc的支持的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
<!--其它依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<finalName>property-management-system</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
二、业务层展示
3、实现功能(小区模块为例)
controller层:(CommunityController)
package com.property.management.contorller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.property.management.entity.Community;
import com.property.management.entity.RespBean;
import com.property.management.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* @author 专业坦克驾驶员
* @version 1.0
* @date 2022年07月12日 14:30
* <p>
* CommunityController层
*/
@Controller
@MultipartConfig
public class CommunityController {
@Autowired
private CommunityService communityService;
private String imgString = null;
private String imgUrl = null;
//添加按钮 跳转到communityadd.jsp页面
@RequestMapping("communityadd")
public String communityadd() {
return "community/communityadd";
}
@RequestMapping("communitylist")
public String queryByName(String communityName, String startTime, String endTime, Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
//查询单个或多个
PageHelper.startPage(pageNum, 6);//pageNum:当前页码数,第一次进来时默认为1(首页)每页显示pagesize条
List<Community> community = communityService.queryByName(communityName, startTime, endTime);
model.addAttribute("startTime", startTime);
model.addAttribute("endTime", endTime);
model.addAttribute("communityName", communityName);
//pageInfo:将分页数据和显示的数据封装到PageInfo当中
PageInfo<Community> pageInfo = new PageInfo<>(community);
Long count = communityService.queryByNameCount(communityName, startTime, endTime);
System.out.println("list = " + community);
model.addAttribute("commList", community);
model.addAttribute("count", count);
model.addAttribute("pageInfo", pageInfo);//将封装好的数据返回到前台页面
return "community/communitylist";
}
//编辑按钮点击 跳转到communityupdate.jsp页面
@RequestMapping("/communityupdate")
public String communityupdate(Integer id, Model model) {
Community community = communityService.queryById(id);
model.addAttribute("comm", community);
return "community/communityupdate";
}
//监听保存按钮进行数据更新
@RequestMapping("updateCommunity")
@ResponseBody
public Object updateCommunity(Community community) {
community.setImg(imgString);
System.out.println("--------------------------------------------------------");
System.out.println("community = " + community);
Boolean b = communityService.updateCommunity(community);
HashMap<String, Object> map = new HashMap<>();
if (b) {
map.put("info", "用户更新成功");
map.put("status", 200);
} else {
map.put("info", "用户更新失败");
map.put("status", 500);
}
return map;
}
//单独删除
@RequestMapping("deleteById/{id}")
@ResponseBody
public Object deleteById(@PathVariable("id") Integer id) {
Boolean b = communityService.deleteById(id);
HashMap<String, Object> map = new HashMap<>();
if (b) {
map.put("info", "用户删除成功");
map.put("status", 200);
} else {
map.put("info", "用户删除失败");
map.put("status", 500);
}
return map;
}
//批量删除
@RequestMapping("deleteCommunityByIds/{ids}")
@ResponseBody
//PathVariable获取路径参数
public Object deleteCommunityByIds(@PathVariable("ids") String ids) {
Boolean b = communityService.deleteCommunityByIds(ids);
HashMap<String, Object> map = new HashMap<>();
if (b) {
map.put("info", "用户删除成功");
map.put("status", 200);
} else {
map.put("info", "用户删除失败");
map.put("status", 500);
}
return map;
}
//添加信息
// @RequestMapping("communityInsert")
// @ResponseBody
// public Object communityInsert(Community community) {
// System.out.println("community = " + community);
// String address = community.getAddress();
// System.out.println("address = " + address);
// Integer line = communityService.insertInfo(community);
// HashMap<String, Object> map = new HashMap<>();
// if (line > 0) {
// map.put("info", "用户添加成功");
// map.put("status", 200);
// } else {
// map.put("info", "用户添加失败");
// map.put("status", 500);
// }
// return map;
// }
@RequestMapping("/communityInsert")
@ResponseBody
public RespBean add(Community community) {
Date date = new Date();
community.setHiredate(date);
System.out.println("imgString = " + imgString);
community.setImg(imgString);
Integer result = communityService.insertInfo(community);
RespBean respBean = null;
if (result == 1) {
respBean = RespBean.ok("添加成功");
} else {
respBean = RespBean.error("添加失败");
}
return respBean;
}
//监听图片的上传按钮进行上传图片
@PostMapping("/communityImgInsert")
@ResponseBody
public Map<String, String> fileupload(MultipartFile img, HttpServletRequest req, Model model) {
Map<String, String> result = new HashMap<>();
String oldName = img.getOriginalFilename();
String realPath = req.getServletContext().getRealPath("/images");
System.out.println("realPath = " + realPath);
File folder = new File(realPath);
if (!folder.exists()) {
folder.mkdirs();
}
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
try {
img.transferTo(new File(folder, newName));
String tomcatUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getContextPath() + "/images/" + newName;
System.out.println("url = " + tomcatUrl);
result.put("status", "200");
result.put("newName", newName);
imgString = newName;
result.put("url", tomcatUrl);
imgUrl = tomcatUrl;
System.out.println("url = " + tomcatUrl);
} catch (IOException e) {
result.put("status", "500");
result.put("msg", e.getMessage());
}
return result;
}
}
service层:包含CommunityService接口和CommunityServiceImpl实现类
service接口:
package com.property.management.service;
import com.property.management.entity.Community;
import java.util.List;
/**
* @author 专业坦克驾驶员
* @version 1.0
* @date 2022年07月12日 14:31
*/
public interface CommunityService {
Integer insertInfo(Community community);
List<Community> queryByName(String communityName, String startTime, String endTime);
Long queryByNameCount(String communityName, String startTime, String endTime);
Community queryById(Integer id);
Boolean deleteById(Integer id);
Boolean deleteCommunityByIds(String ids);
Boolean updateCommunity(Community community);
}
service实现类:
package com.property.management.service.impl;
import com.property.management.entity.Community;
import com.property.management.mapper.CommunityMapper;
import com.property.management.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author 专业坦克驾驶员
* @version 1.0
* @date 2022年07月12日 14:30
*/
@Service
public class CommunityServiceImpl implements CommunityService {
@Autowired
private CommunityMapper communityMapper;
@Override
public Integer insertInfo(Community community) {
// community.setImg("tx.jpg");
Integer line = communityMapper.insert(community);
return line;
}
@Override
public List<Community> queryByName(String communityName, String startTime, String endTime) {
return communityMapper.queryByName(communityName, startTime, endTime);
}
@Override
public Long queryByNameCount(String communityName, String startTime, String endTime) {
return communityMapper.queryByNameCount(communityName, startTime, endTime);
}
@Override
public Community queryById(Integer id) {
return communityMapper.queryById(id);
}
@Override
public Boolean deleteById(Integer id) {
return communityMapper.deleteById(id);
}
@Override
public Boolean deleteCommunityByIds(String ids) {
return communityMapper.deleteCommunityByIds(ids);
}
@Override
public Boolean updateCommunity(Community community) {
return communityMapper.updateCommunity(community);
}
}
mapper层(包含CommunityMapper接口和CommunityMappe.xml)
CommunityMapper接口:
package com.property.management.mapper;
import com.property.management.entity.Community;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author 专业坦克驾驶员
* @version 1.0
* @date 2022年07月12日 14:30
*/
public interface CommunityMapper {
Integer insert(Community community);
List<Community> queryByName(@Param("communityName") String communityName, @Param("startTime") String startTime, @Param("endTime") String endTime);
Long queryByNameCount(@Param("communityName") String communityName, @Param("startTime") String startTime, @Param("endTime") String endTime);
Community queryById(Integer id);
Boolean deleteById(Integer id);
Boolean deleteCommunityByIds(String ids);
Boolean updateCommunity(Community community);
}
CommunityMapper.xml(动态sql)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.property.management.mapper.CommunityMapper">
<!--添加信息-->
<insert id="insert">
insert into db_community(communityNum,name, address, communityArea, developerName,propertyName,greeningRate, buildingNum, roomNum,state,img,hiredate)
values (#{communityNum},#{name}, #{address}, #{communityArea}, #{developerName}, #{propertyName}, #{greeningRate}, #{buildingNum}, #{roomNum}, #{state},#{img},#{hiredate})
</insert>
<!--模糊查询-->
<select id="queryByName" resultType="com.property.management.entity.Community">
select
id,communityNum,name, address, communityArea, developerName,propertyName,greeningRate, buildingNum,roomNum,state,hiredate,img
from db_community
<trim prefix="where" prefixOverrides="and||or">
<if test="communityName != null and communityName != ''">
and name LIKE CONCAT(CONCAT('%',#{communityName},'%'))
</if>
<if test="startTime != null and startTime != ''">
and hiredate >= #{startTime}
</if>
<if test="endTime != null and endTime != ''">
and hiredate <= #{endTime}
</if>
</trim>
</select>
<select id="queryByNameCount" resultType="java.lang.Long">
select
count(*)
from db_community
<trim prefix="where" prefixOverrides="and||or">
<if test="communityName != null and communityName != ''">
and name LIKE CONCAT(CONCAT('%',#{communityName},'%'))
</if>
<if test="startTime != null and startTime != ''">
and hiredate >= #{startTime}
</if>
<if test="endTime != null and endTime != ''">
and hiredate <= #{endTime}
</if>
</trim>
</select>
<!--根据单个id进行查询-->
<select id="queryById" resultType="com.property.management.entity.Community">
select
id,communityNum,name, address, communityArea, developerName,propertyName,greeningRate, buildingNum, roomNum,state,hiredate,img
from db_community
where id = #{id}
</select>
<!-- 根据id进行删除-->
<delete id="deleteById">
delete from db_community where id in (${id})
</delete>
<!-- 批量进行删除-->
<delete id="deleteCommunityByIds">
delete FROM db_community where id in (${ids})
</delete>
<!--进行更新-->
<update id="updateCommunity">
update db_community
<trim prefix="set" suffixOverrides=",">
<if test="communityNum!=null">communityNum=#{communityNum},</if>
<if test="name!=null">name=#{name},</if>
<if test="address!=null">address=#{address},</if>
<if test="communityArea!=null">communityArea=#{communityArea},</if>
<if test="developerName!=null">developerName=#{developerName},</if>
<if test="propertyName!=null">propertyName=#{propertyName},</if>
<if test="greeningRate!=null">greeningRate=#{greeningRate},</if>
<if test="buildingNum!=null">buildingNum=#{buildingNum},</if>
<if test="roomNum!=null">roomNum=#{roomNum},</if>
<if test="state!=null">state=#{state},</if>
<if test="img!=null">img=#{img},</if>
</trim>
where id = #{id}
</update>
</mapper>
演示视频:(小区功能演示)
小结:jsp页面就没有放置? ? ?有需要的话可以私我? (分享学习历程、愿天堂没有代码)
|