一个简单到不能再简单的房产出售项目截片
程序员文章站
2024-03-13 22:38:46
...
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
classpath:com/kumo/dao/*Mapper.xml<?xml version="1.0" encoding="UTF-8"?>
application/json;charset=UTF-852428804096
package com.kumo.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kumo.pojo.House;
import com.kumo.service.HouseService;
@Controller
@RequestMapping("house")
public class HouseController {
@Resource
private HouseService houseService;
@RequestMapping("/listAllHouses")
public String listAllHouses(Model model,String pageNo,String pageSize) {
//初始化数值
int num = 1;
int size = 3;
if(pageNo != null && !"".equals(pageNo)) {
num = Integer.parseInt(pageNo);
}
if (pageSize != null && !"".equals(pageSize)) {
size = Integer.parseInt(pageSize);
}
//开始分页
PageHelper.startPage(num,size);
//查询数据库信息
List list = houseService.listAllHouses();
//将信息放入PageInfo进行分页
PageInfo pageInfo = new PageInfo(list);
model.addAttribute("pageHelper", pageInfo);
return "theHousePage";
}
/**
* 添加一个房屋信息
*
* @param house
* @param request
* @param multipartFile
* @return
* @throws IOException
*/
@RequestMapping("/addHouse")
public String addHouse(House house,HttpServletRequest request, MultipartFile multipartFile) throws IOException {
// 获取上传文件名称
String filename = multipartFile.getOriginalFilename();
// 获取新的文件名
long millis = System.currentTimeMillis();
String newName = millis + filename;
// 获取文件流
InputStream inputStream1 = multipartFile.getInputStream();
// 获取物理路径
String wlPath = "D:\\eclipse\\练习\\HouseSell\\WebContent\\img\\" + newName;
// 逻辑路径
String ljPath = "../img/" + newName;
// 临时路径
String pathRoot = request.getSession().getServletContext().getRealPath("/");// 自动寻找target根目录
String lsPath = pathRoot + "\\img\\" + newName;
// 判断是否需要创建文件夹
// 物理路径文件夹
File wlFile = new File("D:\\eclipse\\练习\\HouseSell\\WebContent\\img\\");
// 临时路径文件夹
File lsFile = new File(pathRoot + "\\img\\");
if (!wlFile.exists()) {
wlFile.mkdir();
}
if (!lsFile.exists()) {
lsFile.mkdir();
}
// 执行读写操作
if (!multipartFile.isEmpty()) {
// 将文件放入物理路径
FileOutputStream wlFileOutputStream = new FileOutputStream(wlPath);
// 将文件放入临时路径
FileOutputStream lsFileOutputStream1 = new FileOutputStream(lsPath);
int line = 0;
while ((line = inputStream1.read()) != -1) {
// 写入物理路径
wlFileOutputStream.write(line);
// 写入临时路径
lsFileOutputStream1.write(line);
}
wlFileOutputStream.flush();
lsFileOutputStream1.flush();
lsFileOutputStream1.close();
wlFileOutputStream.close();
inputStream1.close();
}
// 将相对路径放入数据库中
house.setHouseImg(ljPath);
houseService.addHouse(house);
return "redirect:/house/listAllHouses";
}
}
controller层
package com.kumo.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kumo.pojo.House;
import com.kumo.service.HouseService;
@Controller
@RequestMapping("house")
public class HouseController {
@Resource
private HouseService houseService;
@RequestMapping("/listAllHouses")
public String listAllHouses(Model model,String pageNo,String pageSize) {
//初始化数值
int num = 1;
int size = 3;
if(pageNo != null && !"".equals(pageNo)) {
num = Integer.parseInt(pageNo);
}
if (pageSize != null && !"".equals(pageSize)) {
size = Integer.parseInt(pageSize);
}
//开始分页
PageHelper.startPage(num,size);
//查询数据库信息
List<House> list = houseService.listAllHouses();
//将信息放入PageInfo进行分页
PageInfo<House> pageInfo = new PageInfo<House>(list);
model.addAttribute("pageHelper", pageInfo);
return "theHousePage";
}
/**
* 添加一个房屋信息
*
* @param house
* @param request
* @param multipartFile
* @return
* @throws IOException
*/
@RequestMapping("/addHouse")
public String addHouse(House house,HttpServletRequest request, MultipartFile multipartFile) throws IOException {
// 获取上传文件名称
String filename = multipartFile.getOriginalFilename();
// 获取新的文件名
long millis = System.currentTimeMillis();
String newName = millis + filename;
// 获取文件流
InputStream inputStream1 = multipartFile.getInputStream();
// 获取物理路径
String wlPath = "D:\\eclipse\\练习\\HouseSell\\WebContent\\img\\" + newName;
// 逻辑路径
String ljPath = "../img/" + newName;
// 临时路径
String pathRoot = request.getSession().getServletContext().getRealPath("/");// 自动寻找target根目录
String lsPath = pathRoot + "\\img\\" + newName;
// 判断是否需要创建文件夹
// 物理路径文件夹
File wlFile = new File("D:\\eclipse\\练习\\HouseSell\\WebContent\\img\\");
// 临时路径文件夹
File lsFile = new File(pathRoot + "\\img\\");
if (!wlFile.exists()) {
wlFile.mkdir();
}
if (!lsFile.exists()) {
lsFile.mkdir();
}
// 执行读写操作
if (!multipartFile.isEmpty()) {
// 将文件放入物理路径
FileOutputStream wlFileOutputStream = new FileOutputStream(wlPath);
// 将文件放入临时路径
FileOutputStream lsFileOutputStream1 = new FileOutputStream(lsPath);
int line = 0;
while ((line = inputStream1.read()) != -1) {
// 写入物理路径
wlFileOutputStream.write(line);
// 写入临时路径
lsFileOutputStream1.write(line);
}
wlFileOutputStream.flush();
lsFileOutputStream1.flush();
lsFileOutputStream1.close();
wlFileOutputStream.close();
inputStream1.close();
}
// 将相对路径放入数据库中
house.setHouseImg(ljPath);
houseService.addHouse(house);
return "redirect:/house/listAllHouses";
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kumo.pojo.House;
import com.kumo.service.HouseService;
@Controller
@RequestMapping("house")
public class HouseController {
@Resource
private HouseService houseService;
@RequestMapping("/listAllHouses")
public String listAllHouses(Model model,String pageNo,String pageSize) {
//初始化数值
int num = 1;
int size = 3;
if(pageNo != null && !"".equals(pageNo)) {
num = Integer.parseInt(pageNo);
}
if (pageSize != null && !"".equals(pageSize)) {
size = Integer.parseInt(pageSize);
}
//开始分页
PageHelper.startPage(num,size);
//查询数据库信息
List<House> list = houseService.listAllHouses();
//将信息放入PageInfo进行分页
PageInfo<House> pageInfo = new PageInfo<House>(list);
model.addAttribute("pageHelper", pageInfo);
return "theHousePage";
}
/**
* 添加一个房屋信息
*
* @param house
* @param request
* @param multipartFile
* @return
* @throws IOException
*/
@RequestMapping("/addHouse")
public String addHouse(House house,HttpServletRequest request, MultipartFile multipartFile) throws IOException {
// 获取上传文件名称
String filename = multipartFile.getOriginalFilename();
// 获取新的文件名
long millis = System.currentTimeMillis();
String newName = millis + filename;
// 获取文件流
InputStream inputStream1 = multipartFile.getInputStream();
// 获取物理路径
String wlPath = "D:\\eclipse\\练习\\HouseSell\\WebContent\\img\\" + newName;
// 逻辑路径
String ljPath = "../img/" + newName;
// 临时路径
String pathRoot = request.getSession().getServletContext().getRealPath("/");// 自动寻找target根目录
String lsPath = pathRoot + "\\img\\" + newName;
// 判断是否需要创建文件夹
// 物理路径文件夹
File wlFile = new File("D:\\eclipse\\练习\\HouseSell\\WebContent\\img\\");
// 临时路径文件夹
File lsFile = new File(pathRoot + "\\img\\");
if (!wlFile.exists()) {
wlFile.mkdir();
}
if (!lsFile.exists()) {
lsFile.mkdir();
}
// 执行读写操作
if (!multipartFile.isEmpty()) {
// 将文件放入物理路径
FileOutputStream wlFileOutputStream = new FileOutputStream(wlPath);
// 将文件放入临时路径
FileOutputStream lsFileOutputStream1 = new FileOutputStream(lsPath);
int line = 0;
while ((line = inputStream1.read()) != -1) {
// 写入物理路径
wlFileOutputStream.write(line);
// 写入临时路径
lsFileOutputStream1.write(line);
}
wlFileOutputStream.flush();
lsFileOutputStream1.flush();
lsFileOutputStream1.close();
wlFileOutputStream.close();
inputStream1.close();
}
// 将相对路径放入数据库中
house.setHouseImg(ljPath);
houseService.addHouse(house);
return "redirect:/house/listAllHouses";
}
}
SqlMapConfig.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>
<!-- 配置分页插件 -->
<!--分页-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 4.0.0以后版本可以不设置该参数 -->
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样-->
<property name="offsetAsPageNum" value="true"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="true"/>
<!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
<!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
<!-- 不理解该含义的前提下,不要随便复制该配置 -->
<property name="params" value="pageNum=start;pageSize=limit;"/>
<!-- 支持通过Mapper接口参数来传递分页参数 -->
<property name="supportMethodsArguments" value="true"/>
<!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
<property name="returnPageInfo" value="check"/>
</plugin>
</plugins>
</configuration>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置分页插件 -->
<!--分页-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 4.0.0以后版本可以不设置该参数 -->
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样-->
<property name="offsetAsPageNum" value="true"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="true"/>
<!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
<!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
<!-- 不理解该含义的前提下,不要随便复制该配置 -->
<property name="params" value="pageNum=start;pageSize=limit;"/>
<!-- 支持通过Mapper接口参数来传递分页参数 -->
<property name="supportMethodsArguments" value="true"/>
<!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
<property name="returnPageInfo" value="check"/>
</plugin>
</plugins>
</configuration>
spring-dao.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!-- 包扫描 -->
<context:component-scan base-package="com.kumo">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///house?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="588888"></property>
</bean>
<!-- sqlsessionfactory 创建session工厂 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations">
<list>
<value>classpath:com/kumo/dao/*Mapper.xml</value>
</list>
</property>
<property name="typeAliasesPackage" value="com.kumo.bean"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
<!-- 配置mapperScannerConigure,mapper动态代理生成dao实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kumo.dao"></property>
<property name="sqlSessionFactory" ref="sqlSession"></property>
</bean>
<!-- 配置事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!-- 包扫描 -->
<context:component-scan base-package="com.kumo">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///house?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="588888"></property>
</bean>
<!-- sqlsessionfactory 创建session工厂 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations">
<list>
<value>classpath:com/kumo/dao/*Mapper.xml</value>
</list>
</property>
<property name="typeAliasesPackage" value="com.kumo.bean"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
<!-- 配置mapperScannerConigure,mapper动态代理生成dao实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kumo.dao"></property>
<property name="sqlSessionFactory" ref="sqlSession"></property>
</bean>
<!-- 配置事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
上一篇: 三个节点的搭建
推荐阅读